gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:12:59 25 4
gpt4 key购买 nike

<分区>

我们遇到了一个关于 MySQL 数据库插入的问题。我们已经安装了 .NET 连接器并将 MySql.Data.dll、MySql.Web.dll、MySql.Entity.dll 添加到/bin。如下面的代码所示,我们也使用了正确的命名空间。但是,当我们尝试通过网站插入数据时,没有插入任何内容。webhotel 提供了数据库连接的详细信息。域名位置:http://testing.cce-solutions.dk/testBooking/is/interested/

C#代码:

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.ConnectionString = connectionString;
connection.Open();
MySqlCommand cmd = new MySqlCommand("insert into web626445 (yourName,YourEmail,YourPhone,Category,Description) values(@Name,@Email,@Telephone,@Category,@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("@Description", Description.Text);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
DisplayMessage.Text = "Error occured. Please try again later.";
}
connection.Close();
}}

编辑:首先,感谢所有的回答!所以我们已经实现了 Rahul 和 ActiveHigh 的回答并更新了代码。此外,我们还添加了一种检查连接是否成功的方法。现在,当我们尝试插入数据时,我们会收到错误消息。考试地点还是一样。这是数据库中表的图像:https://www.dropbox.com/s/g2c70ty9qb1h7bw/ScreenshotDatabase.png任何人都知道出了什么问题或知道如何调试它?

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com