Link to home
Start Free TrialLog in
Avatar of psk1
psk1

asked on

How to declare relevant classes and instantiate MySqlConnection object for connection to MySQL DB in ASP.NET

I'm just getting started with attempting to connect to a MySQL database from ASP.NET (VB, web app).  I need to know how to declare the relevant classes and initiate the connection.

The MySQL DB is on a different server on the same LAN and I'd like to use a named pipe to access it, though I would also accept code that will connect using TCP/IP to get started.

So, what do I declare at the top of the page, and what code do I then use to instantiate the MySqlConnection object?
Avatar of psk1
psk1

ASKER

The line:

Dim myConnection As New MySqlConnection(myConnectionString)

returns "Type 'MySqlConnection' is not defined"

so, I obviously need to declare something at the top of the page -- what and how?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you have any Imports for the MySql namespace?

Bob
Avatar of psk1

ASKER

No imports yet for the MySql namespace.  Would like to know how to declare that.
1) Which MySQL .NET data provider are you talking about?

2) My guess would be Imports System.Data.MySql

Bob

Avatar of psk1

ASKER

I don't have a System.Data namespace (?)

The data provider is Connector/Net

http://dev.mysql.com/doc/refman/5.0/en/connector-net.html
Try this:

   Imports MySql.Data

Bob
Avatar of psk1

ASKER

xDJR1975,

When attempting to implement your web.config suggestion, I get:

ASP.NET runtime error: Unrecognized configuration section system.web/assemblies. (web.config line 26)      
Avatar of psk1

ASKER

Correction:

When I implemented the web.config addition suggested above by xDJR1975 *correctly*, I got:

Could not load file or assembly 'MySql.Data, Version=1.0.5.13785, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.      
You will need to download the Connector/Net package to your local computer, find the MySql.Data.dll file for the version of .net running (1.1 in this case). Make sure you have a bin folder under wwwroot on the host server, and copy the MySql.Data.dll into it.
Avatar of psk1

ASKER

I've done that and now:

Could not load file or assembly 'MySql.Data, Version=1.0.5.13785, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)      

So I assume this is an issue with the version or public key token defined above not being exact for the particular DLL (which is for .NET 2.0)
Avatar of psk1

ASKER

I'm using Visual Web Developer Express.  Not Visual Studio.
Get the version that is for .net 2.0 and try it again.
Avatar of psk1

ASKER

I changed the version to match the version number found in .NET's machine.config (Version=5.0.3.0) and

Imports MySql.Data

now works.

However, MySqlConnection was still undefined.  I added

Imports MySql.Data.MySqlClient

and all definition problems related to MySQL disappeared.  THANKS!
Avatar of psk1

ASKER

Just to check, I removed the DLL from the BIN folder and it still works -- appears that the installation of the driver package plus the related line in web.config was what was needed.