Idea Experts
Idea Experts
Idea Experts
1
Querying your database
Select *
from customer;
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.
5
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.
7
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();