数据库中没有保存任何内容

发布于 2024-11-17 18:21:06 字数 2210 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生死何惧 2024-11-24 18:21:06

尝试使用command.ExecuteNonQuery()。另请注意,如果您返回单个值(例如新插入记录的 ID),则可以使用 command.ExecuteScalar()。您还应该使用 using 语句来确保为命令和连接调用 Dispose 方法。整个代码应该如下所示:

using(MySqlConnection connection = new MySqlConnection(MyConString))
{
    connection.Open();
    using(MySqlCommand command = connection.CreateCommand())
    {
        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;

        //Execute command
        command.ExecuteNonQuery();
    }
}

Try using command.ExecuteNonQuery(). Also note that you can use command.ExecuteScalar() if you are returning a single value (for instance, the ID of the newly inserted record). You should also use the using statement to ensure that the Dispose methods are called for the command and the connection. The entire code should look something like this:

using(MySqlConnection connection = new MySqlConnection(MyConString))
{
    connection.Open();
    using(MySqlCommand command = connection.CreateCommand())
    {
        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;

        //Execute command
        command.ExecuteNonQuery();
    }
}
爱殇璃 2024-11-24 18:21:06

您未能执行您的命令。尝试:

connection.Open();
command.ExecuteNonQuery();

You have failed to execute your command. Try:

connection.Open();
command.ExecuteNonQuery();
傲影 2024-11-24 18:21:06

打开连接后,您忘记添加 command.ExecuteNonQuery();

You forgot to add command.ExecuteNonQuery(); after you open the connection.

逆光下的微笑 2024-11-24 18:21:06

您需要执行命令才能保存,您只是打开一个连接。

you need to execute command to save, you are only opening a connection.

请远离我 2024-11-24 18:21:06

您可能缺少此代码:

connection.Open();
command.ExecuteNonQuery();
connection.Close(); 

You might be missing this code:

connection.Open();
command.ExecuteNonQuery();
connection.Close(); 
泼猴你往哪里跑 2024-11-24 18:21:06

您添加了参数,打开了连接,但忘记执行命令。放在

command.ExecuteNonQuery();

您的警报框之前。

作为额外的好处,您可能希望将内容包装在 using 语句中。这将确保您的物品得到正确处理并关闭您的连接;

using (MySqlConnection connection = new MySqlConnection(MyConString))
{
    using (MySqlCommand command = connection.CreateCommand())
    {
       command.Connection = connection;
       /// the rest of your database code goes here
       command.ExecuteNonQuery();
}

MessageBox.Show("Data Saved");

You added the parameters, opened the connection but forgot to execute the command. Put

command.ExecuteNonQuery();

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;

using (MySqlConnection connection = new MySqlConnection(MyConString))
{
    using (MySqlCommand command = connection.CreateCommand())
    {
       command.Connection = connection;
       /// the rest of your database code goes here
       command.ExecuteNonQuery();
}

MessageBox.Show("Data Saved");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文