MySQL Database tool class--datatable batch join MySQL database (NET version)

Source: Internet
Author: User
Tags connectionstrings

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)

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.