【问题标题】:azure connection string c#天蓝色连接字符串c#
【发布时间】:2017-07-20 12:48:58
【问题描述】:

我正在尝试打开与 Azure SQL 数据库的连接。尝试创建通常的connection = new MySqlConnection("Server=" + server + "Database=" + database + "Uid=" + uid + "Password=" + password);,每个字符串变量都以 ; 结尾但是即使数据正确,它也总是无法连接。尝试对 ADO.NET 使用给定的字符串,但随后出现异常“不支持关键字”。我实际上并没有什么可做的......尽可能多地用谷歌搜索,但所有解决方案都完全相同,但对我来说没有任何效果:/

【问题讨论】:

  • 好吧,azure 不是 mysql。去图吧。
  • 你在 Azure 中运行什么样的数据库?你确定是mysql?
  • 哦,对不起..我对数据库很陌生

标签: c# mysql azure


【解决方案1】:

首先,Azure 数据库不使用 mysql。所以使用MySqlConnection() 是行不通的。

改为使用

SqlConnection connection = new SqlConnection(connectionstring);

标准连接字符串应采用以下格式

Server=tcp:[serverName].database.windows.net;Database=myDataBase;
User ID=[LoginForDb]@[serverName];Password=myPassword;Trusted_Connection=False;
Encrypt=True;

更多选项请见https://www.connectionstrings.com/azure-sql-database/

【讨论】:

    【解决方案2】:
    var connectionString = @"Server=tcp:<dbname>.database.windows.net,1433;Initial Catalog=<databasename>;Persist Security Info=False;User ID=<userid>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
    

    这就是我连接到 Azure SQL 数据库的方式,对我有用。

    【讨论】:

      【解决方案3】:

      对于 MySQL In App 可以使用 c# 中的同修代码获取连接字符串:

          static string getConnectionString()
          {
          #if DEBUG
              string connectionString = "dbname=localdb;host=localhost:3306;user=root;pass=password;";
          #else
              string connectionString = Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb");
          #endif
              string[] options = connectionString.Split(";");
              string database = options[0].Split("=")[1]; ;
              string serverport = options[1].Split("=")[1];
              string server = serverport.Split(":")[0];
              string port = serverport.Split(":")[1];
              string user = options[2].Split("=")[1];
              string password = options[3].Split("=")[1]; ;
      
              connectionString = $"server={server};port={port};database={database};user={user};password={password};";
      
              return connectionString;
          }
      

      【讨论】:

        【解决方案4】:

        标准的 .Net Framework 提供程序格式为:

        Server=[serverName].database.windows.net;Database=myDataBase;
        User ID=[LoginForDb]@[serverName];Password=myPassword;Trusted_Connection=False;
        Encrypt=True;
        

        Azure SQL 数据库是 SQL Server 类型的数据库,而不是 MySQL!

        【讨论】:

          【解决方案5】:

          您是否按照 Microsoft 的说明进行操作?

          https://docs.microsoft.com/en-us/azure/sql-database/sql-database-connect-query-dotnet-visual-studio

          您也必须在 azure 上创建服务器级防火墙规则才能连接。

          【讨论】:

            猜你喜欢
            • 2019-02-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-16
            • 1970-01-01
            • 1970-01-01
            • 2019-02-02
            • 1970-01-01
            相关资源
            最近更新 更多