R Pelzer
R Pelzer

Reputation: 1268

Can't connect asp.net page to MySQL database

I want to use a database on my asp.net website, but i have some problems with connecting to my host. I use the following code and i got an error:

private const string ConnStr = "Provider=MSDASQL; Driver={MySQL ODBC 5.1 Driver};Server=pdb9.xxx.net;Database=xxx;User=xxx;Password=xxx;Option=3;";    
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand("INSERT INTO sample(name, address) VALUES (?,?)", con))
{
    cmd.Parameters.Add("@name", OdbcType.VarChar, 255).Value = TextBox3.Text.Trim();
    cmd. Parameters.Add("@address", OdbcType.VarChar, 255).Value = TextBox4.Text.Trim();

    con.Open();
    cmd.ExecuteNonQuery();
    BindDataGrid();
}

The error appears when i call con.open();

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I always used the MySQL database on my PHP pages and it worked fine. Besides Mysql my host also supports PostgreSQL. Does someone know how i can solve this problem or how to find a solution using a database on an asp.net page?

Upvotes: 0

Views: 1178

Answers (2)

Uriil
Uriil

Reputation: 12618

Here is how commonly MySql connection string look like in config file:

<add name="name" connectionString="server=IP;user id=USERNAME;password=PASSWORD;database=DATABASE;persist security info=True" providerName="MySql.Data.MySqlClient"/>

in code:

vat connectionString = "server=IP;user id=USERNAME;password=PASSWORD;database=DATABASE;persist security info=True"

And i suggest you to use MySql Connector For .NET, you can install it using NuGET or from official site

Upvotes: 1

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15767

ConnectionString syntax is like

connectionString="DRIVER={MySQL ODBC 3.51 Driver};Database=YourDatabase;Server=localhost;UID=YourUsername;PWD=YourPassword;"

Upvotes: 0

Related Questions