简体   繁体   English

MySQL与phpMyAdmin的连接,使用C#创建数据库

[英]MySQL connection to phpMyAdmin created database with C#

As our question got closed yesterday, we unfortunately have to open a new one. 昨天我们的问题解决了,很遗憾,我们不得不重新开始一个问题。 So here goes. 所以去。 This is the old question: MySQL connection with C# through PHPMyAdmin created database 这是一个老问题: 通过PHPMyAdmin创建的数据库与MySQL进行C#连接

Firstly, thanks for all the answers! 首先,感谢您的所有回答! So we have implemented Rahul and ActiveHigh's answers and updated the code. 因此,我们已经实现了Rahul和ActiveHigh的答案并更新了代码。 Furthermore, we have added a way to check if the connection is a success or not. 此外,我们添加了一种检查连接是否成功的方法。 Now when we try to insert data we get the error message from the catch. 现在,当我们尝试插入数据时,会从捕获中获得错误消息。 The test location is still the same. 测试位置仍然相同。 Here is an image of the table in the database: https://www.dropbox.com/s/g2c70ty9qb1h7bw/ScreenshotDatabase.png 这是数据库中表的图像: https : //www.dropbox.com/s/g2c70ty9qb1h7bw/ScreenshotDatabase.png

Anyone have any idea what is going wrong or an idea how to debug it? 任何人都知道哪里出了问题或如何调试它? (We have checked inside phpmyadmin whether or not the table is empty with a SQL query. It is empty. (我们已经在phpmyadmin内部检查了表是否为空,并通过SQL查询。它为空。

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

We found out we had assigned erroneous column names. 我们发现分配了错误的列名。 In the insert statement we wrote yourName, yourEmail and so on (as you can see below) which needed to be changed to Name, Email, and so on. 在插入语句中,我们编写了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);

Furthermore we removed the if loop since we did not need it and added a throw to the catch to get more detailed feedback. 此外,由于不需要循环,我们删除了if循环,并向捕获中添加了一个throw,以获得更详细的反馈。 Hope this can help anyone stuck in the same problem. 希望这可以帮助陷入同样问题的任何人。

Don't be us - check your column names! 不要成为我们-检查您的列名! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM