Two ways to connect to the MySQL database

Source: Internet
Author: User

1, connect the MySQL database with Mysqldrivercs to download and install Mysqldrivercs, locate MySQLDriver.dll under the installation folder, and then add the MySQLDriver.dll to the project. usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Data.Odbc; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingMysqldrivercs; namespaceMySQL { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }              Private voidForm1_Load (Objectsender, EventArgs e) {             Mysqlconnection conn =NULL; Conn=NewMysqlconnection (NewMysqlconnectionstring ("localhost","INV","Root","831025").                  asstring); Conn.                Open (); Mysqlcommand COMMN =NewMysqlcommand ("Set names gb2312", conn); Commn.                  ExecuteNonQuery (); stringsql ="SELECT * from Exchange"; Mysqldataadapter MDA=Newmysqldataadapter (SQL, conn); DataSet DS=NewDataSet (); Mda. Fill (DS,"table1");  This. DataGrid1.DataSource = ds. tables["table1"]; Conn.              Close (); }}} Copy code2, accessing the MySQL database via ODBC:1. Install Microsoft Odbc.net: I installed mysql-connector-odbc-3.51. A-Win32.msi2. Installing MDAC2.7 or later: I installed the mdac_typ.exe.2. 7 Simplified Chinese version3. Install the ODBC driver for MySQL: I'm installing Odbc_net.msi.4. management tools, data source odbc–>Configure DSN ...5. Add reference Microsoft.Data.Odbc.dll in Solution management (1.0.3300)6. Add references in codeusingMicrosoft.Data.Odbc; usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Drawing; usingSystem.Linq;//VS2005 does not seem to have this namespace, test automatically generated under c#2008    usingSystem.Text; usingSystem.Windows.Forms; usingMicrosoft.Data.Odbc; namespaceMySQL { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }                    Private voidForm1_Load (Objectsender, EventArgs e) {                  stringMyconstring ="driver={mysql ODBC 3.51 DRIVER};"+"Server=localhost;"+"DATABASE=INV;"+"Uid=root;"+"password=831025;"+"option=3"; OdbcConnection myconnection=NewOdbcConnection (myconstring);                  Myconnection.open (); Console.WriteLine (""N Success, connected successfully!"N"); stringquery ="INSERT into test values (' Hello ', ' Lucas ', ' Liu ')"; OdbcCommand cmd=NewOdbcCommand (query, MyConnection); //Handling Exceptions: Inserting duplicate records with exception    Try{cmd.      ExecuteNonQuery (); }      Catch(Exception ex) {Console.WriteLine ("record Duplicate."); }finally{cmd.      Dispose (); }      //*********************** reading data to textbox********************** using the Read method                stringTMP1 =NULL; stringTMP2 =NULL; stringTmp3 =NULL; Query="SELECT * FROM Test"; OdbcCommand CMD2=NewOdbcCommand (query, MyConnection); OdbcDataReader Reader=CMD2.                  ExecuteReader ();  while(reader. Read ()) {TMP1= reader[0].                      ToString (); TMP2= reader[1].                      ToString (); Tmp3= reader[2].                  ToString (); }                   This. TextBox1.Text = Tmp1 +" "+ TMP2 +" "+Tmp3; *///************************ display the datasheet with the DataGridView control **************************    stringMyconstring ="driver={mysql ODBC 3.51 DRIVER};"+"Server=localhost;"+"DATABASE=INV;"+"Uid=root;"+"password=831025;"+"option=3"; OdbcConnection myconnection=NewOdbcConnection (myconstring); OdbcDataAdapter ODA=NewOdbcDataAdapter ("SELECT * FROM Customer", MyConnection); DataSet DS=NewDataSet (); Oda. Fill (DS,"Employee");  This. Datagridview1.datasource = ds. tables["Employee"]; */Myconnection.close (); }} Copy Code*copyright Notice: When reproduced, please indicate the original source and author information in the form of hyperlinks .*This article from: Unity3d Tutorial Manual* This article links: http://www.unity Bull. com/6880.html

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.