Click to See Complete Forum and Search --> : c# connection to Mysql DB


jakobens
July 8th, 2006, 01:39 PM
hej hej!!!

i am totally confused in connecting to a MYSQL database using C#.Net.

i created a mysql db by using the command line..under C:/mysql/data/example1

now i open a new project in c# and want to connect to this db.

there are lots of staff around and i am totally confused..
some uses OLEDB some uses ODBC..what is the difference..
which one to use for MYSQL..

what should the connectionstring be??
Acc. to http://www.connectionstrings.com/ it should be sth like;
"Provider=MySQLProv;Data Source=mydb;User Id=UserName;Password=asdasd;"

this does not seem to work for me. it gives the error MySqlProv is not registered,(i installed Mic Data Access Components& ODBC .NET Data Provider & MYSQL Connector )

and another question: where do we state that our database is C:/mysql/data/example1...
is it ok if i just write Data Source=example1??

i am totally confused my friends..any help will be greatly apreciated..
and if possible can u show the code part that creates the string and the connection..

thnk you all..the code i tried is a simple form application :

...

using System.Data.OleDb;

...

private void button1_Click(object sender, System.EventArgs e)
{
System.Data.OleDb.OleDbConnection con;

string ConStr;

ConStr = "Provider=MySQLOLEDB;Data Source=example1";

con = new System.Data.OleDb.OleDbConnection(ConStr);

try
{
con.Open();
if (con.State==ConnectionState.Open) MessageBox.Show("Connection to MySQL opened through OLE DB Provider");
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}

Shuja Ali
July 9th, 2006, 05:15 AM
You should be using MySQL Connector (http://dev.mysql.com/downloads/connector/net/1.0.html) rather than OLEDB or ODBC.

MadHatter
July 9th, 2006, 10:22 AM
and then you need to specify

DataSource=yourservername;Database=yourdatabase;UserName=yourusername;Password=yourpassword;