C # access MySQL database

Source: Internet
Author: User

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

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.