MySQL连接phpMyAdmin用C#创建数据库

时间:2014-06-11 08:00:19

标签: c# mysql phpmyadmin

由于昨天我们的问题已经结束,我们不得不开一个新问题。所以这里。 这是一个老问题:MySQL connection with C# through PHPMyAdmin created database

首先,感谢所有答案!所以我们已经实现了Rahul和ActiveHigh的答案并更新了代码。此外,我们添加了一种检查连接是否成功的方法。现在,当我们尝试插入数据时,我们会从catch中获取错误消息。测试位置仍然相同。以下是数据库中表格的图像:https://www.dropbox.com/s/g2c70ty9qb1h7bw/ScreenshotDatabase.png

任何人都知道出了什么问题或想法如何调试它? (我们已经在phpmyadmin中检查了表是否为空查询。它是空的。

protected void Button1_Click(object sender, EventArgs e)
{
MySql.Data.MySqlClient.MySqlConnection connection;
string server = "db.cce-solutions.dk";
string database = "web626445";
string uid = "******";
string password = "******";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";


connection = new MySqlConnection(connectionString);

try
{
    connection.Open();
    if (connection.State == ConnectionState.Open)
    {
        DisplayMessage.Text = "Data entered succesfully.";
        MySqlCommand cmd = new MySqlCommand("insert into  Booking (yourName,YourEmail,YourPhone,Category,Date,Description) values(@Name,@Email,@Telephone,@Category,@Date,@Description)", connection);
        cmd.Parameters.AddWithValue("@Name", YourName.Text);
        cmd.Parameters.AddWithValue("@Email", YourEmail.Text);
        cmd.Parameters.AddWithValue("@Telephone", YourPhone.Text);
        cmd.Parameters.AddWithValue("@Category", Category.SelectedItem.Value);
        cmd.Parameters.AddWithValue("@Date", "test");
        cmd.Parameters.AddWithValue("@Description", Description.Text);
        cmd.ExecuteNonQuery();
    }
    else
    {
        DisplayMessage.Text = "Database connection failed.";
    }
}
catch (Exception ex)
{
    DisplayMessage.Text = "Error occured. Please try again later.";
}

connection.Close();

1 个答案:

答案 0 :(得分:0)

我们发现我们分配了错误的列名。在insert语句中,我们编写了yourName,yourEmail等(如下所示),需要将其更改为Name,Email等。

MySqlCommand cmd = new MySqlCommand("insert into  Booking (yourName,YourEmail,YourPhone,Category,Date,Description) values(@Name,@Email,@Telephone,@Category,@Date,@Description)", connection);

此外,我们删除了if循环,因为我们不需要它并添加了一个throw来获取更详细的反馈。希望这可以帮助任何陷入同样问题的人。

不要成为我们 - 检查您的专栏名称! ;)