Database Programming using MySql .Net Connector and C#

Oct 21st, 2015
DotaCN
Category:
Engineering
Price: $10 USD

Question description

1. Open Visual Studio, create a C# Windows Forms Application by clicking menu File-New…-Project. You can give a name to your project and click OK 

2. An empty form is generated in the new project. You can add all kinds of windows controls to the form by double-clicking or dragging controls from the toolbox to the form. Add controls to the form and make it like the one shown in the next page. Your controls should include TextBox, 

DropDownList, Labels, ListBoxes, DataGridView, and Buttons. In the properties window, name your TextBoxes as txtServer, txtUser, txtPort, and txtPassword. Name your ListBoxes as listDatabases and listTables. Type “*” in the PasswordChar property of the control txtPassword. It will hide the password you input into “*”. 

3. Next step is to add a reference to MySQL .Net connector. In the Solution Explorer, right click on the References and select Add References… 

In the Add Reference dialog, select MySql.Data. If you see there are two versions of MySql.Data in the list, the Runtime v2.x.xxx is for EARLY VERSION of Visual Studio and the Version 4.x is for integration with later versions after Visual Studio 2010. Choose the proper version of the 

Runtime based on the version of the Visual Studio you are using. 

Now it is time to add codes in the program. Add the following two lines of codes in the Form1.cs (Code) right before the declaration of the namespace: 

using MySql.Data; 

using MySql.Data.MySqlClient; 

namespace WindowsApplication1 

4. You can double click necessary controls in the Form (Design Window) to automatically activate an event function and then, add codes in the function. 

a. Double-click button “Connect”, add the following codes in the button-click function. 

private void button1_Click(object sender, EventArgs e) 

 { 

 MySqlConnection conn = new MySqlConnection(); 

 string conString = "server="+txtServer.Text 

 + ";user=" + txtUser.Text 

 + ";password=" + txtPassword.Text 

 + ";port=" + txtPort.Text; 

 conn.ConnectionString = conString; 

 try 

 { 

 conn.Open(); 

 MySqlCommand cmd = new MySqlCommand("show databases", conn); 

 MySqlDataReader rdr = cmd.ExecuteReader(); 

 listDatabases.Items.Clear(); 

 while (rdr.Read()) 

 { 

 listDatabases.Items.Add(rdr[0]); 

 } 

 listDatabases.SelectedIndex = 0; 

 rdr.Close(); 

 } 

 catch (Exception ex) 

 { 

 MessageBox.Show(ex.ToString()); 

 } 

 conn.Close(); 

 } 

b. Double-click Listbox, listDatabases, and add the following codes in 

the index-selection change function. 

 private void listDatabases_SelectedIndexChanged(object sender, EventArgs e) 

 { 

 if (listDatabases.SelectedItems.Count > 0) 

 { 

 MySqlConnection conn = new MySqlConnection(); 

 string conString = "server=" + txtServer.Text 

 + ";user=" + txtUser.Text 

 + ";password=" + txtPassword.Text 

 + ";database=" + 

listDatabases.Items[listDatabases.SelectedIndex].ToString() 

 + ";port=" + txtPort.Text; 

 conn.ConnectionString = conString; 

 try  5

 { 

 conn.Open(); 

 MySqlCommand cmd = new MySqlCommand("show tables", conn); 

 MySqlDataReader rdr = cmd.ExecuteReader(); 

 listTables.Items.Clear(); 

 while (rdr.Read()) 

 { 

 listTables.Items.Add(rdr[0]); 

 } 

 listTables.SelectedIndex = 0; 

 rdr.Close(); 

 } 

 catch (Exception ex) 

 { 

 MessageBox.Show(ex.ToString()); 

 } 

 conn.Close(); 

 } 

 } 

c. Double click Listbox, listTables, and add the following codes in the new function: 

 private void listTables_SelectedIndexChanged(object sender, EventArgs e) 

 { 

 if (listTables.SelectedItems.Count > 0) 

 { 

 MySqlConnection conn = new MySqlConnection(); 

 string conString = "server=" + txtServer.Text 

 + ";user=" + txtUser.Text 

 + ";password=" + txtPassword.Text 

 + ";database=" + 

listDatabases.Items[listDatabases.SelectedIndex].ToString() 

 + ";port=" + txtPort.Text; 

 conn.ConnectionString = conString; 

 try 

 { 

 MySqlCommand cmd = new MySqlCommand(); 

 cmd.CommandText = "Select * from " + 

listTables.Items[listTables.SelectedIndex].ToString(); 

 cmd.Connection = conn; 

 MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); 

 DataSet dbTableContent = new DataSet(); 

 adapter.SelectCommand = cmd; 

 adapter.Fill(dbTableContent, "tableContent"); 

 dataGridTableView.DataSource = dbTableContent; 

 dataGridTableView.DataMember = "tableContent"; 

 } 

 catch (Exception ex)  6

 { 

 MessageBox.Show(ex.ToString()); 

 } 

 } 

 } 

After completing coding, you can start to run the program. Press F5 to start debugging. 

Assignment:

Create a Windows Form based application using C#, which can finish the 

following functions: 

1. System Login: User can login to the MySql database based on the IP 

address, User Name, Password, and Port number specified through the 

interface of the application. 

2. SQL input: User can input a SQL statement in a textbox provided in the 

Application interface. 

3. Result display: The result of the SQL query can be display in the databinding control, such as a datagridview. 

4. Implement Insert, Update, and Delete function in your application, which can make changes to your database. 

The lab report to be turned in should contain: 

a. Graphic interface of the application. 

b. Source Code  (compressed into a zip file) 


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Duke University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

Summary
Quality
Communication
On Time
Value
kevin12622
Feb 15th, 2017
" Goes above and beyond expectations ! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1824 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors