Fra*_*arp 132 c# sql-server asp.net ado.net stored-procedures
我可以在我的程序中执行删除,插入和更新,并尝试通过从我的数据库中调用创建的存储过程来执行插入操作.
这个按钮插件我工作得很好.
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);
}
Run Code Online (Sandbox Code Playgroud)
这是调用名为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);
}
Run Code Online (Sandbox Code Playgroud)
Guf*_*ffa 249
它与运行查询几乎相同.在原始代码中,您正在创建一个命令对象,将其放入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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Rav*_*dag 35
您必须添加参数,因为SP需要执行
using (SqlConnection con = new SqlConnection(dc.Con))
{
using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
cmd.Parameters.AddWithValue("@LastName", txtlastname.Text);
con.Open();
cmd.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
Rah*_*ate 13
cmd.Parameters.Add(String parameterName, Object value)
现已弃用.而是使用cmd.Parameters.AddWithValue(String parameterName, Object value)
不推荐使用Add(String parameterName,Object value).使用AddWithValue(String parameterName,Object value)
功能方面没有区别.他们弃用
cmd.Parameters.Add(String parameterName, Object value)
赞成的原因AddWithValue(String parameterName, Object value)
是为了更清晰.这是相同的MSDN参考
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.AddWithValue("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
567067 次 |
最近记录: |