ADO.NET笔记——使用DataAdapter执行增删改操作

相关知识:

  1. DataSet中的数据一旦从数据库下载下来,便不再与数据库保持联系。如果修改了DataSet中的数据,需要重新建立连接,并且通过SQL命令将修改更新到数据库去
  2. 编写SQL命令往往比较繁琐和机械化,ADO.NET提供了一个SqlCommandBuilder对象,帮助DataAdapter对象从SELECT语句推算出需要的UPDATE,DELETE和INSERT语句;然后DataAdapter便可以利用这些语句,检查DataSet中被修改的数据,然后提交到数据库
  3. SqlCommandBuilder自动生成的SQL命令虽然方便,但却不灵活。尤其是在需要向多个表中写入数据的时候,往往需要自定义增删改语句

代码示例:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient; namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
string strConn = @"server=Joe-PC;database=AccountDBforSQLInjection;uid=sa;pwd=root";
SqlConnection conn = new SqlConnection(strConn); string sql = "SELECT AccountID,AccountName,password FROM Account";
SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "Account"); Console.WriteLine("Account表中原有数据:"); DataTable dt = ds.Tables["Account"];
DataView dv = new DataView(dt);
dv.Sort = "AccountID ASC"; foreach (DataRowView drv in dv)
{
Console.WriteLine("{0}:{1},{2}", drv[], drv[], drv[]);
} Console.WriteLine(""); //添加一行数据——Insert
DataRow newRow = dt.NewRow(); //根据Table的架构,创建一个新的空行
// 给新行赋值
newRow["AccountID"] = ;
newRow["AccountName"] = "new";
newRow["password"] = "";
// 将新行加到表的行集合中
dt.Rows.Add(newRow); //修改一行数据——Update
DataRow updateRow = dt.Rows[]; //修改表中第一行数据
updateRow["password"] = "";//修改了密码为000000 //删除一行数据——Delete
DataRow deleteRow = dt.Rows[];//准备删除原表中第二行数据
dt.Rows.Remove(deleteRow); //测试内存中数据
Console.WriteLine("修改后,Account表中现有数据");
foreach (DataRowView drv in dv)
{
Console.WriteLine("{0}:{1},{2}", drv[], drv[], drv[]);
} //程序在此暂停等待用户输入,检查数据库,newRow此时尚未插入到数据库中。
//此时newRow仅仅是在DataSet对象内存中创建,并为更新到数据库
Console.WriteLine("");
Console.WriteLine("检查数据库中是否已经创建了新行,然后按回车继续。");
Console.ReadLine(); //更新到数据库
//创建SqlCommandBuilder,并且把DataAdapter对象传入。
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
Console.WriteLine("生成的Insert语句:{0}", cmdBuilder.GetInsertCommand().CommandText);
Console.WriteLine("生成的Update语句:{0}", cmdBuilder.GetUpdateCommand().CommandText);
//之前建DataAdapter时候的sql语句,SELECT的键列,必须含有主键,此处SqlCommandBuilder的update才能成功
Console.WriteLine("生成的Delete语句:{0}", cmdBuilder.GetDeleteCommand().CommandText);
//执行更新
da.Update(dt);//将table中的所有修改更新到数据库
Console.WriteLine("数据更新到数据库。检查数据库中是否已经更新了数据。"); Console.WriteLine(""); //重新输出数据库中数据,检测以上操作是否成功
string _sql = "SELECT AccountID,AccountName,password FROM Account";
SqlDataAdapter _da = new SqlDataAdapter(_sql, conn);
_da.Fill(ds, "newAccount");
Console.WriteLine("Account表中现有数据:"); DataTable _dt = ds.Tables["newAccount"];
DataView _dv = new DataView(_dt);
_dv.Sort = "AccountID ASC"; foreach (DataRowView drv in _dv)
{
Console.WriteLine("{0}:{1},{2}", drv[], drv[], drv[]);
} Console.WriteLine("");
}
}
}

ADO.NET笔记——使用DataAdapter执行增删改操作

程序分析:

  1. 在DataSet中修改数据,仅是改了程序内存中的数据,并不会影响到数据库
  2. 通过SqlCommandBuilder帮助构建增删改的SQL语句。通过打印这些语句命令,可以看出,它们是用过参数来执行SQL命令的。当正式更新数据时,会用DataSet中的修改后的数据来填充这些参数值
  3. 调用SqlDataAdapter对象的Update方法,正式将数据更新到数据库

扩展问题:

如果在Fill一个DataSet时的SQL语句是同时从多个表中联合查询的数据,那么要更新数据时,插删改的语句将如何生成呢?

上一篇:.net core 连接sql server 时提示Connection Timeout Expired


下一篇:洛谷 P3225 [HNOI2012]矿场搭建 解题报告