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
上一篇:.net core linux环境下 数据库连接超时


下一篇:解决System.Data.SqlClient.SqlException (0x80131904): Timeout 时间已到的问题