我正在使用 SqlDataSource 来填充 GridView。这是我的代码:
private void DataCall()
{
//Object gets created for use from the class(clsDataConduit)
clsDataDictionary AnOrder = new clsDataDictionary();
//Declaring a new SqlDataSource from the inbuilt class library
SqlDataSource sqlDS_ItemTable = new SqlDataSource();
//Using our datasource object being cast onto our objects connectionstring
sqlDS_ItemTable.ConnectionString = AnOrder.ConnectionString;
//Our sql statement being passed through to our .SelectCommand method
sqlDS_ItemTable.SelectCommand = "Select tblOrders.OrderId, tblItem.ItemName, tblOrders.DateOrdered from tblItem, tblOrders where tblItem.ItemId = tblOrders.ItemId AND tblOrders.AuthId = 5";
//Adding controls to our SqlDataSource object
this.Controls.Add(sqlDS_ItemTable);
//Declares the DataSource for our gridview !
grdOrder.DataSource = sqlDS_ItemTable;
//Binds the data to refresh every time it's used
grdOrder.DataBind();
}
正如您在最后的 SQL 语句中看到的那样,我正在这样做tblOrders.AuthId = 5
。但是我想做这样的事情tblOrders.AuthId = SessionAuthId .
我阅读了一些关于单独做某事的帖子,command.Parameters.Add(new SqlParameter("Name", dogName));
但我不知道如何将其应用于我的代码。
我正在使用其他人的代码(教授)在作业中执行此操作,但我想稍微编辑它,因为我将开发登录系统等。
有人可以看一下该方法,看看我将如何更改它以将参数传递给它。这里还有完整的代码:http: //pastebin.com/sdrvW5Zn