C # MYSQL database connection and display

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.