简体   繁体   中英

How to use ip-address with username in connection string?

I wrote this method, but it throws System.ArgumentException if "uid" has ' symbols in.

        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
            }

Exception message on attempt to connect without '

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)

Change

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

to

var uid = @"a222068_6@10.0.2.13";

Use a verbatim string literal :

Verbatim string literals start with @ and are also enclosed in double quotation marks.

And there are several other StackOverflow answers with examples of how to use MySqlConnectionStringBuilder, such as 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM