I use MYSQL, so first import MySql. Data. dll and MySQLDriverCS. dll to avoid the unavailability of some functions. Namespace: usingSystem; usingSystem. Collections. Generic; usingSystem. ComponentModel; usingSystem. Data; usingSystem. Drawing; usingSystem. Linq
I use MYSQL, so first import MySql. Data. dll and MySQLDriverCS. dll to avoid the unavailability of some functions. Namespace: using System; using System. Collections. Generic; using System. ComponentModel; using System. Data; using System. Drawing; using System. Linq
I use MYSQL, so first import MySql. Data. dll and MySQLDriverCS. dll to avoid the unavailability of some functions.
Namespace:
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 MySQLDriverCS;
Winform uses the dataGridView, which involves the following steps:
(1) database data
MySqlConnection conn = new MySqlConnection ("server =" + IPAdr + "; uid = root; pwd = 111; database = ser; charset = gb2312 ;");
Conn. Open ();
String str = "insert into ped values (NULL, '" + DateTime. now. toString ("yyyy-MM-dd HH: mm: ss") + "','" + dataInfo + "')"; // The primary ID with the ''ID
MySqlCommand comm = new MySqlCommand (str, conn );
Comm. ExecuteNonQuery ();
Comm. Dispose ();
Conn. Close ();
Note: When creating a database, the time format is timestamp, and you need to set the refresh current timestamp; ID is auto-incrementing and the primary key;
(2) database connection and display:
MySqlConnection conn = new MySqlConnection ("server = 127.0.0.1; uid = root; pwd = 111; database = ser; charset = gb2312 ;");
MySqlCommand comm;
Conn. Open ();
String str = "select * from ped ";
MySqlDataAdapter da = new MySqlDataAdapter (); // instantiate sqldataadpter
MySqlCommand cmd1 = new MySqlCommand (str, conn); // SQL statement
Da. SelectCommand = cmd1; // set it to the query command of the instantiated SqlDataAdapter.
DataSet ds1 = new DataSet (); // instantiate dataset
Da. Fill (ds1); // Fill data in dataset
DataGridView1.DataSource = ds1.Tables [0]. DefaultView; // bind the dataset to the datagridview.