-2

I'm trying to write data access code to connect MySQL to visual studio. I have this code so far, but I do not know if it's correct. I'm using my books and have commented out a few other things, but I received errors when I included the commented out information.

public static MySqlConnection getconnection()

{
string connectionstring = "serve=r127.0.0.1; uid = root;" + "pwd = databasses; database = group1;"

MySqlConnection connection = new MySqlConnection(connectionstring);
return connection;
}

Commented out information:

//string connectionstring = "Data Source=localhost\\MySql;Initial Catalog = group1; Integrated Security=True";

and

//conection.Open();

Is there a way for me to check if my connection is made? I am able to run it and display a blank form, but I'm worried it's wrong.

ITNovice15
  • 29
  • 3
  • 9
  • your connection string looks wrong since there is no `=` after server. – Daniel A. White Apr 23 '15 at 18:05
  • check out this site it has working examples on how to connect to many different databases in regards to how to configure your connection string I would store the connection string in the web / app config file [C# ConnectionStrings](http://www.connectionstrings.com) – MethodMan Apr 23 '15 at 18:06
  • I forgot that! I do have one now. – ITNovice15 Apr 23 '15 at 18:07

1 Answers1

2

From the MySQL website here is their example code on how to use C# to connect to a MySQL database:

MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;

myConnectionString = "server=127.0.0.1;uid=root;" +
    "pwd=12345;database=test;";

try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}
Pseudonym
  • 2,052
  • 2
  • 17
  • 38