程序 ST_RCatgManModuleascxcs
protected void GridView_Delete(Object sender
GridViewDeleteEventArgs E)
{
//從文件WebConfig中讀取連接字符串
string ST_sqldb =
ConfigurationSettingsAppSettings[ConnectionString];
//連接ST_GinShopManage數據庫
SqlConnection ST_Conn = new SqlConnection(ST_sqldb)
ST_ConnOpen()
//定義SQL語句
String delsql = delete from ST_RoomCategory where ST_RCategoryId
= @RCategoryId;
//創建ST_mycommand對象調用delsql
SqlCommand ST_mycommand = new SqlCommand(delsql ST_Conn)
ST_mycommandParametersAdd(@RCategoryId SqlDbTypeVarChar)
//從dg_RCategoryList中獲取RCategoryId值
ST_mycommandParameters[@RCategoryId]Value =
dg_RCategoryListDataKeys[ERowIndex]ValueToString()
ST_mycommandExecuteNonQuery()
dg_RCategoryListEditIndex = ;
//更新dg_RCategoryList
Show_RCategoryList()
}
protected void GridView_Page(Object sender GridViewPageEventArgs E)
{
dg_RCategoryListPageIndex = ENewPageIndex;
Show_RCategoryList()
}
【代碼說明】當刪除某行時先創建數據庫連接然後執行代碼第~行的SQL刪除語句此語句中有一個參數@RCategoryId用於指定要刪除數據的ID代碼第~行創建並設置此參數的值代碼第行重新綁定網格顯示更新後的數據
注意ExecuteNonQuery()方法沒有返回值
事件ShowAll_Click用來顯示所有的房間類型它同樣調用了Show_RCategoryList()方法btn_ search_Click事件將RnameTextBox的Text內容作為條件在數據庫中查詢出要搜索的房間類型並顯示出來整段代碼如程序所示
程序 ST_RCatgManModuleascxcs
private void ShowAll_Click(object sender SystemEventArgs e)
{
Show_RCategoryList()
}
private void btn_search_Click(object sender SystemEventArgs e)
{
//從文件WebConfig中讀取連接字符串
string ST_sqldb =
ConfigurationSettingsAppSettings[ConnectionString];
//連接ST_GinShopManage數據庫
SqlConnection ST_Conn = new SqlConnection (ST_sqldb)
//定義SQL語句
String ST_selsql=select
ST_RCategoryIdST_NameST_BedNumST_Price from ST_RoomCategory where
ST_Name = @Name;
//創建SqlDataAdapter對象調用ST_selsql
SqlDataAdapter ST_myadapter = new SqlDataAdapter
(ST_selsqlST_Conn)
ST_myadapterSelectCommandParameters Add
(@NameSqlDbTypeVarChar )
ST_myadapterSelectCommandParameters [@Name]Value
=RNameTextBoxTextTrim()
//創建並填充DataSet
DataSet ds = new DataSet ()
ST_myadapterFill (ds)
dg_RCategoryListDataSource =ds;
dg_RCategoryListDataBind()
ST_ConnClose ()
}
【代碼說明】上述代碼是一個標准的查詢數據庫流程最關鍵的是第行這是查詢語句的條件這裡使用了參數@Name;第行是設置參數的值最後查詢數據並綁定到網格控件中
返回目錄ASPNET項目開發指南
編輯推薦
ASPNET MVC 框架揭秘
ASPNET開發寶典
ASP NET開發培訓視頻教程
[] []
From:http://tw.wingwit.com/Article/program/net/201311/15986.html