如何在连接字符串中使用带有用户名的ip-address?

时间:2018-02-09 20:07:48

标签: c# mysql database connection database-connection

我编写了这个方法,但如果" uid"它会抛出System.ArgumentException。有'符号。

        public void Init(string constr)
            {
                var server = "a222068_6.mysql.mchost.ru";
                var database = "'a222068_6'";
                var uid = "'a222068_6'@'10.0.2.13'";
                var pass = "pass";

                constr = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + uid + ";PASSWORD=" + pass + ";SSL Mode=None;";
                cnt = new MySqlConnection(constr); // Exception is thrown here
            }

在没有'

的情况下尝试连接的异常消息
Authentication to host 'a222068_6.mysql.mchost.ru' for user 'a222068_6@10.0.2.13' using method 'mysql_native_password' failed with message: Access denied for user 'a222068_6@10.0.2.13'@'localhost' (using password: YES)

2 个答案:

答案 0 :(得分:0)

更改

var uid = "'a222068_6'@'10.0.2.13'";

var uid = @"a222068_6@10.0.2.13";

答案 1 :(得分:0)

使用verbatim string literal

  

逐字字符串文字以@开头,并且也用双引号括起来。

还有其他一些StackOverflow答案以及如何使用MySqlConnectionStringBuilder的示例,例如this one

var connectionStringBuilder = new MySqlConnectionStringBuilder
{
    Server = "<instanceIp>",
    UserID = "<userId>",
    Password = "<password>",
    Database = "<databaseName>",
    CertificateFile = @"<Path_To_The_File>\client.pfx",
    CertificatePassword = "<Password_For_The_Cert>"
};

var conn = new MySqlConnection(connectionStringBuilder.ToString())