usingMySql.Data.MySqlClient;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceshoujihao{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidImports_click (Objectsender, EventArgs e) { //File Open dialog boxOpenFileDialog dlg =NewOpenFileDialog (); //If the user does not select a file, it returns if(DLG. ShowDialog ()! =System.Windows.Forms.DialogResult.OK) {return; } //get user-selected files stringFileName =dlg. FileName; //Read the selected file using(Stream fs =NewFileStream (filename,filemode.open))using(StreamReader reader =NewStreamReader (FS, Encoding.default)) { //start reading this file stringLine ; //Skip the first lineline =Reader. ReadLine (); //Create a database link using(Mysqlconnection conn =mysqlhelper.createconnection ())//Transaction Rollback using(Mysqltransaction tx =Conn. BeginTransaction ()) {Try { while(line = reader.) ReadLine ())! =NULL) { //If the last behavior is an empty string, jump out of the loop if(string. IsNullOrEmpty (line)) { Break; } //divide the line string by the number string[] lines = line. Split (','); //Remove the "" number from the string stringMobilenumber = lines[1]. Trim ('"'); stringMobilearea = lines[2]. Trim ('"'); stringMobiletype = lines[3]. Trim ('"'); //Insert Database//Mysqlhelper.executenonquery (conn, "INSERT into T_mobile (Mobilenumber,mobilearea,mobiletype) VALUES (@ Mobilenumber, @MobileArea, @MobileType)) ",//new Mysqlparameter {parametername = "@MobileNumber", Value = Mobilenumber},//new Mysqlparameter {parametername = ", @MobileArea", Value = Mobilearea},//new Mysqlparameter {parametername = "@MobileType", Value = Mobiletype});Mysqlhelper.executenonquery (Conn,"INSERT into T_mobile (mobilenumber,mobilearea,mobiletype) VALUES (@MobileNumber, @MobileArea, @MobileType)", Newmysqlparameter {parametername ="@MobileNumber", Value =Mobilenumber},Newmysqlparameter {parametername ="@MobileArea", Value =Mobilearea},Newmysqlparameter {parametername ="@MobileType", Value =Mobiletype}); } tx.commit (); } Catch(Exception ex) {TX. Rollback (); MessageBox.Show ("Import Failed"+Ex. Message); }}} MessageBox.Show ("Import Successful!"); } Private voidCx_click (Objectsender, EventArgs e) { //get the phone number entered by the user stringPhonenum =Textphonenum.text; //The first seven digits of the user's mobile phone number stringPhonefix = phonenum.substring (0,7); //go to database query informationDataTable table = Mysqlhelper.executequery ("SELECT Mobilenumber,mobilearea from t_mobile WHERE mobilenumber = @phoneFix", Newmysqlparameter {parametername ="@phoneFix", Value =Phonefix}); // if(table. Rows.Count <=0) {MessageBox.Show ("Wood has query to data"); } Else{DataRow row= table. rows[0]; stringMobilearea = (string) row[1]; stringMobiletype = (string) row[2]; MessageBox.Show (Mobilearea+Mobiletype); } } }}
C #------Mobile phone Number attribution query (query data)