A ado.net common operations common class that supports SqlClient and OLE DB

Source: Internet
Author: User
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
Using System.Data.OleDb; /*
***************************************
*                                                                           *
*                                                                           Author: ghostbear * * Blog: http://blog.csdn.net/ghostbear * * * *************************************** * * * namespace Adonetomnip
    otence {///<summary>///generic Class (SQL and OLE DB)///</summary> public class omnipotence for manipulating ado.net {bool Issqlclient = true;//to determine whether to call SqlClient or OLE DB string connstr;//connection string SqlConnection sconn = Nu
        ll
        OleDbConnection oconn = null; <summary>///Initialization string///</summary>///<param name= "ConnectionString" > Connection string </param> public omnipotence (stringConnectionString) {this.connstr = connectionstring; ///<summary>///Specifies the connection string and the target of the manipulation///</summary>///<param name= "Connectio Nstring "> Connection string </param>///<param name=" issqlclient "> Manipulated target </param> public omnipotence
        (string connectionstring, bool issqlclient)
            {this.connstr = connectionstring;
        This.issqlclient = issqlclient;
            ///<summary>///Open connection///</summary> private void open () { if (issqlclient) {if (sconn = = null) sconn = new SqlConnection (Connst
                R); if (sconn. state = = connectionstate.closed) sconn.
            Open (); else {if (oconn = null) oconn = new OleDbConnection (connst
                R); if (oconn. state = = Connectionstate.closed) oconn.
            Open ();
        }///<summary>///shut down connection///</summary> private void Close () {if (issqlclient) {if (sconn!= null) if (sconn. state = = ConnectionState.Open) sconn.
            Close (); } else {if (oconn!= null) if (oconn). state = = ConnectionState.Open) oconn.
            Close ();                                                                           }
        }
            
        /*
***************************************
* * * Author: ghostbear * * Blog: Http://blog.csdn.net/gho Stbear * * * ******************************* *////<summary>///Return Command object, can be SqlCommand or OLedbcommand///</summary>///<param name= "sqlcmd" > Command </param>///<param nam E= "isstored" > Whether stored procedures </param>///<param name= "param" > Parameters </param>///<returns> One pack 
            Box Command object </returns> Private Object command (String sqlcmd, bool isstored, idataparameter[] param) {    if (issqlclient) {SqlCommand cmd = new SqlCommand (sqlcmd); Initializes command cmd.                     Connection = sconn;  Give the Connection object if (isstored) Cmd.commandtype = CommandType.StoredProcedure;
                Give the command type else Cmd.commandtype = CommandType.Text;
                        if (param!= null) {foreach (SqlParameter t in param) { Cmd.          Parameters.Add (t); Parameter database is not empty add parameters}cmd    Return SqlCommand} else {OleDbCommand cmd = new OleDbCommand (sqlcmd); Initializes command cmd.                     Connection = oconn;  Give the Connection object if (isstored) Cmd.commandtype = CommandType.StoredProcedure;
                Give the command type else Cmd.commandtype = CommandType.Text;
                        if (param!= null) {foreach (OleDbParameter t in param) { Cmd.          Parameters.Add (t);                             Parameter database is not empty then add parameters} return cmd; Return OleDbCommand}}///<summary>///return dataadapt object///</summary&gt
        ;
        <param name= "command" >command object </param>///<returns> a boxed DataAdapter object </returns> Private Object AdaptThe ER (Object command)//Returns an object of type Dataadpter {object adpt;
            if (issqlclient) {adpt = new SqlDataAdapter ((SqlCommand) command);
            else {adpt = new OleDbDataAdapter (OleDbCommand) command);
        return ADPT; ///<summary>///Perform the operation. Returns the number of rows affected///</summary>///<param name= "sqlcmd" >sql statement or stored procedure name </param>///<param name= "isstored"                                                                           > Whether the number of rows affected by a stored procedure </param>///<returns> statement is performed </returns>/* *************************************** *                                               * * Author: ghostbear                                                                           * * Blog: http://blog.csdn.net/ghostbear * * * *************************************** */public void Runproc (string sqlcmd, bool Isstored,out int Infect
      Ionrows) {      SqlCommand Scmd;
            OleDbCommand Ocmd;
            Open ();
                if (issqlclient) {scmd = (SqlCommand) command (sqlcmd, isstored, NULL); Infectionrows = Scmd.
                ExecuteNonQuery ();
            Close ();
                else {ocmd = (OleDbCommand) command (sqlcmd, isstored, NULL); Infectionrows = ocmd.
                ExecuteNonQuery ();
            Close ();
        } return; ///<summary>///Perform the operation. Returns the number of rows affected///</summary>///<param name= "sqlcmd" >sql statement or stored procedure name </param>///<para M name= "isstored" > For stored Procedures </param>///<param name= "param" > execution required Parameters </param>///<re Number of rows affected by the turns> operation </returns> public void Runproc (string sqlcmd, bool isstored, idataparameter[] param,out int
            Infectionrows) {SqlCommand scmd; OleDbCommand Ocmd;
            Open ();
                 
                if (issqlclient) {scmd = (SqlCommand) command (sqlcmd, isstored, param); Infectionrows=scmd.
                ExecuteNonQuery ();
            Close ();
                 
                else {ocmd = (OleDbCommand) command (sqlcmd, isstored, param); Infectionrows=ocmd.
                ExecuteNonQuery ();
            Close ();
        } return;                                                                           }
            
        /*
***************************************
*   
* Author: ghostbear * * Blog: http://blog.csdn.net/ghostbear * *                                                                           *
***************************************
* ////<summary>///Returns the value in the first column of the first row in the data collection///</summary>///<param name= "sqlcmd ">sql statement or stored procedure name </param>///<param name= "isstored" > Whether stored procedures </param>///<param name= "ISRETURNCONTETN" > Return value <  /param>///<returns></returns> public void Runproc (string sqlcmd, bool Isstored,out Object
            isreturncontent) {SqlCommand scmd;
            OleDbCommand Ocmd;
            Open ();
                 
                if (issqlclient) {scmd = (SqlCommand) command (sqlcmd, isstored, NULL); Isreturncontent = Scmd.
                ExecuteScalar ();
            Close ();
                  
                else {ocmd = (OleDbCommand) command (sqlcmd, isstored, NULL); Isreturncontent = ocmd.
                ExecuteScalar ();
            Close ();
        } return; ///<summary>///Returns the value in the first column of the first row in the data collection///</summary>///<param name= "sqlcmd ">sql statement or stored procedure name </param>///<param name="isstored "> Whether stored procedures </param>///<param name=" param "> Parameter set </param>///<param name=" IsR Eturncontent "> Return value </param>///<returns></returns> public void Runproc (String sqlcmd,
            BOOL isstored, idataparameter[] param, out object isreturncontent) {SqlCommand scmd;
            OleDbCommand Ocmd;
            Open ();
                   
                if (issqlclient) {scmd = (SqlCommand) command (Sqlcmd,isstored,param); Isreturncontent = Scmd.
                ExecuteScalar ();
            Close ();
                 
                else {ocmd = (OleDbCommand) command (sqlcmd, isstored, param); Isreturncontent = ocmd.
                ExecuteScalar ();
            Close ();
        } return;                                                                           }
        /*
***************************************
** Author: ghostbear * * Blog: http://blog.csdn.net/ghostbear * * * *************************************** * * *///<s Ummary>///returns SqlDataReader object///</summary>///<param name= "sqlcmd" >sql statements or stored procedures ;/param>///<param name= "isstored" > Whether stored procedures </param>///<param name= "reader" > Output sqldata
            Reader's out parameter </param> public void Runproc (string sqlcmd, bool Isstored,out SqlDataReader reader) {

           SqlCommand Scmd;
          Open ();
          
           Scmd = (SqlCommand) command (sqlcmd, isstored, NULL); Reader = Scmd.
            ExecuteReader (commandbehavior.closeconnection);
        Return ///<summary>///returns OleDbDataReader object///</summary>///<param name= "sql CMD ">sql statements or stored procedures </param>///&Lt;param name= "isstored" > Whether stored procedures </param>///<param name= "reader" > Output OleDbDataReader out Parameters </param > public void Runproc (string sqlcmd, bool isstored, out OleDbDataReader reader) {Ol
            Edbcommand ocmd;
        
                Open ();
            
                Ocmd = (OleDbCommand) command (sqlcmd, isstored, NULL); Reader = ocmd.
            
            ExecuteReader (commandbehavior.closeconnection);
        Return ///<summary>///returns OleDbDataReader object///</summary>///<param name= "sql CMD ">sql statement or stored procedure </param>///<param name=" isstored "> Whether stored procedures </param>///<param name= "param" > Command parameters </param>///<param name= "reader" > output OleDbDataReader out parameter </param> publi c void Runproc (String sqlcmd, bool isstored, idataparameter[] param, out SqlDataReader reader) {SQLC
       Ommand Scmd;     Open ();
            Scmd = (SqlCommand) command (sqlcmd, Isstored,param); Reader = Scmd.
            ExecuteReader (commandbehavior.closeconnection);
        Return ///<summary>///returns OleDbDataReader object///</summary>///<param name= "sql CMD ">sql statement or stored procedure name </param>///<param name=" isstored "> whether stored procedure </param>///<param name = "param" > Command parameters </param>///<param name= "reader" > output parameter </param> public void Runproc (Strin
            G sqlcmd, BOOL isstored, idataparameter[] param, out OleDbDataReader reader) {OleDbCommand ocmd;
            Open ();
            Ocmd = (OleDbCommand) command (sqlcmd, isstored, param); Reader = ocmd.
            ExecuteReader (commandbehavior.closeconnection);
        Return                                                                           }
            
        /*
***************************************
* * * forPerson: ghostbear * * Blog: http://blog.csdn.net/ghostbear * *  *
***************************************
*/
          
        /// <summary>///Returns the DataSet object///</summary>///<param name= "sqlcmd" >sql statement or command </ param>///<param name= "isstored" > Whether stored procedures </param>///<param name= "ds" > Output Parameters </param&
        Gt
            public void Runproc (string sqlcmd, bool isstored, out DataSet ds) {SqlDataAdapter sadpt = null;
            OleDbDataAdapter oadpt = null;
            DataSet myds = new DataSet ();
                if (issqlclient) {Open ();
                    
                SADPT = (SqlDataAdapter) Adapter (command (sqlcmd, isstored, null)); Sadpt.
                Fill (myds);
            Close ();
else {Open ();                OADPT = (OleDbDataAdapter) adapter (command (sqlcmd, isstored, null)); Oadpt.
                Fill (myds);
            Close ();
            ds = myDS;
            
        Return                                                                           }
            
        /*
***************************************
*   
* Author: ghostbear * * Blog: http://blog.csdn.net/ghostbear * *                                                                           *
***************************************
* ////<summary>///return DataSet object///</summary>///<param name= "sqlcmd" > SQL statements or commands </param>///<param name= "isstored" > Whether stored procedures </param>///<param name= "param" Parameters </param>///<param name= "ds" > Output dataset</param> public void Runproc (String sqlcmd, B Ool isstored, IdataparameteR[] param, out DataSet ds) {SqlDataAdapter sadpt = null;
            OleDbDataAdapter oadpt = null;
            DataSet myds = new DataSet ();
                if (issqlclient) {Open ();
                SADPT = (SqlDataAdapter) Adapter (command (sqlcmd, Isstored,param)); Sadpt.
                Fill (myds);
            Close ();
                else {Open ();
                OADPT = (OleDbDataAdapter) adapter (command (sqlcmd, Isstored,param)); Oadpt.
                Fill (myds);
            Close ();
            ds = myDS;
        Return }
        
    }
}

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.