正在加载图片...
按钮,其单击事件代码如下: protected void EditRow Click(object sender, EventArgs e) SqlConnection conn=new Sqlconnectiono conn. Connection String=@" Data source=服务器名; Initial Catalog= Student Integrated Security=True ∥把“服务器名”更改为自己的 SQLServer服务器实例名 SqlDataAdapter da= new SqlDataAdapterO string SelectSql="select* from StudentInfo da. Select Command=new SqlCommand(selectSql, conn) SqlCommand Builder scb= new SqlCommand Builder(da,∥为 Data Adapter自动生成更新 命令 Data Set ds=new DataSet da Fill(ds) ∥创建行对象,从表 Tables(0对象中得到要修改的行,修改第4行的记录 DataRow My Row =ds. Tables[0]. Rows(3 My Row" StudentID]="200902601100;该行各字段赋以新值,如果学号重复,则 出错 My Row" StudentName"]="张无忌" Grid Viewl DataSource= ds. Tables[O,∥在GridⅤiew中显示添加记录后的结果 Grid View l. DataBindo a Update(ds),∥将 DataSet中数据变化提交到数据库(更新数据库)中 (3)通过 Data Set对象删除数据表记录。在Web窗体中添加“删除记 录”按钮,其单击事件代码如下: protected void DelRecord Click(object sender, EventArgs e) SqlConnection conn =new Sqlconnectiono conn ConnectionString:=@" Data Source=服务器名; Initial Catalog= StudentDB Integrated Security=True ∥把“服务器名”更改为自己的 SQLServer服务器实例名 SqlDataAdapter da= new SqlDataAdapterO string SelectSql="select* from StudentInfo da SelectCommand=new SqlCommand( selectSql, conn) SqlCommand Builder scb= new SqlCommand Builder(da)∥为 DataAdapter自动生成更新 命令 DataSet ds=new DataSet da Fill(ds) ∥创建行对象,指定要删除的行索引值(删除第1个记录) Data Row DeleteRow =ds. Tables[0]. Rows[0] Delete Row. Delete,∥调用 DataRow对象的 DeleteD方法,从 DataSet填充的数据表中删按钮,其单击事件代码如下: protected void EditRow_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString=@"Data Source=服务器名;Initial Catalog=StudentDB; Integrated Security=True"; //把“服务器名”更改为自己的 SQL Server 服务器实例名 SqlDataAdapter da = new SqlDataAdapter(); string SelectSql = "select * from StudentInfo"; da.SelectCommand = new SqlCommand(SelectSql, conn); SqlCommandBuilder scb = new SqlCommandBuilder(da); //为 DataAdapter 自动生成更新 命令 DataSet ds = new DataSet(); da.Fill(ds); //创建行对象,从表 Tables[0]对象中得到要修改的行,修改第 4 行的记录 DataRow MyRow = ds.Tables[0].Rows[3]; MyRow["StudentID"] = "200902601100"; //为该行各字段赋以新值,如果学号重复,则 出错 MyRow["StudentName"] = "张无忌"; GridView1.DataSource = ds.Tables[0]; //在 GridView 中显示添加记录后的结果 GridView1.DataBind(); da.Update(ds); //将 DataSet 中数据变化提交到数据库(更新数据库)中 } (3) 通过 DataSet 对象删除数据表记录。在 Web 窗体中添加“删除记 录”按钮,其单击事件代码如下: protected void DelRecord_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString=@"Data Source=服务器名;Initial Catalog=StudentDB; Integrated Security=True"; //把“服务器名”更改为自己的 SQL Server 服务器实例名 SqlDataAdapter da = new SqlDataAdapter(); string SelectSql = "select * from StudentInfo"; da.SelectCommand = new SqlCommand(SelectSql, conn); SqlCommandBuilder scb = new SqlCommandBuilder(da); //为 DataAdapter 自动生成更新 命令 DataSet ds = new DataSet(); da.Fill(ds); //创建行对象,指定要删除的行索引值(删除第 1 个记录) DataRow DeleteRow = ds.Tables[0].Rows[0]; DeleteRow.Delete(); //调用 DataRow 对象的 Delete()方法,从 DataSet 填充的数据表中删
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有