数据库中没有保存任何内容
我是 Visual Studio 2010 和 MySQL 的新手。我正在创建一个表单,用户将在其中输入要添加到数据库中的信息。没有发生错误,但是当我检查 MySQL 时,我输入的数据没有保存在我的数据库中。有人可以检查我的代码是否有错误或不正确吗?请帮忙。谢谢。
这是我的代码:
private void button_adduser_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=timekeeping;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.Connection = connection;
command.CommandText = "insert into users(addfname, addmname, addlname, addposition, addcontact, addemail, addbday, addusername, addpassword) values(@fname, @mname, @lname, @position, @contactnumber, @emailadd, @birthday, @username, @password)";
command.Parameters.Add(new MySqlParameter("@fname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@mname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@lname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@position", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@contactnumber", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@emailadd", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.Date));
command.Parameters.Add(new MySqlParameter("@username", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@password", SqlDbType.VarChar));
command.Parameters["@fname"].Value = addfname.Text.ToLower();
command.Parameters["@mname"].Value = addmname.Text.ToLower();
command.Parameters["@lname"].Value = addlname.Text.ToLower();
command.Parameters["@position"].Value = addposition.Text;
command.Parameters["@contactnumber"].Value = addcontact.Text;
command.Parameters["@emailadd"].Value = addemail.Text;
command.Parameters["@birthday"].Value = addbday.Text;
command.Parameters["@username"].Value = addusername.Text;
command.Parameters["@password"].Value = addpassword.Text;
connection.Open();
MessageBox.Show("Data Saved");
}
I'm new to Visual Studio 2010 and MySQL. I'm creating a form where the user will enter the information to be added in the database. No errors occur, however when I check MySQL the data that I've entered was not saved in my database. Can someone check my code for errors or lack of correctness? Please help. Thanks.
Here's my code:
private void button_adduser_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=timekeeping;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.Connection = connection;
command.CommandText = "insert into users(addfname, addmname, addlname, addposition, addcontact, addemail, addbday, addusername, addpassword) values(@fname, @mname, @lname, @position, @contactnumber, @emailadd, @birthday, @username, @password)";
command.Parameters.Add(new MySqlParameter("@fname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@mname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@lname", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@position", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@contactnumber", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@emailadd", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.Date));
command.Parameters.Add(new MySqlParameter("@username", SqlDbType.VarChar));
command.Parameters.Add(new MySqlParameter("@password", SqlDbType.VarChar));
command.Parameters["@fname"].Value = addfname.Text.ToLower();
command.Parameters["@mname"].Value = addmname.Text.ToLower();
command.Parameters["@lname"].Value = addlname.Text.ToLower();
command.Parameters["@position"].Value = addposition.Text;
command.Parameters["@contactnumber"].Value = addcontact.Text;
command.Parameters["@emailadd"].Value = addemail.Text;
command.Parameters["@birthday"].Value = addbday.Text;
command.Parameters["@username"].Value = addusername.Text;
command.Parameters["@password"].Value = addpassword.Text;
connection.Open();
MessageBox.Show("Data Saved");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用command.ExecuteNonQuery()。另请注意,如果您返回单个值(例如新插入记录的 ID),则可以使用
command.ExecuteScalar()
。您还应该使用using
语句来确保为命令和连接调用Dispose
方法。整个代码应该如下所示:Try using
command.ExecuteNonQuery()
. Also note that you can usecommand.ExecuteScalar()
if you are returning a single value (for instance, the ID of the newly inserted record). You should also use theusing
statement to ensure that theDispose
methods are called for the command and the connection. The entire code should look something like this:您未能执行您的命令。尝试:
You have failed to execute your command. Try:
打开连接后,您忘记添加
command.ExecuteNonQuery();
。You forgot to add
command.ExecuteNonQuery();
after you open the connection.您需要执行命令才能保存,您只是打开一个连接。
you need to execute command to save, you are only opening a connection.
您可能缺少此代码:
You might be missing this code:
您添加了参数,打开了连接,但忘记执行命令。放在
您的警报框之前。
作为额外的好处,您可能希望将内容包装在 using 语句中。这将确保您的物品得到正确处理并关闭您的连接;
You added the parameters, opened the connection but forgot to execute the command. Put
before your alert box.
As an added bonus you'll probably want to wrap things in a using statement. This will ensure your objects are disposed of properly and your connection is closed;