使用C#连接到远程MySQL的问题

时间:2010-09-18 12:33:23

标签: c# mysql visual-studio connection-string

我正在使用Visual Studio 2010,当我将DataGridView与我的远程mysql数据库绑定时,正常运行
但是当我从向导中获取连接字符串并尝试将其与代码一起使用时,我得到:“提供者:命名管道提供程序,错误:40 - 无法打开与SQL Server的连接”

这是我尝试的连接字符串(我试过很多变种):
“服务器= myserver.org;数据库= MY_DB; UID =为myuser; PWD = MYPWD;”

任何想法?
感谢

这是代码:
string connectionString = "Server = sql.server.org; Database = my_db; Uid = my_user; Pwd = mypwd;";

        SqlConnection myConnection = new SqlConnection(connectionString);
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Table", myConnection);
        DataSet myDataSet = new DataSet();
        DataRow myDataRow;

        // Create command builder. This line automatically generates the update commands for you, so you don't 
        // have to provide or create your own.
        SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

        // Set the MissingSchemaAction property to AddWithKey because Fill will not cause primary
        // key & unique key information to be retrieved unless AddWithKey is specified.
        mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        mySqlDataAdapter.Fill(myDataSet, "Table");

2 个答案:

答案 0 :(得分:0)

已解决:显然 SqlConnection 不适用于MySQL

使用: MySqlConnection 而不是

答案 1 :(得分:0)

           string connectionString = "server=localhost;uid=root;pwd=***********;database=terra_incognita_online";
           MySqlConnection sqlConnection = new MySqlConnection();
           sqlConnection.ConnectionString = connectionString;
           sqlConnection.Open();
           Console.WriteLine("open");
           sqlConnection.Close();
           Console.WriteLine("close");