Preface
Ado. NET includes 5 large objects, namely connection, Command, DataReader, DataSet, DataAdapter, using ADO to access the database has two steps: to establish a database connection, read or update data. There are two main ways of reading data: Command and DataReader, DataAdapter and DataSet.
Connection to MySQL database preparation 1. Download the free-install. zip link on the MySQL website as follows: Http://dev.mysql.com/downloads/connector/net/5.0.html,platform SELECT. Net & Mono. 2. Unzip after download, unzip the file open V4.0 (the example in this blog is used in this version) 3. You can see some DLL files when you open them and add references to them in your project 4. The following example is the operation of the table Comment table in database test in MySQL. Step one: Establish a database connection 1. Define Global variables
[CSharp]View PlainCopy
- To create a Command object
- private Mysqlcommand cmd = null;
- Create a Connection Connection object
- private Mysqlconnection conn = null;
2. Establishing a database connection
[CSharp]View PlainCopy
- Database connection string
- String connstr = "Server=localhost;database =test;uid=root;pwd=1;charset=utf8";
- Establishing a database connection
- conn = new Mysqlconnection (CONNSTR);
Step two: Read Data 1.command and DataReader object read Data Description: The DataReader object provides a read-only forward pointer to the database retrieval data. DataReader is an abstract class that cannot be instantiated directly, so you need to create a command object before using it, and then create the DataReader object, which is read-only, so you cannot modify it. Process of reading data: ① Create connection ② Open connection ③ create command object ④ Execute command ExecuteReader () method ⑤ bind DataReader to Data Control ⑥ close Datareader⑦ Close The connection code shows:
[CSharp]View PlainCopy
- Mysqldatareader reader = null;
- Try
- {
- Conn. Open (); //② Opening a database connection
- cmd = new Mysqlcommand ("select * from comment", conn); //③ Creating a SqlCommand object using the specified SQL command and connection object
- reader = cmd. ExecuteReader (); //④ ExecuteReader () method to execute command
- //⑤ binding DataReader to a Data control
- DataTable dt = new DataTable ();
- Dt. Load (reader);
- Datagridview1.datasource = DT;
- }catch (Exception) {
- throw;
- }finally{
- //⑥ Close DataReader
- Reader. Close ();
- //⑦ Closing the connection
- Conn. Close ();
- }
[CSharp]View PlainCopy
2.DataSet and DataAdapter Object read Data Description: DataAdapter is the media between the dataset and the database, DataAdapter open a connection and execute the corresponding MySQL statement, the dataset is equivalent to a small database, You can store many tables, the dataset is a collection object, we can read the data in the dataset, and we can update the data in it. The process of reading data: ① Create a connection ② create a DataAdapter object ③ create a DataSet object ④ execute the DataAdapter object's fill () method ⑤ bind the table in the dataset to the code presentation in the Data control:
[CSharp]View PlainCopy
- Try
- {
- string mysqltext = "select * from comment;";
- //Create a SqlDataAdapter object with the specified SQL command and connection object
- Mysqldataadapter MYSDA = new Mysqldataadapter (MYSQLTEXT, conn);
- DataSet ds = new DataSet (); //Create a DataSet object
- //Use the SqlDataAdapter Fill method to populate the DataSet Mysda. Fill (ds, "comment");
- //bind a table in a dataset to a data control
- BindingSource BS;
- BS = new BindingSource ();
- Bs. DataSource = ds. tables["comment"];
- Datagridview1.datasource = BS;
- }
- catch (Exception) {
- throw;
- }
[CSharp]View PlainCopy
Step three: Update data 1.command Object Update data Description: A transaction is a set of units of related tasks that either succeed or fail altogether. If they fail, all are rolled back, and the four properties (ACID) of the transaction are atomicity (atomicity), consistency (consistency), isolation (isolation), persistence (durability), respectively. Detailed information no longer repeat, do not know can Baidu a bit Oh! Code Show:
[CSharp]View PlainCopy
- Database connection string
- String connstr = "Server=localhost;database =test;uid=root;pwd=1;charset=utf8";
- Establishing a database connection
- using (conn = new Mysqlconnection (CONNSTR))
- {
- Conn. Open ();
- //Start a transaction
- using (mysqltransaction transaction = conn. BeginTransaction ())
- {
- using (cmd=conn. CreateCommand ())
- {
- Try
- {
- Cmd. Transaction = Transaction; //Specify a transaction for the command
- Cmd.commandtext = "insert into comment (quantity,comment) value (' 11 ', ' very good ');";
- Cmd. ExecuteNonQuery ();
- Cmd.commandtext = "insert into comment (quantity,comment) value (' 12 ', ' very good ');";
- Cmd. ExecuteNonQuery ();
- Transaction.commit (); //Transaction Submission
- }
- catch (Exception)
- {
- Transaction. Rollback (); //Transaction rollback
- }
- }
- }
- }
2.DataAdapter Object Update Data
[CSharp]View PlainCopy
- <span style="FONT-SIZE:18PX;" >//Database connection string
- String connstr = "Server=localhost;database =test;uid=root;pwd=1;charset=utf8";
- //Establish database connection
- conn = new Mysqlconnection (CONNSTR);
- Try
- {
- //comment is the table name, quantity is the field name
- string mysqltext = "select * from comment;";
- Mysqldataadapter MYSDA = new Mysqldataadapter (MYSQLTEXT, conn);
- DataSet ds = new DataSet ();
- Mysda. Fill (ds, "comment");
- //Bind Mysqldataadapter object, automatically generate command to update MySQL from DataSet
- Mysqlcommandbuilder cb = new Mysqlcommandbuilder (MYSDA);
- //Update data in DS
- Ds. tables["comment"]. rows[0]["Quantity"] = 111;
- //Update data in the database
- Mysda. Update (ds, "comment");
- }
- catch (Exception)
- {
- throw;
- }</span>
Summary 1.DataReader object read data in the database can only read, and is read-only, can not be modified; The data read by the dataset and the DataAdapter object is readable and can be modified, and many tables can be stored in the dataset. 2.DataReader read the data before the need to manually write code to connect the database, that is Conn.Open (), after the completion of the query need to manually write code to close the database connection; the DataSet and the DataAdapter object do not need to manually write code to connect to the database before reading the data, it will automatically identify If the database connection is not open, it is turned on and automatically shuts down if it is not turned off.
Ado. NET Operation MySQL Database