Although the actual development of the Entity is used, but the foundation is to master and review the ^^
//Set connection string, Server,database,username,passwordMysqlconnection con =NewMysqlconnection ("Server=localhost;database=gridview; Uid=keatkeat; password=001001"); Mysqltransaction Transaction=NULL;
Mysqldatareader reader = null;Try{mysqlcommand command=NewMysqlcommand (); Command. Connection=con; Command.commandtype=CommandType.Text; Con. Open (); Transaction=con. BeginTransaction (); Command.commandtext="SELECT * from Task_record where id=?para0"; //Add and Remove Parameterscommand. Parameters.clear (); Command. Parameters.addwithvalue ("? para0",1);//For Protect SQL inject attack//For Insert Update//int result = command. ExecuteNonQuery (); //long lastinsertid = command. Lastinsertedid;Transaction.commit (); //For select and use AdapterMysqldataadapter adapter =Newmysqldataadapter (command); DataTable Table=NewDataTable (); Adapter. Fill (table); //after loading the table, you can call the for(inti =0, L = table. Rows.Count; I < L; i++) { for(intA =0, B = table. Columns.count; a < b; a++) { stringColumnName =table. Columns[a]. ColumnName; } stringSomedata = table. rows[i]["ColumnName"]. ToString (); } //Select then use ReaderReader=command. ExecuteReader (); intColummcount =Reader. FieldCount; intRowIndex =0; while(reader. Read ()) the data within the//reader can only be read once Oh { for(inti =0; i < Colummcount; i++) { stringColumnName =Reader. GetName (i); ObjectValue =Reader[i]; } RowIndex++; } reader. Close (); }Catch(Exception ex) {if(Transaction! =NULL) transaction. Rollback (); stringx =Ex. Message;}finally{ if(Con. state = =System.Data.ConnectionState.Open) {con. Close (); }}
Basic ADO Access MySQL database example