使用c#中的参数调用存储过程我可以在我的程序中进行删除、插入和更新,我试图通过调用我的数据库中创建的存储过程来进行插入。这是一个按钮插入我使工作良好。private void btnAdd_Click(object sender, EventArgs e){
SqlConnection con = new SqlConnection(dc.Con);
SqlCommand cmd = new SqlCommand("Command String", con);
da.InsertCommand = new SqlCommand("INSERT INTO tblContacts VALUES (@FirstName, @LastName)", con);
da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}这是调用名为sp_Add_contact添加联系人。两个参数sp_Add_contact(@FirstName,@LastName)..我在谷歌上搜索了一些很好的例子,但没有发现什么有趣的地方。private void button1_Click(object sender, EventArgs e){
SqlConnection con = new SqlConnection(dc.Con);
SqlCommand cmd = new SqlCommand("Command String", con);
cmd.CommandType = CommandType.StoredProcedure;
???
con.Open();
da. ???.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
3 回答
四季花海
TA贡献1811条经验 获得超5个赞
cmdda.InsertCommand.
using
private void button1_Click(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}}
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
using (SqlConnection con = new SqlConnection(dc.Con)){
using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtfirstname);
cmd.Parameters.AddWithValue("@LastName", txtlastname);
con.Open();
cmd.ExecuteNonQuery();
} }- 3 回答
- 0 关注
- 643 浏览
添加回答
举报
0/150
提交
取消
