下面的代碼似乎有問題,我想使用下面的代碼片段在數據庫中插入一個值。SQL數據適配器插入命令
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組合,數據適配器的Insert/update方法不能正常工作。沒有數據行正在插入,因爲我也監視了這一點。也不會產生異常。 Al