Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Connection String C#

Tags:

c#

mysql

I've this error:

"ObdcException was unhandled by user code"

enter image description here

I dont know why this...

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'm developing in the localhost, but database is online

The name of the data source was not found and there was no default driver specified

like image 345
Severiano Avatar asked Sep 03 '14 18:09

Severiano


People also ask

Can we connect MySQL with C?

MySQL database is available on most important OS platforms. It runs on BSD Unix, Linux, Windows, or Mac OS. To be able to compile C examples, we need to install the MySQL C development libraries. The above line shows how we can do it on Debian based Linux.

What is C in MySQL?

The C API provides low-level access to the MySQL client/server protocol and enables C programs to access database contents. The C API code is distributed with MySQL and implemented in the libmysqlclient library.

Can C# link with MySQL?

Connect C# to MySQLAll the communication between a C# application and the MySQL server is routed through a MySqlConnection Object. So, before your application can communicate with the server, it must instantiate, configure, and open a MySqlConnection object.


2 Answers

You are attempting to connect to your MySQL database from your .net code using ODBC. Your error message is telling you that you haven't created an appropriately named ODBC data source object (DSN). You can do that with the ODBC Data Source Adminstrator control panel if you need to.

If I were you I would use Connector/NET instead of ODBC. It performs better and it isn't quite such a pain in the neck to configure correctly.

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

You'll need to change your code for this. But, it's worth it! Seriously! Your code will end up looking 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 */
                }
            }
        }
    }
}
like image 82
O. Jones Avatar answered Oct 05 '22 06:10

O. Jones


Try this to connect and test your 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);
}
like image 44
daniele3004 Avatar answered Oct 05 '22 06:10

daniele3004