简体   繁体   中英

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

Firstly, thanks for all the answers! So we have implemented Rahul and ActiveHigh's answers and updated the code. 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

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.

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.

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. Hope this can help anyone stuck in the same problem.

Don't be us - check your column names! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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