简体   繁体   English

使用存储过程更新C#中的列

[英]Using a stored procedure to update columns in C#

I am trying to update columns in my table by filling out text boxes and clicking save; 我想通过填写文本框并单击“保存”来更新表格中的列; I don't get an error or anything. 我没有得到任何错误或错误。 Just nothing happens! 什么都没发生!

Here is my stored procedure: 这是我的存储过程:

ALTER PROCEDURE [dbo].[sp_UpdateProj]
    @ORAID INT = NULL,
    @FullTitle NVARCHAR(250) = NULL
AS
BEGIN
    UPDATE tbl_ProjectFile
    SET FullTitle = @FullTitle
    WHERE ORAID = @ORAID
END

and it works when I run it in SQL Server Management Studio, given an ID and Title name 如果我在SQL Server Management Studio中运行它,它会在ID和标题名称下运行

Here is my C# code 这是我的C#代码

protected void Button_Save_Click(object sender, EventArgs e)
{
    string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(connectionStr))
    {
        con.Open();

        string query = "sp_UpdateProj Where ORAID=" + int.Parse(TextBox_ORAID.Text);

        SqlCommand cmd = new SqlCommand(query, con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;

        cmd.Parameters.AddWithValue("@ORAID", Convert.ToInt32(TextBox_ORAID.Text));
        cmd.Parameters.AddWithValue("@FullTitle", TextBox_FullTitle.Text);

        con.Close();
    }
}

You're setting everything up (almost) correctly - but you're never actually executing the stored procedure! 您正在(几乎)正确设置所有内容 - 但您实际上从未执行过存储过程!

Try this code: 试试这段代码:

protected void Button_Save_Click(object sender, EventArgs e)
{
    string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
    // the query string should be **ONLY** the stored procedure name - nothing else!
    string query = "dbo.sp_UpdateProj";

    // you should put **both** SqlConnection and SqlCommand in "using" blocks
    using (SqlConnection con = new SqlConnection(connectionStr))
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        // fill the parameters - avoiding "AddWithValue"
        cmd.Parameters.Add("@ORAID", SqlDbType.Int).Value = Convert.ToInt32(TextBox_ORAID.Text);
        cmd.Parameters.Add("@FullTitle", SqlDbType.NVarChar, 250).Value = TextBox_FullTitle.Text;

        con.Open();
        // you need to **EXECUTE** the command !
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

There are some errors in the Button_Save_Click event handler: Button_Save_Click事件处理程序中存在一些错误:

  1. When you use commandType is StoredProcedure you have to pass just the stored procedure name 当您使用commandTypeStoredProcedure您必须只传递存储过程名称

  2. With having a stored procedure with sp_ prefix create performance issue ( Using sp_ as prefix for user stored procedures in SQL server causing performance impact ) 使用带有sp_前缀的存储过程会产生性能问题( 使用sp_作为SQL Server中用户存储过程的前缀,从而导致性能影响

  3. You forgot to call the ExecuteNonQuery method 你忘了调用ExecuteNonQuery方法

Try this code: 试试这段代码:

protected void Button_Save_Click(object sender, EventArgs e)
{
 string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
 string procedureName = "dbo.UpdateProj";


 using (SqlConnection con = new SqlConnection(connectionStr))
 using(SqlCommand cmd = new SqlCommand(procedureName , con))
 {

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@ORAID", Convert.ToInt32(TextBox_ORAID.Text));
    cmd.Parameters.AddWithValue("@FullTitle", TextBox_FullTitle.Text);
    con.Open();
    cmd.ExecuteNonQuery()
    con.Close();
 }
}

Your query line should just be: 您的查询行应该只是:

string query = "sp_UpdateProj";

You already have the parameters as objects below that. 您已将参数作为下面的对象。

Then add 然后加

cmd.ExecuteNonQuery();

to execute 执行

Here is the brief info bout executing stored procedure using C# 这是使用C#执行存储过程的简要信息

Call a stored procedure with parameter in c# 使用c#中的参数调用存储过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM