程序 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开发培训视频教程
[] []