简体   繁体   English

如何在两个条件之间更新没有公共字段的单个表?

[英]How to update single table with no common fields inbetween and with two conditions?

I am making webservice in asp.net with c# and MYSQL. 我正在使用c#和MYSQL在asp.net中创建webservice。

I want to update one table based on two condition. 我想根据两个条件更新一个表。 But there is no any common fields between them. 但是它们之间没有任何共同的领域。

Is it possible ? 可能吗 ?

 **EDITED** -

I have used code as - 我用过代码 -

             using (MySqlConnection con1 = new MySqlConnection(conString))
             {

                string update = "UPDATE tbl_UserInfo U SET Chips= @param1 where UserName = @param2 AND QuestionID = @param3";

                using (MySqlCommand cmd1 = new MySqlCommand(update, con1))
                {
                    cmd1.Parameters.AddWithValue("@param1", getChips(answerby));
                    cmd1.Parameters.AddWithValue("@param2", answerby);
                    cmd1.Parameters.AddWithValue("@param3", queid);
                    con1.Open();
                    success = cmd1.ExecuteNonQuery();
                    con1.Close();

                    if (success > 0)
                    {
                       return success;
                    }
                     else
                         return 0;

                }


            }

But it gives an error 但它给出了一个错误

    Unknown column QuestionID in where clause

What can be the problem ? 可能是什么问题?

Waiting for solution ... 等待解决方案......

You have to use SqlCommand class and provide parameters, and pass the input to one of them: 您必须使用SqlCommand类并提供参数,并将输入传递给其中一个:

    using(SqlConnection conn = new SqlConnection("connString"))
    {
        string incrementChips = "UPDATE tbl_UserInfo U SET Chips= @param1 where UserName = @param2 AND QuestionID = @param3";
        // no need to use U.Chips or U.UserName, since you do not update in 2 or more table (where can be same fields)
        using(SqlCommand cmd = new SqlCommand(incrementChips, conn))
        {
           //input parameter:
           cmd.Parameters.Add("@param1", SqlDbType.VarChar, 50).Value = "your input string";
           //and condition parameters:
           cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = "answerby";
           cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = "queid";
           try
           {
               conn.Open();
               cmd.ExecuteNonQuery();
           }
           catch(Exception ex)
           {
               //show exception to user (if it happens)
           }
       }
   }

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

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