命令执行期间遇到致命错误。 [英] Fatal error encountered during command execution.

查看:76
本文介绍了命令执行期间遇到致命错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中遇到MySQL问题。我想在我的MySQL数据库中更新数据。



I have a Problem during MySQL in C#. I want update data in my MySQL Database.

String MyCon = "SERVER=********** +
            DATABASE=asterisk +
             UID=root +
             PASSWORD=**** + Convert Zero Datetime = True";

private void btnsubmit_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection(MyCon);
            con.Open();                          
            MySqlCommand cmd = new MySqlCommand(@"Update 
userid, password, role, first_name, last_name, user_group, user_level, active 
from aster_users 
Set password=@password, role=@role, first_name=@first_name, last_name=@last_name,
user_level=@user_level, user_group=@user_group, active=@active 
where userid = userid ", con);
            cmd.ExecuteNonQuery();
            cmd.Parameters.AddWithValue("@pasword",txtconformpassword.Text );
            cmd.Parameters.AddWithValue("@role",cmbrole.Text);
            cmd.Parameters.AddWithValue("@first_name",txtfirstname.Text);
            cmd.Parameters.AddWithValue("@last_name",txtlastname.Text);
            cmd.Parameters.AddWithValue("@user_group",cmbUser1.Text);
            cmd.Parameters.AddWithValue("@user_level",cmbuser.Text);
            cmd.Parameters.AddWithValue("@active",cmbstatus.Text);
            
            MessageBox.Show("updated......");
            con.Close();
        }





帮我...



help me...

推荐答案

ExecuteNonQuery()应该是最后一个命令:

ExecuteNonQuery() should be the last command:
cmd.Parameters.AddWithValue("@pasword",txtconformpassword.Text );
cmd.Parameters.AddWithValue("@role",cmbrole.Text);
cmd.Parameters.AddWithValue("@first_name",txtfirstname.Text);
cmd.Parameters.AddWithValue("@last_name",txtlastname.Text);
cmd.Parameters.AddWithValue("@user_group",cmbUser1.Text);
cmd.Parameters.AddWithValue("@user_level",cmbuser.Text);
cmd.Parameters.AddWithValue("@active",cmbstatus.Text);

cmd.ExecuteNonQuery();// <---- moved to after the parameters


cut

cut
cmd.ExecuteNonQuery();



并粘贴之前


and paste it before

MessageBox.Show("updated......");


由于你没有说出你会得到什么样的错误,我认为这部分会给你带来麻烦。

As you don't say what kind of error you get I assume that this part is causing you problems.
where userid = userid "



您要插入文字字符串userid,而不是变量 userid 的值。




You are inserting the literal string "userid", not the value of the variable userid.

string sqlCommand = string.Format(@"UPDATE 
userid, password, role, first_name, last_name, user_group, user_level, active 
FROM aster_users 
SET password=@password, role=@role, first_name=@first_name, last_name=@last_name,
user_level=@user_level, user_group=@user_group, active=@active 
WHERE userid = '{0}';", userid);

// This line can be used to check if the SQL command looks OK.
// It also makes it easy to copy the command into the SQL query tool for testing
Debug.WriteLine(sqlCommand);

MySqlCommand cmd = new MySqlCommand(sqlCommand, con);



它是将长代码块拆分成较小的部分并不危险。这样做可以更容易地调试代码。



就个人而言,如果我使用大写字母表示SQL预定义的单词,我会发现读取SQL字符串更容易。这只是我个人的口味,对此有很多意见。


It is not dangerous to split a long code chunk into smaller parts. Doing so makes it easier to debug the code.

Personally I find it easier to read an SQL string if I use capital letters for the SQL pre-defined words. This is just my personal taste and there are many opinions about this.


这篇关于命令执行期间遇到致命错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆