SqlDataAdapter.UpdateCommand 属性(极易出错的地方)(转自:http://blog.youkuaiyun.com/tsapi/archive/2011/03/29/6286654.as...

本文介绍如何使用SQLDataAdapter创建增删改查命令,并演示了具体的代码实现。包括如何配置Select、Insert、Update和Delete命令及其参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection) { SqlDataAdapter adapter = new SqlDataAdapter(); // Create the SelectCommand. SqlCommand command = new SqlCommand("SELECT * FROM Customers " + "WHERE Country = @Country AND City = @City", connection); // Add the parameters for the SelectCommand. command.Parameters.Add("@Country", SqlDbType.NVarChar, 15); command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; //以上查询语句中的parameters没有注明对应的字段名称,没错,可以实现查询。否则会出现异常,导致Update方法无法执行。 // Create the InsertCommand. command = new SqlCommand( "INSERT INTO Customers (CustomerID, CompanyName) " + "VALUES (@CustomerID, @CompanyName)", connection); // Add the parameters for the InsertCommand. command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; // Create the UpdateCommand. command = new SqlCommand( "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + "WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand. command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); SqlParameter parameter = command.Parameters.Add( "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID"); parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // Create the DeleteCommand. command = new SqlCommand( "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // Add the parameters for the DeleteCommand. parameter = command.Parameters.Add( "@CustomerID", SqlDbType.NChar, 5, "CustomerID"); parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter; }

调用update方法

public DataSet CreateCmdsAndUpdate(DataSet dataSet, string connectionString, string queryString) { using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand(queryString, connection); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); connection.Open(); DataSet customers = new DataSet(); adapter.Fill(customers); //code to modify data in dataset here adapter.Update(customers); return customers; } }

实例:

public DataSet InsertValues() { SqlConnection con = new SqlConnection(Configuration.Conn); SqlDataAdapter sdp = new SqlDataAdapter(); DataSet dt = new DataSet(); SqlCommand comm = new SqlCommand(); comm.Connection = con; comm.CommandText = "select Names,Address,Pid,Image from Users"; sdp.SelectCommand = comm;//首先要指定selectitem,并且字段要制定清楚,和insert字段个数和名称一致 SqlCommandBuilder scom = new SqlCommandBuilder(sdp); sdp.Fill(dt, "Users"); comm.CommandText = "Insert into Users values(@Pid,@Names,@Address,@Image)"; SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@Pid",SqlDbType.Int,4,"Pid") ,new SqlParameter("@Names",SqlDbType.VarChar,10,"Names") ,new SqlParameter("@Address",SqlDbType.NVarChar,20,"Address") ,new SqlParameter("@Image",SqlDbType.Image,200,"Image") }; pars.ToList<SqlParameter>().ForEach(parm => comm.Parameters.Add(parm)); DataRow dr = null; for (int i = 1; i < 3; i++) { dr = dt.Tables[0].NewRow(); dr["Names"] = "Jeep" + i.ToString(); dr["Address"] = "第" + i.ToString() + "街道"; dr["Pid"] = i + 8; dt.Tables[0].Rows.Add(dr); }//通过对表值的修改,实现方法Update(table) SqlCommandBuilder icom = new SqlCommandBuilder(sdp); sdp.InsertCommand = comm;//insert com语句 sdp.Update(dt.Tables["Users"]); dt.AcceptChanges(); return dt; }

ms-help://MS.MSDNQTR.v90.chs/fxref_system.data/html/1f860610-1b88-52ae-6ae3-a74f76d97880.htm

当使用 Update 时,执行的顺序如下:

  1. 将 DataRow 中的值移至参数值。

  2. 引发 OnRowUpdating 事件。

  3. 执行命令。

  4. 如果该命令设置为 FirstReturnedRecord,返回的第一项结果将放置在 DataRow 中。

  5. 如果存在输出参数,它们将被放在 DataRow 中。

  6. 引发 OnRowUpdated 事件。

  7. 调用 AcceptChanges。

与 DbDataAdapter 关联的每个命令通常都有一个与其关联的参数集合。参数通过 .NET Framework 数据提供程序的 Parameter 类的 SourceColumnSourceVersion 属性映射到当前行。SourceColumn 引用 DataTable 列,而 DbDataAdapter 引用该列来获取当前行的参数值。

ms-help://MS.MSDNQTR.v90.chs/fxref_system.data/html/c83756f0-70c2-4a21-cecf-fd2351e3f319.htm

在调用 AcceptChanges 时,EndEdit 方法被隐式调用,以便终止任何编辑。如果行的 RowState 原来是“Added”或“Modified”,则 RowState 将变成“Unchanged”。如果 RowState 是“删除”,则该行将被移除。

有关更多信息,请参见 BeginEdit 方法。

DataTable 类还具有 AcceptChanges 方法,此方法会影响整个表中所做的更改。

ms-help://MS.MSDNQTR.v90.chs/fxref_system.data/html/e02d9dca-3f45-3d13-bc48-81776568cec1.htm

调用 AcceptChanges 时,任何仍处于编辑模式的 DataRow 对象将成功结束其编辑。DataRowState 也发生更改:所有 AddedModified 行都变为 UnchangedDeleted 行则被移除。

在您尝试使用 DbDataAdapter..::.Update 方法更新 DataSet 之后,通常会对 DataTable 调用 AcceptChanges 方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值