Library tutorials & articles
Using MySQL with .NET
- Introduction
- 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();
}
Related articles
Related discussion
-
class file in C#
by glad024 (0 replies)
-
Storing images into MySQL using C#
by Onaopepo (1 replies)
-
Looking for .Net C# Software Developers
by sugagirisa (2 replies)
-
Lets Open our Eyes
by mawcot (0 replies)
-
LINQ in Action
by naser1 (0 replies)
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.
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
This thread is for discussions of Using MySQL with .NET.