MySQL Database tool class--datatable batch join database (NET version), Mysqldbhelper General class want to be useful to everyone, the code is as follows:
usingMySql.Data.MySqlClient;usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Text; Public classmysqldbhelper{#regionPrivate variablesPrivate Const stringDefaultconfigkeyname ="mysqlconnection";//connection string database= ' database ';D ata source= ' IP address '; User id= ' sa '; password= ' sa ';p ooling=true Private stringconnectionString; Private stringProviderName; #endregion #regionconstructor function/// <summary> ///default Constructor (DBHelper)/// </summary> PublicMysqldbhelper () { This. connectionString =Configurationmanager.connectionstrings[defaultconfigkeyname]. ConnectionString; This. ProviderName =Configurationmanager.connectionstrings[defaultconfigkeyname]. ProviderName; } /// <summary> ///DBHelper constructor Function/// </summary> /// <param name= "KeyName" >Connection string Name</param> PublicMysqldbhelper (stringkeyName) { This. connectionString =Configurationmanager.connectionstrings[keyname]. ConnectionString; This. ProviderName =Configurationmanager.connectionstrings[keyname]. ProviderName; } #endregion Public intExecuteNonQuery (stringSqlparamsmysqlparameter[] Parameters) { intres =0; using(Mysqlconnection con =Newmysqlconnection (connectionString)) {con. Open (); using(Mysqlcommand cmd =Newmysqlcommand (sql, con)) { foreach(Mysqlparameter parameterinchparameters) {cmd. Parameters.Add (parameter); } Try{res=cmd. ExecuteNonQuery (); } Catch{res= -1; } } } returnRes; } Public ObjectGetexescalar (stringSqlparamsmysqlparameter[] Parameters) { Objectres =NULL; using(Mysqlconnection con =Newmysqlconnection (connectionString)) {con. Open (); using(Mysqlcommand cmd =Newmysqlcommand (sql, con)) { foreach(Mysqlparameter parameterinchparameters) {cmd. Parameters.Add (parameter); } Res=cmd. ExecuteScalar (); } } returnRes; } PublicDataTable getdatatable (stringSqlparamsmysqlparameter[] Parameters) {DataSet DataSet=NewDataSet (); using(Mysqlconnection con =Newmysqlconnection (connectionString)) {con. Open (); using(Mysqlcommand cmd =Newmysqlcommand (sql, con)) { foreach(Mysqlparameter parameterinchparameters) {cmd. Parameters.Add (parameter); } Mysqldataadapter Adapter=Newmysqldataadapter (CMD); Adapter. Fill (DataSet); } } returnDataSet. tables[0]; } /// <summary> ///DataTable Batch join MySQL database/// </summary> /// <param name= "dataTable" ></param> /// <returns></returns> Public stringinsertbydatatable (DataTable DataTable) {stringresult =string. Empty; if(NULL= = DataTable | | DataTable.Rows.Count <=0) { return "Add failed! DataTable No data! "; } if(string. IsNullOrEmpty (Datatable.tablename)) {return "Add failed! Please set the name of the DataTable first! "; } //Building INSERT StatementsStringBuilder SB =NewStringBuilder (); Sb. Append ("INSERT into"+ Datatable.tablename +"("); for(inti =0; i < DataTable.Columns.Count; i++) {sb. Append (Datatable.columns[i]. ColumnName+","); } sb. Remove (sb.) ToString (). LastIndexOf (','),1); Sb. Append (") VALUES"); for(inti =0; i < DataTable.Rows.Count; i++) {sb. Append ("("); for(intj =0; J < DataTable.Columns.Count; J + +) {sb. Append ("'"+ Datatable.rows[i][j] +"',"); } sb. Remove (sb.) ToString (). LastIndexOf (','),1); Sb. Append ("),"); } sb. Remove (sb.) ToString (). LastIndexOf (','),1); Sb. Append (";"); intres =-1; using(Mysqlconnection con =Newmysqlconnection (connectionString)) {con. Open (); using(Mysqlcommand cmd =NewMysqlcommand (sb.) ToString (), con)) {Try{res=cmd. ExecuteNonQuery (); } Catch(Exception ex) {res= -1; //Unknown column ' names ' in ' Field List 'result ="operation failed! "+ ex. Message.replace ("Unknown Column","Unknown column"). Replace ("In ' field list '","exists in the field collection! "); } } } if(Res >0) {result="congratulations on adding success!"; } returnresult; }}
MySQL Database tool class--datatable batch join MySQL database (NET version)