The first part of this
article illustrated how to install and configure the MySQL Database Server,
install and use the MySQL Administrator, create a new database, and create and
populate a new sample table with some sample data.
The second part covered
creating a MySQL user account, granting that user access to the database,
and examined the basic steps required to connect to the database from an ASP.NET
web page.
When it was written, part two was designed to be the end of the article. However,
since then I've received a number of email messages requesting additional information.
Therefore this part will attempt to address the questions raised in those messages and share what
I've found while researching them.
Please note that the code listings below assume that your tables and users are set up as
outlined in the earlier parts of the article. The information in this part may be useful
even if you haven't read parts one and two, but if that's the case, it is highly unlikely
that the code will actually run without some minor tweaks. For example, I assume most people
who haven't followed along with the earlier parts won't have a database named "mydatabase"
and most certainly wouldn't have a user named "15secs". ;)
Namespace Not Found Errors
As I mentioned in part two, I ran into a problem where I was unable to get my ASP.NET pages to access
the MySQL Connector/Net namespace without placing a copy of the MySql.Data.dll file into my
application's /bin directory. Well, a reader was kind enough to send me the following email
explaining the situation and so I thought I should pass it along.
Hello Ziran,
I was reading [A Step-by-Step Guide To Using MySQL with ASP.NET], and in the second half of the tutorial
you are having a problem that Mysql.Data could not be found. The solution you give of putting the
.dll in the bin directory is just fine but there is a more 'official' manner for this.
You need to change the machine.config file of the server and add a reference to the assembly
in the GAC or, if you are using the latest .NET Framework, you can make this reference
in the web.config file. You can find it here:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
You need some information about the MySql.Data assembly in the GAC, which you can obtain
by typing the following at the SDK Command Prompt:
gacutil -l Mysql.Data
With this information you can put sometime like this in the web.config file:
Please tell me if I can do something for this great community. I like to be involved.
I am from The Netherlands, Europe.
Regards,
Rene Veenstra
I hope this helps explain things a bit and in response to Rene, you
already have done something for the community... keep up the good work!
A C# Version of the Sample ASP.NET 1.x Page to Query a MySQL Database
I didn't realize how many people were actually using C# instead of VB as their language of
choice for .NET development. One of the most popular requests I received was for a
C# version of the MySQL.aspx page that I published in part two. While C# is not my
development language of choice, I was able to get a C# version of the page working and
am including the listing for it here for your reference.
You'll have to pardon me if the style of the code isn't perfect, but like I said C# isn't
really my thing and getting it running was about the best I could do.
(The case sensitivity threw me off for a bit and those C# error messages aren't
really all that helpful.) I know it's not much,
but hopefully those of you who were looking for a C# version at least now have something
to start with.
Connecting to MySQL from ASP.NET 2.0
While you can still connect to MySQL from ASP.NET 2.0 using the same old ASP.NET 1.x style of
code (using the MySql.Data.MySqlClient) that sort of defeats the point of all the cool
.NET 2.0 stuff. Hopefully the official MySQL Connector/Net will support the new
fully declarative data binding model soon, but in the mean time you'll need to turn to a third party.
While there may well be others, one third-party connector that does support
the new provider model is CoreLab's
MySQLDirect .NET Data Provider.
They provide a fully-functional 30-day trial version so you can determine whether
their product is right for you.
Please note that there are a few different versions available for download.
Make sure that you get the one for .NET Framework 2.0 if you're going to be trying the
code that follows. The versions for .NET Framework 1.x are not the same thing.
You don't need to use the same version I used, but if for some reason you want to then look for
"MySQLDirect .NET version 3.50 beta for .NET Framework 2.0".
I didn't play with the Visual studio plug-ins or any of the bells and whistles, but
just did my best to get it working on it's own. The first step is obviously
to download and install the connector, which is extremely straight-forward.
Once that's done, the next step is to get the connector registered as a data provider.
To do this you need to add an entry to either your web server's machine.config or
your application's web.config. Here's a copy of the web.config file I used to get
my sample page running:
The connector also has a licensing requirement. How to generate a license is covered in the documentation,
but it is a little bit of an exercise so I'm going to outline the steps I used here.
Create a text file named licenses.licx.
Place the text CoreLab.MySql.MySqlConnection, CoreLab.MySql in the file.
Save the file and add it to a Visual Studio 2005 project.
Right click on licenses.licx in the VS's Solution Explorer and select "Build Runtime Licenses" from the context menu.
This will create a file named App_Licenses.dll in the solution's /bin folder.
Copy this file to the /bin folder of your application.
I wasn't actually using Visual Studio to write the code. The licensing step may be simpler if
you do, but I was trying to keep things as simple as possible for the sake of illustration.
Once the provider is registered and the licensing is taken care of, all that's left to do is
write the code to connect to our data. I'm going to use some very simple code from my earlier article
Introducing the ASP.NET 2.0 GridView Control
and simply modify it to connect to MySQL instead of Microsoft SQL Server.
The line in red is the one that tells the script to use CoreLab's provider. Without that
the script would attempt to connect via the default SQL Server provider which obviously
wouldn't work very well.
And here's a quick screen shot, just to show you that this actually does work:
Now I realize that the output isn't much to look at, but it does run and you automatically get
all the goodies that go along with the new ASP.NET 2.0 data-bound controls.
Conclusion
I hope this part of the article has helped address some of the questions and issues that readers have been
running into when trying to use ASP.NET with MySQL. Whether you were introduced to the combination by
the earlier parts or arrived here simply trying to find a solution to a problem you ran
into on your own, please let me know if you found the information useful and/or how it could be made more so.
You can reach me by using the site's feedback form and mentioning either my name, Ziran Sun, or the
title of the article.
While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
[Read This Article][Top]
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
[Read This Article][Top]
Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
[Read This Article][Top]
Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server. [Read This Article][Top]
In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance. [Read This Article][Top]
In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance. [Read This Article][Top]
In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
[Read This Article][Top]
In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications. [Read This Article][Top]
With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
[Read This Article][Top]
Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications. [Read This Article][Top]
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.