没有数据发送到MySQL数据库

塞缪尔·韦斯特

我对将MySQL与C#结合使用并不熟悉,但我不明白这里出了什么问题。我的代码能够连接到数据库,但是即使3个文本框中有文本也不会发送任何数据。

public static void testSendData ()
        {
            MainWindow form = new MainWindow();
            string input_username = form.textUsername.Text; 
            string input_password = form.textPassword.Text; 
            string input_email = form.textEmail.Text; 

            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); 
            builder.Server = "localhost"; 
            builder.UserID = "root"; 
            builder.Password = "root"; 
            builder.Database = "authentication"; 
            MySqlConnection connection = new MySqlConnection(builder.ToString()); 
            connection.Open();
            string newuser_sql = "INSERT INTO `authentication`.`account` (`username`, `sha_pass_hash`, `email`) VALUES ('@username', '@pass', '@email');"; 
            MySqlCommand newuser = new MySqlCommand(newuser_sql, connection); 
            newuser.CommandText = newuser_sql; 
            newuser.Parameters.AddWithValue("@username", input_username); 
            newuser.Parameters.AddWithValue("@pass", input_password); 
            newuser.Parameters.AddWithValue("@email", input_email); 
            newuser.ExecuteNonQuery(); 
            MessageBox.Show("Worked."); 
        } 

编辑:我更新了我的“ newuser_sql”字符串,它现在将发送数据,但是它将文字@ username,@ pass,@ email发送到数据库。

cl

MySQL .NET提供程序命令参数格式与典型格式不同。

试试这个:

string newuser_sql = "INSERT INTO `authentication`.`account` (`username`, `sha_pass_hash`, `email`) VALUES (?username, ?pass, ?email);";
//..
newuser.Parameters.AddWithValue("?username", input_username); 
newuser.Parameters.AddWithValue("?pass", input_password); 
newuser.Parameters.AddWithValue("?email", input_email); 

如果该值为空,则调试该input_usernameinput_passwordinput_email实际上包含非空值

更新

事实证明,这几天它实际上也应该可以使用@(只是不能用参数的引号引起来),因此请检查是否确实传递了任何数据(input_username在添加参数时创建一个断点并调试etc的值)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章