6

I am trying to connect to my remote MySQL server, using the following code. Can you tell me what I am doing wrong as the replacement of database connection info with variables doesn't seem to be working.

   using MySql.Data.MySqlClient;       

   string db_Server = "10.0.0.0";
   string db_Name   = "myDatabase";
   string db_User   = "myUser";
   string db_Pass   = "myPassword";


   // Connection String
   MySqlConnection myConnection = new MySqlConnection("server = {0}; database = {1}; uid = {2}; pwd = {3}", db_server, db_Name, db_User, db_Pass);

As a PHP developer, I prefer to use the code above instead of the deliberate casting below:

   MySqlConnection myConnection = new MySqlConnection("server=10.0.0.0; database=myDatabase; uid=myUser; pwd=myPassword");

But as you can see in this image, I am getting a lot of red squiggles: http://screencast.com/t/xlwoG9by

1
  • 4
    db_Server, db_Name, db_User, db_Pass not db_Server, db_User, db_Pass, db_Name Commented Dec 19, 2012 at 9:47

2 Answers 2

10

Order of your parameter is wrong, it should be:

db_server, db_Name, db_User, db_Pass

Currently it is:

"server = {0}; database = {1}; uid = {2};   pwd = {3}"
       db_Server         db_User   db_Pass   db_Name

So your statement should be:

MySqlConnection myConnection = new MySqlConnection(string.Format(
"server = {0}; database = {1}; uid = {2}; pwd = {3}", 
db_Server,db_Name, db_User, db_Pass));

EDIT: based on the comments and discussion, the error you are getting is that you are trying all the stuff at class level. You should have these lines inside a method and call that method where you need it. Something like:

class MyClass
{
    string db_Server = "10.0.0.0";
    string db_User = "myUser";
    string db_Pass = "myPassword";
    string db_Name = "myDatabase";


    public MySqlConnection GetConnection()
    {
        MySqlConnection myConnection = new MySqlConnection(string.Format(
                   "server = {0}; database = {1}; uid = {2}; pwd = {3}",
                    db_Server, db_Name, db_User, db_Pass));
        return myConnection;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

You may need a port too, after server.
Remember to include string.Format
@Jeagr, have you included String.Format ? you are missing that
@Jeagr, check the statement I have written in my answer, you are missing string.Format(.....), copy paste the code I have in my answer for myConnection and let me know if the error still stays
@Jeagr, you are formatting string using place holders, you have to use string.Format as well
|
1
 MySqlConnection myConnection = new MySqlConnection(string.Format("server = {0}; database = {1}; uid = {2}; pwd = {3}", db_Server, db_User, db_Pass, db_Name))

string.Format() is missing

2 Comments

With string.Format the other issue is wrong parameter, you need to adjust that
True. Missed that. But basically the red squiggles occur because of the missing string.Format() ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.