通过PHPMyAdmin创建的数据库与C#建立MySQL连接

通过PHPMyAdmin创建的数据库与C#建立MySQL连接,c#,mysql,asp.net,C#,Mysql,Asp.net,我们遇到了一个关于MySQL数据库插入的问题。 我们已经安装了.NET连接器,并在/bin中添加了MySql.Data.dll、MySql.Web.dll、MySql.Entity.dll。正如下面的代码所示,我们也使用了正确的名称空间。 然而,当我们试图通过网站插入数据时,什么也插不进去。 webhotel提供了数据库连接的详细信息。 域位置: C#代码: 编辑: 首先,感谢所有的答案所以我们实现了Rahul和ActiveHigh的答案并更新了代码。此外,我们还添加了一种检查连接是否成功的方法

我们遇到了一个关于MySQL数据库插入的问题。 我们已经安装了.NET连接器,并在/bin中添加了MySql.Data.dll、MySql.Web.dll、MySql.Entity.dll。正如下面的代码所示,我们也使用了正确的名称空间。 然而,当我们试图通过网站插入数据时,什么也插不进去。 webhotel提供了数据库连接的详细信息。 域位置:

C#代码:

编辑: 首先,感谢所有的答案所以我们实现了Rahul和ActiveHigh的答案并更新了代码。此外,我们还添加了一种检查连接是否成功的方法。现在,当我们尝试插入数据时,会收到错误消息。测试位置仍然相同。 以下是数据库中表的图像: 有人知道出了什么问题或者知道如何调试吗

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();

您正在insert语句中使用数据库名称-

...

string server = "db.cce-solutions.dk";
    string database = "web626445";
    string uid = "******";
...

MySqlCommand cmd = new MySqlCommand("insert into  web626445 (yourName,YourEmail,YourPhone,Category,Description) values(@Name,@Email,@Telephone,@Category,@Description)", connection);
我看到
web626445
数据库
而不是
。改用表名。由于您已使用try块将其打包,因此无法看到错误


并检查是否正确创建了表。

删除行
connection.ConnectionString=ConnectionString内部
尝试
块。您正在重新分配连接字符串。如何确保数据不会被插入?你有什么例外吗?是否在DB中检查是否存在新数据?连接是否正确建立?已更新代码,但仍没有插入数据。我们通过phpMyadmin检查表是否为空。
...

string server = "db.cce-solutions.dk";
    string database = "web626445";
    string uid = "******";
...

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