MySQL C # connection string

I have this error:

"ObdcException was not handled by user code.

enter image description here

I don't know why this is ...

This is the connection string:

<add name="MiniBoxConnection" connectionString="DRIVER={MySQL ODBC 5.1 Driver};Database=DATABASENAME;Server=SERVERNAME;UID=USER;PWD=PASS;"/>

      

how can i solve this problem?

I am developing in localhost but the database is online

No data source name was found and no default pointer was specified

+3


source to share


2 answers


You are trying to connect to your MySQL database from your .net code using ODBC. The error message states that you did not create an appropriate ODBC Data Source (DSN). You can do this using the ODBC Data Source Adminstrator Control Panel if you need.

If I were you, I would use Connector / NET instead of ODBC. It works better and it's not exactly a neck pain to set up properly.

You can download the installation kit for it here. http://dev.mysql.com/downloads/connector/net/



To do this, you need to change your code. But it's worth it! Jokes aside! Your code will look like this.

using System;
//etc etc
using MySql.Data.MySqlClient;
//etc etc

namespace myapp
{
    class Myclass
    {
        static void Mymethod(string[] args)
        {
            string connStr = "server=server;user=user;database=db;password=*****;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            string sql = "SELECT this FROM that";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            using (MySqlDataReader rdr = cmd.ExecuteReader()) {
                while (rdr.Read()) {
                    /* iterate once per row */
                }
            }
        }
    }
}

      

+8


source


Try to connect and test the connection:



MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "127.0.0.1";
conn_string.UserID = "sa";
conn_string.Password = "myPassword";
conn_string.Database = "myDatabase";


MySqlConnection MyCon = new MySqlConnection(conn_string.ToString());


try
{
    MyCon.Open();
    MessageBox.Show("Open");
    MyCon.Close();
    MessageBox.Show("Close");
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

      

+1


source







All Articles