C # Fourth job: MySQL database and C # operate MySQL database,
1. Basic MySQL database operations include the following two tasks:
(1) Insert data from two files into the MySQL database
(Note: when creating a database, select gb2312 -- GB2312 Simplified Chinese. Otherwise, it cannot be imported because it cannot be recognized)
Import list_class1.txt to mysql:
1. Add attributes to the data in the txt document
2. TXT file before the default encoding is ANSI, save as encoded as UTF-8 text file, so that the import of mysql will not garbled
3. Start Import
4. Import successful
Import list_class1_submit.xls to mysql:
The process is similar to importing a. txt file into mysql.
(2) Use the SQL command to find all the student lists that appear in "List_Class1" but not "List_Class1_Submit", and save or output ().
1. Set the encoding so that the database and cmd code are consistent. Otherwise, Chinese characters are garbled:
Set character_set_client = gbk;
Set character_set_results = gbk;
2. Select a database
Use cshape;
3. Enter the query statement:
Select list_class1. name from list_class1 where list_class1. Name not in (select name from list_class1_submit );
4. query results
2. C # operate databases
(1) connect to the related table in the database created in Job 1 and display it in the DataGridView control.
Note: Reference MySql. Data. dll in C #
Code:
[Csharp]View plaincopy
- Using System;
- Using System. Collections. Generic;
- Using System. ComponentModel;
- Using System. Data;
- Using System. Drawing;
- Using System. Linq;
- Using System. Text;
- Using System. Windows. Forms;
- Using MySql. Data. MySqlClient;
- Using System. Data. SqlClient;
- Namespace mysqltest1
- {
- Public partial class Form1: Form
- {
- Public Form1 ()
- {
- InitializeComponent ();
- }
- Private void button#click (object sender, EventArgs e)
- {
- String strConn = "server = localhost; User Id = root; password =; Database = cshape; Charset = utf8 ";
- MySqlConnection conn = new MySqlConnection (strConn );
- Conn. Open (); // link to the database
- DataSet ds = new DataSet ();
- String SQL = "select * from list_class1_submit ";
- MySqlDataAdapter da = new MySqlDataAdapter (SQL, conn );
- Da. Fill (ds, "table ");
- DataGridView1.DataSource = ds. Tables ["table"];
- }
- }
- }
Running result:
Press the button key to connect to the list_class1_submit table in the mysql database cshape written in the code, which is displayed in the dataGridView
(2) C # programming language inserts, deletes, and modifies one or more records of a database.
Code:
[Csharp]View plaincopy
- Using System;
- Using System. Collections;
- Using System. Configuration;
- Using MySql. Data;
- Using MySql. Data. MySqlClient;
- Using System. Data;
- Namespace mysqlconsletest
- {
- Class Program
- {
- Static void Main (string [] args)
- {
- String constr = "server = localhost; User Id = root; password =; Database = cshape; Charset = utf8 ";
- MySqlConnection mycon = new MySqlConnection (constr );
- Mycon. Open ();
- MySqlCommand mycmd1 = new MySqlCommand ("insert into list_class1_submit (student ID, name, group, job URL) values ('20140901', 'liang Jiahui ', '1', 'HTTP: // blog.csdn.net/u010580422') ", mycon );
- MySqlCommand mycmd2 = new MySqlCommand ("update list_class1_submit set student ID = '000000' where student ID = '000000'", mycon );
- MySqlCommand mycmd3 = new MySqlCommand ("delete from list_class1_submit where student ID = '20140901'", mycon );
- If (mydomain1.executenonquery ()> 0)
- {
- Console. WriteLine ("data inserted successfully! ");
- }
- If (mydomain2.executenonquery ()> 0)
- {
- Console. WriteLine ("data modified successfully! ");
- }
- If (mydomain3.executenonquery ()> 0)
- {
- Console. WriteLine ("data deleted successfully! ");
- }
- Console. ReadLine ();
- Mycon. Close ();
- }
- }
- }
Running result:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.