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?
MySQL Server.NET Programming

Avatar of undefined
Last Comment
psk1
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Do you have any Imports for the MySql namespace?

Bob
Avatar of psk1
psk1

ASKER

No imports yet for the MySql namespace.  Would like to know how to declare that.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) Which MySQL .NET data provider are you talking about?

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

Bob

Avatar of psk1
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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Try this:

   Imports MySql.Data

Bob
Avatar of psk1
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
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
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
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
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
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.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo