【问题标题】:Problem connecting to Remote MySQL with C#使用 C# 连接到远程 MySQL 的问题
【发布时间】:2011-04-14 02:11:59
【问题描述】:

我使用的是 Visual Studio 2010,当我将 DataGridView 与远程 mysql 数据库绑定时,它工作正常
但是当我从向导中获取连接字符串并尝试将其与代码一起使用时,我得到:“provider: Named Pipes Provider, error: 40 - could not open a connection to SQL Server”

这是我尝试的连接字符串(我尝试了许多变体):
"Server=myserver.org;Database=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);
        数据集 myDataSet = new DataSet();
        数据行我的数据行;

        // 创建命令生成器。此行会自动为您生成更新命令,因此您无需
        // 必须提供或创建您自己的。
        SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

        // 将 MissingSchemaAction 属性设置为 AddWithKey 因为 Fill 不会导致主要
        // 要检索的密钥和唯一密钥信息,除非指定了 AddWithKey。
        mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        mySqlDataAdapter.Fill(myDataSet, "Table");

【问题讨论】:

  • 你能显示无效的代码吗?

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


【解决方案1】:

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

使用:MySqlConnection 代替

【讨论】:

    【解决方案2】:
               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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2017-11-27
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多