【发布时间】:2018-03-09 20:59:41
【问题描述】:
我正在尝试连接到我的本地 MySQL 数据库,但我不确定所有参数是什么。希望有人可以提供帮助。
我的代码:
string connetionString = "Server = 127.0.0.1; Database = name of database; User Id = root, Password = password of database";
SqlConnection cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "titel", MessageBoxButtons.OK);
MessageBox.Show("Can not open connection ! ");
}
谢谢
【问题讨论】:
-
你得到什么错误信息?
-
Google 搜索需要 12 秒,创建 SO 问题需要 4 分钟。前者只会浪费你的时间,后者会浪费很多人的时间。 dotnetperls.com/sqlconnection
-
您使用了错误的连接字符串和错误的实例。您应该使用
MySql.Data.MySqlConnection和链接上给出的连接字符串示例。 -
如果您使用的是 MySQL,那么您必须使用
MySqlConnection和MySqlCommand类 - 不能SqlConnection和SqlCommand- 这些是仅限 SQL Server -
@NielsSchutte 正如@marc_s & 我之前提到的,我建议您区分 SQL Server 和 MySQL 连接字符串,不要将它们混合使用。 MySQL 使用
MySql.Data.MySqlClient命名空间,而 SQL Server 使用System.Data.SqlClient命名空间,它们不同。
标签: c# mysql connection