C# 使用 SqlCommand.Parameters 更新表

发布于 2024-08-15 00:34:17 字数 1801 浏览 8 评论 0原文

我正在尝试使用 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)

I'm trying to update an MSSQL table using SqlCommand, I think it's a syntax error with my T-SQL, but here is what I have so far:

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

The parameters are working as they should, however, the table never gets updated when I run the code. Any help would be appreciated =)

Here is the rest of the code:

    #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();
    }

And the method's signature:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏闹i 2024-08-22 00:34:17

UPDATE FROM 是无效语法(编辑:OP 更正了这一点)。问题也可能出在“text”列。 text 是 SQL Server 中的关键字,因为它是一种数据类型。尝试用括号括起来。

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

UPDATE FROM is invalid syntax (edit: OP corrected this). The problem might also be the "text" column. text is a keyword in SQL Server, since it's a datatype. Try putting brackets around it.

UPDATE yak_tickets 
SET email = @emailParam, 
    subject = @subjectParam, 
    [text] = @textParam, 
    statusid = @statusIDParam, 
    ticketClass = @ticketClassParam 
WHERE id = @ticketIDParam
水溶 2024-08-22 00:34:17

必须使用 if(!Page.IsPostBack)

Had to use if(!Page.IsPostBack)

玩套路吗 2024-08-22 00:34:17

有几个问题:

  1. 这是在回滚的事务内部吗?
  2. 您是否已验证 @ticketIDParam 是否与表中的一组行匹配?特别是如果它不仅仅是一个整数键,
  3. 您是否正在更新没有副作用的行(即更新为相同的值)?
  4. 您能否提供此查询的paramaters.Add 语句
  5. 表上是否有触发器或其他设置(我假设没有,因为您没有提及任何内容)。
  6. 你说你知道参数工作正常,你能说说你是如何验证这一点的吗? (轮廓仪、目视检查等)。

Couple of questions:

  1. Is this inside of a transaction thats getting rolledback?
  2. Have you verified that you @ticketIDParam matches a set of rows on the table? Especially if its not just a integer key
  3. Are you updating rows that have no side effects (i.e. your updating to the same values)?
  4. Can you provide the paramaters.Add statements for this query
  5. Is there a trigger or other setting on the table (I assume not, as you did not mention anything).
  6. You said you know the params are working correctly, can you say how you verified this? (profiler, visual inspection, etc).
清旖 2024-08-22 00:34:17

听起来您的托管提供商限制了您的调试选项,迫使您以老式的方式进行操作。如果在更新之后立即输入类似以下内容会怎么样:

;SELECT @@ROWCOUNT

然后执行 ExecuteScalar,而不是执行 ExecuteNonQuery,然后查看 SQL 是否认为其更新了任何内容。

Sounds like your hosting provider limits your debug options, forcing you to do it the old fashioned way. What if immediately after the update, you put something like:

;SELECT @@ROWCOUNT

then instead of ExecuteNonQuery, do ExecuteScalar, and see if SQL even thinks its updated anything.

怎言笑 2024-08-22 00:34:17

我不知道自从你第一次问以来情况是否有所改变。但这对我有用,例如:

var SchoolName = cmd.Parameters.AddWithValue("@SchoolName", school.SchoolName);
SchoolName.SqlDbType = SqlDbType.NVarChar;

在你的代码中它是:

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

I don't know if it changed since you first asked. but this works for me , e.g :

var SchoolName = cmd.Parameters.AddWithValue("@SchoolName", school.SchoolName);
SchoolName.SqlDbType = SqlDbType.NVarChar;

In your code it's :

sqlCmd.Parameters.Add("@emailParam", SqlDbType.NVarChar);
sqlCmd.Parameters["@emailParam"].Value = ticketToBeSubmitted.getEmail();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文