1. Requirements
Use C # to access MySQL database for curd
2. Installation and class library references
Http://www.tuicool.com/articles/rAbuee
The package in the Baidu Cloud disk, can also be downloaded through the official
3. Database Operation read
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Collections;usingSystem.Collections.Generic;usingMySql.Data.MySqlClient;usingSystem.Data;namespaceconsoleapplication8{classProgram {Static voidMain (string[] args) { Const stringConnStr ="server=localhost;database =storecode;user id=root; password=root;";//Connection Stringmysqlconnection Mysqlconn=Newmysqlconnection (CONNSTR); stringSqlstr ="SELECT * from Ssc_common_block";//SQL statements Try{mysqlconn.open ();;//Open ConnectionMysqldataadapter MSDA =NewMysqldataadapter (Sqlstr, mysqlconn);//New Mysqldataadapter ObjectDataSet ds =NewDataSet ();//New DataSet ObjectMsda. Fill (DS);//Populating a DataSet objectConsole.WriteLine ("Id\t\tname"); intCNT = ds. tables[0]. Rows.Count;//number of rows read for(intIX =0; IX! = CNT; ++ix) {Console.WriteLine ("{0}\t\t{1}", Turnstring (ds. tables[0]. Rows[ix]. itemarray[0]. ToString ()), Turnstring (ds. tables[0]. Rows[ix]. itemarray[3]. ToString ()));//cyclic reading of data Note Index range} console.readkey (); } Catch(Mysqlexception ex) {Console.WriteLine (ex). Message); } Catch(Exception ex) {Console.WriteLine (ex). Message); } finally{mysqlconn.close ();//Close Connection } } //garbled resolution of conversion functions Public Static stringTurnstring (stringsource) { varEn = System.Text.Encoding.GetEncoding ("UTF-8"); varBS =en. GetBytes (source); varA =System.Text.Encoding.UTF8.GetString (BS); returnA; } }}
C # access MySQL database