使用c#中的參數(shù)調(diào)用存儲(chǔ)過程我可以在我的程序中進(jìn)行刪除、插入和更新,我試圖通過調(diào)用我的數(shù)據(jù)庫中創(chuàng)建的存儲(chǔ)過程來進(jìn)行插入。這是一個(gè)按鈕插入我使工作良好。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);
}這是調(diào)用名為sp_Add_contact添加聯(lián)系人。兩個(gè)參數(shù)sp_Add_contact(@FirstName,@LastName)..我在谷歌上搜索了一些很好的例子,但沒有發(fā)現(xiàn)什么有趣的地方。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貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
cmd
da.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(); } }}

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
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 關(guān)注
- 371 瀏覽
添加回答
舉報(bào)
0/150
提交
取消