C # Connection Database __ Database

Source: Internet
Author: User

Run in vs2010, you must refer to the Mysql.data
/Connection Database/
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Mysql.data;
Using System.Data;
Using MySql.Data.MySqlClient;

Namespace Connection Database
{
Class Program
{
static void Main (string[] args)
{
Mysqlconnection conn = null;
Conn=new mysqlconnection ("Data source=localhost;database=php; User Id=root; Password=111111 ");
Conn. Open ();
IF (Conn. state.tostring () = = "Open")
Console.WriteLine ("Connection succeeded.") ”);
Else
Console.WriteLine ("Connection failed.") ”);
}
}
}

/perform additions and deletions or change operations
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Mysql.data;
Using System.Data;
Using MySql.Data.MySqlClient;

Namespace Connection Database
{
Class Program
{
static void Main (string[] args)
{
Mysqlconnection conn = null;
Conn=new mysqlconnection ("Data source=localhost;database=php; User Id=root; Password=111111 ");
Conn. Open ();
IF (Conn. state.tostring () = = "Open")
Console.WriteLine ("Connection succeeded.") ”);
Else
Console.WriteLine ("Connection failed.") ”);

        Mysqlcommand com = new Mysqlcommand ();
        Com. Connection = conn;
        Com.commandtext = "INSERT into class values (1, ' xiaoming ',"), (2, ' Xiaoliang ', 5) ";
        int i = com. ExecuteNonQuery ();
        if (i > 0)
            Console.WriteLine ("Insert record {0} bar", i);
        Else
            Console.WriteLine ("Insert not successful.") ");
    }
}

}

/Query Operations/
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Mysql.data;
Using System.Data;
Using MySql.Data.MySqlClient;

Namespace Connection Database
{
Class Program
{
static void Main (string[] args)
{
Mysqlconnection conn = null;
Conn=new mysqlconnection ("Data source=localhost;database=php; User Id=root; Password=111111 ");
Conn. Open ();
IF (Conn. state.tostring () = = "Open")
Console.WriteLine ("Connection succeeded.") ”);
Else
Console.WriteLine ("Connection failed.") ”);

        Mysqlcommand com = new Mysqlcommand ("SELECT * FROM class", conn);
        Mysqldatareader dr = null;
        Dr = Com. ExecuteReader ();
        while (Dr. Read ())
        {
            Console.WriteLine (dr["id"] + "" + dr["name" + "" + dr["Age");}

    }

}

/Continuous Query/
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Mysql.data;
Using System.Data;
Using MySql.Data.MySqlClient;

Namespace Connection Database
{
Class Program
{
static void Main (string[] args)
{
Mysqlconnection conn = null;
Conn=new mysqlconnection ("Data source=localhost;database=php; User Id=root; Password=111111 ");
Conn. Open ();
IF (Conn. state.tostring () = = "Open")
Console.WriteLine ("Connection succeeded.") ”);
Else
Console.WriteLine ("Connection failed.") ”);

        Mysqlcommand com = new Mysqlcommand ("SELECT * from class where ID =1;select * from class where ID =2", conn);
        Mysqldatareader dr = null;
        Dr = Com. ExecuteReader ();
        while (Dr. Read ())
        {
            Console.WriteLine (dr["id"] + "" + dr["name" + "" + dr["age"));
        if (Dr. NextResult ())
        {while
            (DR). Read ())
                Console.WriteLine (dr["id"] + "" + dr["name" + "" + dr["Age");}

}

SqlConnection Conn;
    SqlDataAdapter adapter; private void Button1_Click (object sender, EventArgs e) {conn = new SqlConnection ("server=.;
        database=db_16;uid=sa;pwd=123456 ")//Connection Database SqlDataAdapter SDA = new SqlDataAdapter (" select * from Tb_emp ", conn);
        DataSet ds = new DataSet (); Sda.
        Fill (DS); Datagridview1.datasource = ds. tables[0];//bound data Source to datagridview datagridview1.rowheadersvisible = false;//Hide first column for (int i = 0; i < Datag ridview1.columncount;i++) {Datagridview1.columns[i].
        Width = 84; } button1.
        Enabled = false; Datagridview1.columns[0].
    ReadOnly = true; The Private DataTable Dbconn (String strSQL)//connects the database, fills the DataTable, and returns {Conn.
        Open ();
        This.adapter = New SqlDataAdapter (strSQL, Conn);
        DataTable dtselect = new DataTable ();
        int rnt = This.adapter.Fill (Dtselect); Conn.
        Close ();
    return dtselect; } private void BuTton2_click (object sender, EventArgs e) {if (Dbupdate ())//Calling Private Bool dbupdate () and judging {MessageBox.Show ("modified successfully.")
        ");
        } private Boolean Dbupdate () {String strSQL = ' select * from Tb_emp ';
        DataTable dtupdate = new DataTable ();
        Dtupdate = This.dbconn (strSQL);//Call Private DataTable Dbconn (string strSQL) and get dtupdate empty table dtUpdate.Rows.Clear ();
        DataTable dtshow = new DataTable (); Dtshow = (DataTable) this.datagridview1.datasource;//bind the modified DataGridView data to the Dtshow table for (int i = 0; i < DTSHOW.R oWS. Count;
        i++) {Dtupdate.importrow (dtshow.rows[i]);//Copy data from Dtshow table to dtupdate empty table} try                            {This.conn.Open ();
            Open the database and update the SqlCommandBuilder Commandbuiler;
            Commandbuiler = new SqlCommandBuilder (this.adapter);
            This.adapter.Update (dtupdate); This.conn.ClOSE (); The catch (Exception ex) {MessageBox.Show (ex).
            Message.tostring ());
        return false;
    Dtupdate.acceptchanges ();//update database return true;
 }
}
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.