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 时,执行的顺序如下:
-
将 DataRow 中的值移至参数值。
-
引发 OnRowUpdating 事件。
-
执行命令。
-
如果该命令设置为 FirstReturnedRecord,返回的第一项结果将放置在 DataRow 中。
-
如果存在输出参数,它们将被放在 DataRow 中。
-
引发 OnRowUpdated 事件。
-
调用 AcceptChanges。
与 DbDataAdapter 关联的每个命令通常都有一个与其关联的参数集合。参数通过 .NET Framework 数据提供程序的 Parameter 类的 SourceColumn 和 SourceVersion 属性映射到当前行。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 也发生更改:所有 Added 和 Modified 行都变为 Unchanged,Deleted 行则被移除。
在您尝试使用 DbDataAdapter..::.Update 方法更新 DataSet 之后,通常会对 DataTable 调用 AcceptChanges 方法。