Category: Getting Started

GettingStartedDotNet

An Introduction to working with MySQL and .NET

By Anil Mahadev
MySQL User and Database Enthusiast

Introduction: As many of us have realized that working on a Robust Database Platform such as MySQL and Easy to Develop Platform such as .NET, Developers have embraced them with ease and great pleasure.

Join me on a tour of exploring the wonderful possibilities of using MySQL And .NET together.

To get started, one would need the following tools

Contents

[edit] System Requirements

[edit] Software Requirements

Database Software

Graphical User Interface tools to help MySQL Users

MySQL .NET and ODBC Connectors for connecting to .NET ODBC Driver

.NET Data Provider Driver

[edit] Building a Simple MySQL with .NET App using Visual Basic.NET

The C# version will be in the next WIKI update...

Now that we have the tools ready it's time to set the ball rolling.

Start a New Project in Visual Studio 2005 and choose VB as the language and Project Type as Windows Application. Give it a name say MySQLwithDotnet :)

Next right click on the Project and choose Add Reference. Under that choose browse and navigate to the folder where you have installed the MySQL Connector/NET. And under the bin folder choose the NET 2.0 version, click the MySQL.Data.dll assembly. Now your application can listen to MySQL.

Now under the Toolbox, right click and choose Add Item. Under the List choose DataGrid and click OK.

Now place the Datagrid onto the Form.Now outside the area of the Datagrid double click to launch the Code Window.

Now let's write some code

[edit] Creating our Database

In the MySQL Command Window, issue the following command

CREATE DATABASE mysqldotnetdemo;

and hit enter.

[edit] Creating our Guest Table

In the MySQL Command Window, issue the following command

CREATE TABLE guests
  (ID INT PRIMARY KEY,
  Name VARCHAR(20));

[edit] Adding some data

INSERT INTO guests(ID,Name) VALUES
('1','Anil'), ('2','Angelina'), ('3','Mark'), ('4','Maggy');

So now that we have our database infrastructure in place, let's view the above data in our DataGrid.

[edit] Coding the .NET Application

In the Code Window, at the top enter:

Imports MySQL.Data.MySqlClient

In the Form Load event issue the following code:

Imports MySql.Data.MySqlClient

Public Class Form1
Public Sub Conn()
Try

// create a new connection to mysql

Dim mysqlcon As New MySqlConnection("datasource=localhost;username=xyz;
password=abc;database=mysqldotnetdemo")

// open the connection
mysqlcon.Open()

// create a new mysql data adapter

Dim mydp As New MySqlDataAdapter("select * from guests", mysqlcon)

// create a dataset
Dim mysqlds As New DataSet

// fill the adapter
mydp.Fill(mysqlds, "guests")

// now populate the grid with data
DataGrid1.DataSource = mysqlds.Tables("guests")
DataGrid1.SetDataBinding(mysqlds, "guests")

// Now Catch the MySQLException
Catch ex As MySqlException
MsgBox("Error!!! " & ex.Message, MsgBoxStyle.Critical)

End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)   Handles MyBase.Load

Conn()

End Sub
End Class

Finally hit F5 or click on the Run icon to run the application.

Congratulations :) , you have just built your first .NET with MySQL Powered Application!

[edit] Stay Tuned for the C# Special :)

For any Feedback , send me your thoughts, suggestions to anilm001@gmail.com

Retrieved from "http://forge.mysql.com/wiki/GettingStartedDotNet"

This page has been accessed 5,137 times. This page was last modified 12:40, 2 October 2007.

Find

Browse
MySQLForge
Main Page
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Special pages
New pages
File list
Statistics
Bug reports
More...