简体   繁体   中英

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.

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:

    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)
           }
       }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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