的两条记录。选择employees并且计算它们的定单总数的SQL查询,将会如下:
SELECT count(*) AS Orders, E.FirstName, E.LastName
FROM Orders O
INNER JOIN Employees E ON O.EmployeeID = E.EmployeeID
WHERE E.EmployeeID IN(SELECT Value FROM fn_Split(@employeeIDs, '',''))
GROUP BY FirstName, LastName
ORDER BY count(*) DESC
使用以上查询所需要的是必须建立和传递@employeeIDs参数。这个参数将是用逗号隔开的IDs列表。为了建立该字符串,为了弄明白行是否被用户选择,我们需要使用一个循环,这一循环以行数循环次数,并且检查每一个checkbox控件。如果用户选择了行,通过从表的DataKeys属性中(它被建立在ASPX文件中来指向EmployeeID字段)提取检验人,将关键字保存在employee中。
private string GetCheckedEmployeeIDs()
{
String delimiter = String.Empty;
StringBuilder employeeIDs = new StringBuilder();
for(int i = 0; i < DataGrid1.Items.Count; i++)
{
CheckBox checkbox;
checkbox = DataGrid1.Items[i].FindControl("EmployeeCheckBox") as CheckBox;
if(checkbox != null && checkbox.Checked == true)
{
employeeIDs.Append(delimiter + DataGrid1.DataKeys[i].ToString()) ;
delimiter = ",";
}
}
return employeeIDs.ToString();
}
以上方法返回一个字符串,像“10,7,20”。对Orders按钮单击事件处理器将涉及这样一个方法,将信息传递至SQL以得到employees和orders的列表,并且将其结果绑定在第二个DataGrid对象中。
private void Orders_Click(object sender, System.EventArgs e)
{
string employeeIDs = GetCheckedEmployeeIDs();
Database db = DatabaseFactory.CreateDatabase();
DBCommandWrapper dbCommandWrapper;
using(dbCommandWrapper = db.GetSqlStringCommandWrapper(SELECT_ORDERS))
{
dbCommandWrapper.AddInParameter("@employeeIDs", DbType.String, employeeIDs);
using (IDataReader dataReader = db.ExecuteReader(dbCommandWrapper))
{
DataGrid2.DataSource = dataReader;
DataGrid2.DataBind();
}
}
}