Library tutorials & articles

Using MySQL with .NET

Page 2 of 2
  1. Introduction
  2. Connecting to the Database

Connecting to the Database

Now that the database is created we can move on to creating the application to view it. The first step is to create a new C# Windows Application in Visual Studio. On the main form, place a datagrid control in the middle and call it mysqltable. Next we will want to fill this datagrid with information from our table. To accomplish this we will need to use ADO at load time. Double click on the main form to bring up the code view for the Form1_Load function. The code for the load function should look as follows:

private void Form1_Load(object sender, System.EventArgs e)
{
    //create a connection string, contains the driver, the servername and the database name
    String ConnectionString = “Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mysqlsample;option=3”;
    //create a query string
    String myquery = “SELECT * FROM tutorials”;
    //run the connection string and the query and store it in the dataadapter
    OdbcDataAdapter MySQLDataAdapter = new OdbcDataAdapter(myquery,ConnectionString);
    //create a new dataset
    DataSet MySQLDataSet = new DataSet();
    //fill the dataset with data from the dataadapter
    MySQLDataAdapter.Fill(MySQLDataSet,”tutorials”);
    //now set the datasource of our grid to that of the main table of the dataset
    mysqltable.DataSource = MySQLDataSet.Tables[”tutorials”].DefaultView;
}

This code connects to the database using the OdbcDataAdapter and performs our query. We then store the results in a dataset which can be used as the datasource for our grid. Next we add a quit button somewhere on the form to allow the application to exit. The code for quitting is simply:

private void button1_Click(object sender, System.EventArgs e)
{
    //quit the application
    Application.Exit();
}

Comments

  1. 13 Aug 2006 at 00:18
    HI a better way to Connect to a Mysql Database ould be using the Mysql Provided Connector. provides faster access provides suport for almost all mysql Functionality. you can check out the Following Link.

    http://dev.mysql.com/downloads/connector/net/5.0.html you get the connector along with a basic documnetion that will help you with Connecting to the Mysql database.
    being a Mysql fan will also tell people who do not want to have transaction support that mysql with a myisam type table is blazingly fast.

    regards

    David Xavier
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Using MySQL with .NET.

Leave a comment

Sign in or Join us (it's free).

 cardinals33
AddThis

Related discussion

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Events coming up

  • Nov 8

    The Los Angeles PHP Developers November Meetup

    Santa Monica, United States

    As usual, our monthly schmooze meeting. This is an informal networking and discussion get together. It's a meetup mash-up of the Los Angeles PHP / MySQL / Semantic Web meetup groups. A great group of PHP developers and enthusiasts discussing various issues on PHP and software development as well as latest trends in web-related technologies.

We'd love to hear what you think! Submit ideas or give us feedback