Basic ADO Access MySQL database example

Source: Internet
Author: User

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

Related Article

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.