它与运行查询几乎相同。在原始代码中,您正在创建一个命令对象,将其放入
cmd变量中,并且永远不要使用它。但是,在这里,您将使用而不是
da.InsertCommand。
另外,
using对所有一次性物品都使用A ,以确保正确放置它们:
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(); } }}


