从C#代码背后激发的SQL存储过程无法处理UPDATE

本文关键字:存储过程 处理 UPDATE SQL 代码 背后 | 更新日期: 2023-09-27 18:00:19

我有一个从C#代码后面调用的存储过程。代码会激发,但更新命令不会执行。如果存储过程直接运行,则可以正常工作。我想我脑子里有个屁。请帮忙。

代码:

protected void btnAbout_Click(object sender, EventArgs e)
{
    SqlConnection myConnection = new SqlConnection(strConnection);
    SqlCommand myCommand = new SqlCommand("spUpdateCMSAbout", myConnection);
    myConnection.Open();
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.Add("@AboutText", SqlDbType.NVarChar, -1).Value = txtAbout.Text.ToString();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
}

存储过程:

ALTER PROCEDURE fstage.spUpdateCMSAbout
(
   @AboutText nvarchar(max)
)
AS
BEGIN
SET NOCOUNT ON;
    UPDATE fstage.staticCMS SET About = @AboutText;
END

标记:

<asp:Button ID="btnAbout" runat="server" 
    Text="Save" CausesValidation="False" onclick="btnAbout_Click" 
    UseSubmitBehavior="False" />

C#.NET 4.0

从C#代码背后激发的SQL存储过程无法处理UPDATE

您的代码应该可以工作。然而,您可以稍微简化它,使其更加健壮:

using(var myConnection = new SqlConnection(strConnection))
using(var myCommand = myConnection.CreateCommand())
{
    myCommand.CommandText = "spUpdateCMSAbout";
    myCommand.CommandType = CommandType.StoredProcedure;
    myCommand.Parameters.AddWithValue("AboutText", txtAbout.Text);
    myConnection.Open();
    myCommand.ExecuteNonQuery();
}

如果没有,请尝试通过直接调用UPDATE:绕过存储过程

using (var myConnection = new SqlConnection(strConnection))
using (var myCommand = myConnection.CreateCommand())
{
    myCommand.CommandText = "UPDATE fstage.staticCMS SET About = @AboutText";
    myCommand.Parameters.AddWithValue("AboutText", txtAbout.Text);
    myConnection.Open();
    myCommand.ExecuteNonQuery();
}

如果它不起作用,那么UPDATE fstage.staticCMS SET About = 'something'很可能因为某种触发而什么都不做。

我总是运行SQL Server Profiler并在数据库上设置跟踪。然后您可以确定您的数据库是否正在获取查询。如果是,那么你知道问题出在你的数据库中,如果不是,那么你就知道这是你的代码。

protected void uiUpdateButton_Click(object sender, EventArgs e) {
    using(SqlConnection con = new SqlConnection(connstring)) {
        con.Open();
        SqlCommand cmd = new SqlCommand("usb_update_empdtls", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@EmployeeID", SqlDbType.NVarChar, -1).Value = TextBox1.Text.ToString();
        SqlParameter parEmployeeID = AddInputParameters("@EmployeeID", SqlDbType.Int, 10, ParameterDirection.Input, TextBox1.Text);
        SqlParameter parLastName = AddInputParameters("@LastName", SqlDbType.VarChar, 20, ParameterDirection.Input, TextBox2.Text);
        SqlParameter parFirstName = AddInputParameters("@FirstName", SqlDbType.VarChar, 20, ParameterDirection.Input, TextBox3.Text);
        SqlParameter parTitle = AddInputParameters("@Title", SqlDbType.VarChar, 100, ParameterDirection.Input, TextBox4.Text);
        SqlParameter parPresentAddress = AddInputParameters("@PresentAddress", SqlDbType.VarChar, 60, ParameterDirection.Input, TextBox5.Text);
        SqlParameter parCity = AddInputParameters("@City", SqlDbType.VarChar, 50, ParameterDirection.Input, TextBox6.Text);
        SqlParameter parPostalCode = AddInputParameters("@PostalCode", SqlDbType.VarChar, 10, ParameterDirection.Input, TextBox7.Text);
        SqlParameter parCountry = AddInputParameters("@Country", SqlDbType.VarChar, 50, ParameterDirection.Input, TextBox8.Text);
        SqlParameter parExtension = AddInputParameters("@Extension", SqlDbType.VarChar, 4, ParameterDirection.Input, TextBox9.Text);
        SqlParameter parResult = AddOutputParameter("@Result", SqlDbType.VarChar, 100, ParameterDirection.Output);
        cmd.Parameters.Add(parEmployeeID);
        cmd.Parameters.Add(parLastName);
        cmd.Parameters.Add(parFirstName);
        cmd.Parameters.Add(parTitle);
        cmd.Parameters.Add(parPresentAddress);
        cmd.Parameters.Add(parCity);
        cmd.Parameters.Add(parPostalCode);
        cmd.Parameters.Add(parCountry);
        cmd.Parameters.Add(parExtension);
        cmd.Parameters.Add(parResult);
        cmd.ExecuteNonQuery();
        uiResultLabel.Visible = true;
        uiResultLabel.Text = "updated Successfully";
    }
}