1 可以用SqlCommand 操作数据,
string myExecuteQuery="DELETE FROM table1";
SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
2 非要用到SqlDataAdapter 的方法的话.BELOW
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd;
SqlParameter parm; // Create the SelectCommand. cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn); cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15); da.SelectCommand = cmd; // Create the DeleteCommand. cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", conn); parm = cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parm.SourceVersion = DataRowVersion.Original; da.DeleteCommand = cmd; return da;
}
string myExecuteQuery="DELETE FROM table1";
SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
2 非要用到SqlDataAdapter 的方法的话.BELOW
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd;
SqlParameter parm; // Create the SelectCommand. cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn); cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15); da.SelectCommand = cmd; // Create the DeleteCommand. cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", conn); parm = cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parm.SourceVersion = DataRowVersion.Original; da.DeleteCommand = cmd; return da;
}
删除还是直接ExecuteNonQuery 简单~
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
是这样吗?好象不行
===========
应该可以 因为SqlCommandBuilder已经自动生成了删除语句
只是你还没有执行删除操作而已
SqlDataAdapter adapter = new SqlDataAdapter("select * FROM table1", oleDbConnection1);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
//对table进行一些操作(如增删改)后
adapter.Update(table);
在执行Update方法的过程才会自动生成Sql语句去执行。