通过phpmyadmin用C#MySQL连接创建的数据库 [英] MySQL connection with C# through PHPMyAdmin created database
问题描述
我们正坐在有关MySQL数据库插入一个问题。
我们已经安装了.NET连接器和附加MySql.Data.dll,MySql.Web.dll,MySql.Entity.dll到/箱。作为显示在下面的code,我们还使用了正确的命名空间。
然而,当我们通过网站尝试插入数据,没有被插入。
数据库连接的细节是从webhotel提供。
域位置: http://testing.cce-solutions.dk/testBooking/is/interested /
C#code:
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
MySql.Data.MySqlClient.MySqlConnection连接;
字符串服务器=db.cce-solutions.dk;
字符串数据库=web626445;
字符串的uid =******;
字符串密码=******;
字符串的connectionString;
的connectionString =服务器=+服务器+; +DATABASE =+
数据库+; +UID =+ UID +; +PASSWORD =+密码+;;
连接=新的MySqlConnection(的connectionString);
尝试
{
connection.ConnectionString =的connectionString;
connection.Open();
MySqlCommand的CMD =新的MySqlCommand(插入web626445(YOURNAME,YourEmail,YourPhone,类别,描述)值(@姓名,电子邮件@,@电话,@类,@说明),连接);
cmd.Parameters.AddWithValue(@名,YourName.Text);
cmd.Parameters.AddWithValue(@电子邮件,YourEmail.Text);
cmd.Parameters.AddWithValue(@电话,YourPhone.Text);
cmd.Parameters.AddWithValue(@范畴,Category.SelectedItem.Value);
cmd.Parameters.AddWithValue(@描述,Description.Text);
cmd.ExecuteNonQuery();
}
赶上(异常前)
{
DisplayMessage.Text =发生错误,请稍后再试。
}
connection.close()时;
}}
编辑:
首先,感谢所有的答案!的因此,我们已经实现了拉胡尔和ActiveHigh的答案,更新了code。此外,我们还增加了一个方法来检查连接是否成功与否。现在,当我们试图插入数据,我们得到的错误信息。试验位置仍然是相同的。
这里是在数据库表中的一个图像: https://www.dropbox.com/ S / g2c70ty9qb1h7bw / ScreenshotDatabase.png
任何人有任何想法什么错误或一个想法如何调试呢?
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
MySql.Data.MySqlClient.MySqlConnection连接;
字符串服务器=db.cce-solutions.dk;
字符串数据库=web626445;
字符串的uid =******;
字符串密码=******;
字符串的connectionString;
的connectionString =服务器=+服务器+; +DATABASE =+
数据库+; +UID =+ UID +; +PASSWORD =+密码+;;
连接=新的MySqlConnection(的connectionString); 尝试
{
connection.Open();
如果(connection.State == ConnectionState.Open)
{
DisplayMessage.Text =数据成功地输入。
MySqlCommand的CMD =新的MySqlCommand(插入预定(YOURNAME,YourEmail,YourPhone,类别,日期,描述)值(@姓名,电子邮件@,@电话,@类,@日,@说明),连接);
cmd.Parameters.AddWithValue(@名,YourName.Text);
cmd.Parameters.AddWithValue(@电子邮件,YourEmail.Text);
cmd.Parameters.AddWithValue(@电话,YourPhone.Text);
cmd.Parameters.AddWithValue(@范畴,Category.SelectedItem.Value);
cmd.Parameters.AddWithValue(@日,测试);
cmd.Parameters.AddWithValue(@描述,Description.Text);
cmd.ExecuteNonQuery();
}
其他
{
DisplayMessage.Text =数据库连接失败。
}
}
赶上(异常前)
{
DisplayMessage.Text =发生错误,请稍后再试。
} connection.close()时;
您使用的是插入陈述书内的数据库名称
...字符串服务器=db.cce-solutions.dk;
字符串数据库=web626445;
字符串的uid =******;
...MySqlCommand的CMD =新的MySqlCommand(插入web626445(YOURNAME,YourEmail,YourPhone,类别,描述)值(@姓名,电子邮件@,@电话,@类,@说明),连接);
我看到 web626445
是数据库
不是表
。使用表名来代替。既然你已经有一个try块包好了,你看不到的错误。
和检查是否有表正确创建。
We are sitting with a problem regarding MySQL database insertion. We have installed .NET connector and added MySql.Data.dll, MySql.Web.dll, MySql.Entity.dll to the /bin. As displayed in the code below, we are also using the right namespace. However, when we try and insert data through the website, nothing gets inserted. The details for the database connection is provided from the webhotel. Domain location: http://testing.cce-solutions.dk/testBooking/is/interested/
C# code:
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();
}}
EDIT: 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. 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?
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();
You are using database name inside insert statement-
...
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);
I see that web626445
is the database
not table
. Use table name instead. Since you have wrapped it up with a try block you cannot see the error.
And check that you have the table correctly created.
这篇关于通过phpmyadmin用C#MySQL连接创建的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!