C # basic operations of MySQL on windows

Source: Internet
Author: User

Notes for using MySQL in Windows:

1: Download MYSQL. Use GB2312 encoding when installing the configuration to avoid Chinese garbled characters!

2: download and install the corresponding driver MySQLDriver.

3: When writing code, set reference MySQLDriverCS. dll!

//************************************** **************************************** *********

View data in MYSQL: --->

<Form id = "form1" runat = "server">
<Div> </div>
<Asp: TextBox ID = "tbName" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "tbAddress" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "tbTelephone" runat = "server"> </asp: TextBox>
<Asp: Button ID = "btnSubmit" runat = "server" OnClick = "btnSubmit_Click" Text = "insert data"/> <br/>
<Div>
<Asp: GridView ID = "gwShow" runat = "server">
</Asp: GridView>
</Div>
</Form>

 

 

//**************
Using MySQLDriverCS;
 

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
BindData ();
}

Private void BindData ()
{
MySQLConnection conn = null;
Conn = new MySQLConnection (new MySQLConnectionString ("localhost", "MyTest", "root", "jasenkin"). AsString );

// Name of the local database, username, root Password
Conn. Open ();
MySQLCommand commn = new MySQLCommand ("set names gb2312", conn );
Commn. ExecuteNonQuery (); // set the set names gb2312 to solve the garbled characters.
String SQL = "select * from user ";
MySQLDataAdapter mda = new MySQLDataAdapter (SQL, conn );
DataSet ds = new DataSet ();
Mda. Fill (ds, "tb ");
This. gwShow. DataSource = ds. Tables ["tb"];
GwShow. DataBind ();
Conn. Close ();
}


Protected void btnSubmit_Click (object sender, EventArgs e)
{
MySQLConnection conn = null;
Conn = new MySQLConnection (new MySQLConnectionString ("localhost", "MyTest", "root", "jasenkin"). AsString );
Conn. Open ();
MySQLCommand commn = new MySQLCommand ("set names gb2312", conn );
Commn. ExecuteNonQuery ();
MySQLCommand comm = new MySQLCommand (string. format ("insert into user (name, address, telephone) values ({0}, {1}, {2})", tbName. text. trim (), tbAddress. text. trim (), tbTelephone. text. trim (), conn );
Comm. ExecuteNonQuery ();
Conn. Close ();
BindData ();
}
}
 

 

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.