strWhereClause.Substring(0, strWhereClause.Length - 4);这句什么意思?

zhizhang 2003-10-16 05:27:42
这是一个多项选择的例子 其中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>
...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiks 2003-10-16
  • 打赏
  • 举报
回复
up
xinyu_1980 2003-10-16
  • 打赏
  • 举报
回复
strWhereClause去除右边4个字符赋给字符串str
acewang 2003-10-16
  • 打赏
  • 举报
回复
String str = strWhereClause.Substring(0, strWhereClause.Length - 4);
strWhereClause去除右边4个字符赋给字符串str
windofcity 2003-10-16
  • 打赏
  • 举报
回复
截掉strWhereClause的后四个字符!

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧