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