更新查询不工作表数据没有改变

n7taea2i  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(296)

mysql更新查询不工作。表数据没有更改。我使用mysql和c。我的表字段是:

username,
password,
name,
Loggedin

代码:

try
{
    string userlogout = "UPDATE  UsersTable SET Loggedin = '0'   WHERE username = '" + txtlogoutusername.Text+ "' ";
    obj.cmd.Connection = obj.conn;
    obj.cmd.CommandText = userlogout;
    obj.cmd.ExecuteNonQuery();
    userpanel.Visible = false;
}
catch (Exception ex)
{
    MessageBox.Show("" + ex);
}
zbdgwd5y

zbdgwd5y1#

app.config上的连接字符串

<connectionStrings>
    <add name ="con_str" connectionString="~~~~~your connection string~~~~~~~~~~~"/>
</connectionStrings>

检查select语句

string myconnstring = ConfigurationManager.ConnectionStrings["con_str"].ConnectionString;

    SqlConnection conn = new SqlConnection(myconnstring);
        try
        {
            string sql = "UPDATE UsersTable SET Loggedin = '0' WHERE username = @userName";              
            SqlCommand cmd = new SqlCommand(sql, conn);          
            cmd.Parameters.AddWithValue("@userName", txtlogoutusername.Text);
            conn.Open();
            int rowsAffected =cmd.ExecuteNonQuery();
            Console.WriteLine("rows affected: " + rowsAffected);
        }
        catch(Exception ex)
        {

        }
        finally
        {
            conn.Close();
        }

相关问题