以下代码似乎有问题我想使用以下代码片段在数据库中插入一个值。
DataTable dtUsers = new DataTable("tblUsers");
BindingSource bsUsers = new BindingSource();
SqlDataAdapter daUsers = new SqlDataAdapter("usp_GetUsers", Properties.Resources.ConnectionString);
daUsers.InsertCommand = new SqlCommand("usp_InsertNewUser");
daUsers.InsertCommand.Connection = new SqlConnection(Properties.Resources.ConnectionString);
daUsers.InsertCommand.CommandType = CommandType.StoredProcedure;
daUsers.InsertCommand.Parameters.Clear();
daUsers.InsertCommand.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = txtUser.Text;
daUsers.InsertCommand.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = txtPass.Text;
daUsers.InsertCommand.Parameters.Add("@userType", SqlDbType.Int).Value = cbxUserType.SelectedValue;
daUsers.Update(dtUsers);
在你问之前,存储过程工作正常。而且,如果我将上面的 InsertCommand 更改为 SelectCommand 并调用 Fill 方法,那么一切正常!这件事令人沮丧,因为如果您使用 Select/Fill 组合,数据适配器的插入/更新方法无法正常工作。没有数据行被插入,因为我也监控了这一点。也不会产生异常。铝