插入将添加到mysql db的用户名和密码

时间:2017-07-19 13:08:06

标签: c# mysql

这是我正在尝试使用的代码

        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = conn;

        cmd.CommandText = "insert into student_logintable.login_table(username,password) values = '"+txtusernew.Text.Trim()"','"+txtpasswordnew.Text.Trim()"'";
        cmd.Parameters.Add("username", MySqlDbType.VarChar).Value = " + txtusernew.Text.Trim() ";
        cmd.Parameters.Add("password", MySqlDbType.VarChar).Value = " + txtpasswordnew.Text.Trim()";
        cmd.ExecuteNonQuery();

2 个答案:

答案 0 :(得分:0)

我相信INSERT语句不会使用=符号进行归因。

cmd.CommandText = "INSERT INTO student_logintable.login_table(username, password) VALUES ('" +txtusernew.Text.Trim()+ "', '" +txtpasswordnew.Text.Trim()+ "')"

答案 1 :(得分:0)

我并不像你一样对我进行非常好的格式化参数添加。

我建议使用Parameters.AddWithValue。看看:

cmd.Parameters.AddWithValue("username", txtusernew.Text.Trim());

创建它时,您需要在参数名称中包含@:

cmd.Parameters.AddWithValue("@username", txtusernew.Text.Trim());

您的插入语句也不正确。请记住,格式为:

INSERT INTO [table_name] ([column_name], ...) VALUE|VALUES([value], ...)

所以你的insert语句(带参数)应该更像这样:

cmd.CommandText = "insert into student_logintable.login_table(username,password)
    values(@username, @password)";
相关问题