C # ASP. NET connect MySQL Database

Source: Internet
Author: User

First add a reference:

Using System.Data;
Using MySql.Data.MySqlClient;

Code: Here only the background code is written

Mysql connection and SQL connection code almost as long as the

Mysqlcommand com = new mysqlcommand (Sqlin,con); Similar mysqlcomand are changed to SqlCommand .
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingMySql.Data.MySqlClient;namespacewebapplication1{ Public Partial classAddertong:System.Web.UI.Page {//Database Address        stringSer ="Server=localhost; Userid=root; Password=password;database=child;pooling=false; charset=utf8;port=3306"; protected voidPage_Load (Objectsender, EventArgs e) {            ////Determine if the user is logged in            //if (session["name"] = = NULL | | session["name"] = = "")//{            //Response.Write ("<script>alert (\" Please login first!)            \ ");</script>"); //Response.Redirect ("Login.aspx"); //return; //}                    }        protected voidBtnzhece_click (Objectsender, EventArgs e) {            stringa=session["name"].            ToString (); Mysqlconnection Con=Newmysqlconnection (Ser); //string sqlin = "INSERT into Ertong (name2,mima,name) VALUES (name2= '" +txtname2. Text.trim () + "', mima= '" +txtmima. Text.trim () + "'" + ", name= '" +session["name"]. ToString () + "'";            stringSqlin ="INSERT INTO Ertong (Name2,mima,name,beizu) VALUES ('"+ txtname2. Text.trim () +"', '"+ Txtmima. Text.trim () +"'"+", '"+ session["name"]. ToString () +"', '"+txtpassword2. Text.trim () +"')"; Mysqlcommand com=NewMysqlcommand (Sqlin,con); DataSet DS=NewDataSet (); Mysqldataadapter DB=Newmysqldataadapter (COM); Db.            Fill (DS); Response.Write ("<script>alert (\ "add success \");</script>"); }    }}

C # ASP. NET connection to 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.