How to connect asp.net to mysql

PelicanDev
PelicanDev used Ask the Experts™
on
Hi,

I tried to connect to a MySql data base with asp.net. It work fine on my dev machine but when i publish my web site on the server i got this message... I installed the odbc 3.51 driver on the server.

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

[OdbcException (0x80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified]
   System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) +1156770
   System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle) +95
   System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions) +53
   System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +55
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +29
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +4863644
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
   System.Data.Odbc.OdbcConnection.Open() +40
   website_dealer.ViewSearchClient_Activate(Object sender, EventArgs e) +100
   System.Web.UI.WebControls.View.OnActivate(EventArgs e) +111
   System.Web.UI.WebControls.MultiView.set_ActiveViewIndex(Int32 value) +398
   System.Web.UI.WebControls.MultiView.OnInit(EventArgs e) +49
   System.Web.UI.Control.InitRecursive(Control namingContainer) +333
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Control.InitRecursive(Control namingContainer) +210
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +378

string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";
        using (OdbcConnection con = new OdbcConnection(ConnStr))
        {
            using (OdbcCommand cmd = new OdbcCommand("DELETE FROM succursales WHERE ID=?", con))
            {
                cmd.Parameters.AddWithValue("@customerId", customerId);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are you sure you configured the OCBC driver correctly on your server?

It's a two stage process:
install the driver, then add a user or system DSN "linked" to that driver. The User or system DSN will then have a "name" that you allocate to it, and it is this name that you can use in your code. Also make sure you configure the driver/dsn with your specific connection settings.

Author

Commented:
Yes, i have create a DSN Entry for  MySql connector and when i try the test connection button it connect succefully.

I added the DSN=MyConnector name in the connection string but no success...
string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};DSN=XXX;SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";

Open in new window

Try to use w/o DSN

string ConnStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=XXX;DATABASE=XXX;UID=XXX;PASSWORD=XXX;PORT=3306;OPTION=3;";

Bm Keshav
It doesn't work for me... I change everyting and start to use MySQL Connector/Net. You can find a great tutorial a this website.

http://www.connectionstrings.com/mysql#28
http://www.connectionstrings.com/Providers/mysql-connector-net-mysqlconnection
which version of MySql are you using ? I hope you are using 5.1 thats why mysql odbc 3.51 driver is not working, its work upto 5.0 ver.

Bm Keshav

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial