How To Use MySql With C
How To Use MySql With C
Ross Sherman
1
Using MySql
MySql is free. You could download it from:
http://dev.mysql.com/downloads/
When the setup wizard completes check Launch the MySQL Instance
Configuration Wizard and click Finish
Choose Development Machine and leave the defaults as they are
Choose a password
Add at least one user account
Choose defaults for the rest of the screens
money decimal
create table movies
(id int(4), nchar string
moviename varchar(20),
numberavailable int (3)); nvarchar string
Inserting values into your database’s tables
INSERT INTO customer
VALUES ('1212','Stan','Gundy','[email protected]','coach'),
('1132','Phil','Jackson','[email protected]','eleven');
select max(id)
from customer;
select id
from movies
where id = 1212;
select *
from customer
where firstname like ‘P%’;
In the Solution Explorer, click on the name of your presentation, right click on
references, click on add reference.
9
After clicking on add reference you will be taken to the reference manager form.
In Reference Manager form, select Extensions, find MySql.Data and press the check
box next to MySq.Data, them click on ok.
At this point, you have created a database
and you have connected that database to
MySql.
11
At the top, include the line: using MySql.Data.MySqlClient;
In the button’s code include the following lines:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionInfo = "datasource = localhost; port = 3306; username = root; password = csi";
MySqlConnection connect = new MySqlConnection(connectionInfo);
connect.Open();
MessageBox.Show("Connected");
connect.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
To find out what to put into:
string myConnection = "datasource = localhost; port = 3306; username = root; password = csi";
go to the MySql workbench and press ctrl+u.
The following screen should appear:
The password is the password you created. If you are using the school’s computer the password is csi.
How to use MySql to create a login.
if (count == 1)
{
MessageBox.Show("Firstname and password is correct");
}
else if (count > 1)
{
MessageBox.Show("Duplicate Firstname and password. You are not allowed access");
}
else
MessageBox.Show("Firstname and password is not correct");
connect.Close();
} // end of try block
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} // end of login button code
Adding additional forms to your Visual Studio project
First, right click on presentation. Then, go down to add and click on new item. Select windows form
and click add.
The code that makes clicking on the login button take
you to the second form.
Go back to the code for the login button in slide 17. Inside of if (count == 1) add the 3 lines of code
that are in purple:
if (count == 1)
{
MessageBox.Show("Firstname and password is correct");
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
Querying and inserting values into a database
//create an object of MySqlConnection with the string containing datasource, etc as a parameter
string connectionInfo = "datasource = localhost; port = 3306; username = root; password = pawsox11";
MySqlConnection connect = new MySqlConnection(connectionInfo);
//create an object of MySqlCommand with the SQL Query as the first parameter and the object of MySqlConnection
//as the second parameter
MySqlCommand SQLCommand1 = new MySqlCommand("select * from videostore.customer;”,connect);
try
{
//open the connection using the MySqlConnection object
connect.Open();
//use a DataReader to process each record
MySqlDataReader reader;
reader = SQLCommand1.ExecuteReader();
while (reader.Read()) { //do something with each record }
}
catch (Exception er) {//do something with the exception}
//always close the connection
connect.Close();
References
From the Stanford Online Database Course:
Introduction to SQL:
http://www.youtube.com/watch?feature=player_embedded&v=wxFmiRwXcQY
basic-select-statement:
http://www.youtube.com/watch?feature=player_embedded&v=4IxirOdp6bw
table-variables-set-operators:
http://www.youtube.com/watch?v=-BCCy5Z6i-s&feature=player_embedded
subqueries-in-where:
http://www.youtube.com/watch?v=IJPXosPGLTU&feature=player_embedded
subqueries-in-from-select:
http://www.youtube.com/watch?v=8OCAxk1Rybg&feature=player_embedded