数据库
首页 > 数据库> > C#使用SqlCommand.Parameters更新表

C#使用SqlCommand.Parameters更新表

作者:互联网

我正在尝试使用SqlCommand更新MSSQL表,我认为这是我的T-SQL的语法错误,但这是我到目前为止:

SqlCommand sqlCmd = new SqlCommand("UPDATE yak_tickets SET email = @emailParam, subject = @subjectParam, text = @textParam, statusid = @statusIDParam, ticketClass = @ticketClassParam WHERE id = @ticketIDParam", sqlConn);

参数正在按预期工作,但是,当我运行代码时,表永远不会更新.任何帮助将不胜感激=)

这是代码的其余部分:

    #region Parameters
    /* Parameters */
    sqlCmd.Parameters.Add("@ticketIDParam", SqlDbType.BigInt);
    sqlCmd.Parameters["@ticketIDParam"].Value = ticketID;

    sqlCmd.Parameters.Add("@emailParam", SqlDbType.NVarChar);
    sqlCmd.Parameters["@emailParam"].Value = ticketToBeSubmitted.getEmail();

    sqlCmd.Parameters.Add("@subjectParam", SqlDbType.NVarChar);
    sqlCmd.Parameters["@subjectParam"].Value = ticketToBeSubmitted.getSubject();

    sqlCmd.Parameters.Add("@textParam", SqlDbType.Text);
    sqlCmd.Parameters["@textParam"].Value = ticketToBeSubmitted.getTicketContent();

    sqlCmd.Parameters.Add("@statusIDParam", SqlDbType.NVarChar);
    sqlCmd.Parameters["@statusIDParam"].Value = ticketToBeSubmitted.getStatus();

    sqlCmd.Parameters.Add("@ticketClassParam", SqlDbType.NVarChar);
    sqlCmd.Parameters["@ticketClassParam"].Value = ticketToBeSubmitted.getTicketClass();
    #endregion

    #region Try/Catch/Finally
    /* Try/Catch/Finally */

    try
    {
        sqlConn.Open();
        sqlCmd.ExecuteNonQuery();
    }
    catch (SqlException sqlEx)
    {
        sqlErrorLabel.Text = sqlEx.ToString();
        sqlErrorLabel.ForeColor = System.Drawing.Color.Red;
    }
    finally
    {
        sqlConn.Close();
    }

并且该方法的签名:

  public static void updateTicketInDatabase(Ticket ticketToBeSubmitted, Label sqlErrorLabel, int ticketID)

解决方法:

UPDATE FROM是无效语法(编辑:OP更正此).问题也可能是“文本”列. text是SQL Server中的关键字,因为它是一种数据类型.尝试在它周围放置括号.

UPDATE yak_tickets 
SET email = @emailParam, 
    subject = @subjectParam, 
    [text] = @textParam, 
    statusid = @statusIDParam, 
    ticketClass = @ticketClassParam 
WHERE id = @ticketIDParam

标签:c,sql,sqlcommand,sqlclient
来源: https://codeday.me/bug/20190527/1159814.html