strWhereClause.Substring(0, strWhereClause.Length - 4);这句什么意思?
这是一个多项选择的例子 其中String str = strWhereClause.Substring(0, strWhereClause.Length - 4);这句是什么意思呀?谢谢!
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head><title>Multiple Selections</title></head>
<body>
<h3>Multiple Selections</h3>
<form runat="server">
<asp:CheckBoxList id="ckbEmployees" runat="server"
RepeatLayout="table"
RepeatDirection="vertical"
RepeatColumns="3"
CellPadding="9"
CellSpacing="18"
TextAlign="right"
OnSelectedIndexChanged="subListChange"
AutoPostBack="true" />
<br/><br/>
<asp:DataGrid id="dgEmployee" runat="server" />
</form>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
String strConnection = ConfigurationSettings.AppSettings["NWind"];
String strSQLforCheckBoxes = "SELECT LastName, EmployeeID " + "FROM Employees ORDER BY LastName;";
SqlConnection objConnection = new SqlConnection(strConnection);
SqlCommand objCommand = new SqlCommand(strSQLforCheckBoxes, objConnection);
objConnection.Open();
ckbEmployees.DataSource = objCommand.ExecuteReader();
ckbEmployees.DataTextField = "LastName";
ckbEmployees.DataValueField = "EmployeeID";
ckbEmployees.DataBind();
objConnection.Close();
}
}
private void subListChange(object s, System.EventArgs e)
{
// Remove next line prior to deployment
//Response.Write("subListChange triggered<hr/>");
String strWhereClause = "";
foreach (ListItem liThisOne in ckbEmployees.Items)
{
if (liThisOne.Selected)
{
strWhereClause += "EmployeeID=" + liThisOne.Value + " OR ";
}
}
// Remove next line prior to deployment
// Response.Write("strWhereClause = <br/>" + strWhereClause + "<hr/>");
if (strWhereClause.Length > 0)
{
dgEmployee.Visible = true;
// This line removes the final OR from the WHERE clause
String str = strWhereClause.Substring(0, strWhereClause.Length - 4);
strWhereClause = "WHERE " + str;
String strConnection = ConfigurationSettings.AppSettings["NWind"];
String strSQLforGrid = "SELECT TitleOfCourtesy, FirstName, " + "LastName, Country, Region, City, Notes " + "FROM Employees " + strWhereClause;
SqlConnection objConnection = new SqlConnection(strConnection);
SqlCommand objCommand = new SqlCommand(strSQLforGrid, objConnection);
// Remove next line prior to deployment
// Response.Write("strSQLforGrid = <br/>" + strSQLforGrid + "<hr/>");
objConnection.Open();
dgEmployee.DataSource = objCommand.ExecuteReader();
dgEmployee.DataBind();
objConnection.Close();
}
else
dgEmployee.Visible = false;
}
</script>