MySQL for the ASP.NET and SQL Server developer

For a recent project, I was asked to create a ASP.NET web page, but use MySQL instead of SQL Server.  I have been using SQL Server since version 7.0 and have never used MySQL.  Let me sum up my experience with one word....excellent!  I went into the process knowing nothing and within a half hour, I had a new database created and I was writing C#.  The following steps are what I did, mostly for my future reference, but for anyone who is in my boat.

1.  A google search provided me with a link to the MySQL download page.  The current version is 5.1 and I downloaded the Windows MSI Installer (x86):

http://dev.mysql.com/downloads/mysql/5.1.html#win32

for x64, go here: http://dev.mysql.com/downloads/mysql/5.1.html#winx64

2.  Download and start it up.  I accepted all the defaults.

3. Get the MySQL GUI Tools here:

http://dev.mysql.com/downloads/gui-tools/5.0.html

4. For .NET development, you need the Connector/Net 6.0 tools:

http://dev.mysql.com/downloads/connector/net/6.0.html

Once you have that installed, you can open up the MySQL Administrator tool and log in.  By default, the server host is "localhost", the username is "root", and the password is whatever you set it to be.

Now you can open up Visual Studio 2008.  In your project, you are going to need a reference to MySql.Data so you can access MySQL.   And you'll need a ConnectionString.

<connectionStrings>
     <
add name="MyConnection" connectionString="SERVER=localhost;DATABASE=MyDatabase;UID=root;PASSWORD=sa;"/>
</
connectionStrings>

Once you've done that, you can write some ADO.NET code, just like you'd do with SQL Server.  Here is some code to return a MySqlDataReader from a query to a MySQL database: 

MySqlConnection connection = new MySqlConnection(ConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from sometable where something= @Parameter";
command.Parameters.AddWithValue(
"@Parameter", somevalue);
connection.Open();
MySqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
     // Do something with the reader
}

connection.Close();

Happy programming!

A great reference I used to get started is heret http://www.globalnerdy.com/2009/05/04/installing-mysql-server-51-on-windows/

 

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading