Basic Database Operation Methods

Source: Internet
Author: User

1. Obtain database connection and Basic Information

1. Define an Interface

 
Namespace com. mycompany. Data {public interface idbsetting {string connectionstring {Get;} dbservertype {Get;} string coId {Get;} string userno {Get ;}............ }}
2 Inheritance Interface
Namespace COM. mycompany. web. webui {public class mydbsetting: idbsetting {public mydbsetting (string language, userinformation user) {}/// <summary> /// data connection string /// </Summary> Public String connectionstring {get {If (httpcontext. current. application ["connectionstring"] = NULL) {If (configurationmanager. connectionstrings ["conncetion"] = NULL) {Throw new exception (kdcommon. dataconnectioninfo );} Else {httpcontext. current. application ["connectionstring"] = configurationmanager. connectionstrings ["conncetion"]. tostring () ;}} return httpcontext. current. application ["connectionstring"]. tostring () ;}/// <summary> /// Database Type // </Summary> Public dbservertype {get {return dbservertype. sqlserver ;}//< summary> /// current company coId /// </Summary> Public String coId {get {sessiontim Eout ("coId"); Return httpcontext. current. session ["coId"]. tostring () ;}} Public String userno {get {sessiontimeout ("loginuserno"); Return httpcontext. current. session ["loginuserno"]. tostring ();}}............ }}
3. encapsulation Interface
 
Namespace COM. mycompany. common {public class baseinfo {Private Static idbsetting s_dbsetting = NULL; public static idbsetting dbsetting {get {return s_dbsetting;} set {baseinfo. s_dbsetting = value ;}} public baseinfo (){}}............}
 
 
Ii. Basic Database Operations // <summary>
/// Access the database object (auxiliary tool)
/// </Summary>
/// <Remarks> </remarks>
Public sealed class dbutils 1. Create a connection

/// <Summary> /// data connection string /// </Summary> Public static string connectionstring {get {If (_ connectionstring = NULL | _ connectionstring. length = 0) {return baseinfo. dbsetting. connectionstring ;}else {return _ connectionstring ;}} set {_ connectionstring = value ;}} /// <summary> /// data server type /// </Summary> Public static dbservertype {get {return baseinfo. dbsetting. dbservertype ;}/// <summary> // product ID /// </Summary> Public static string coId {get {return baseinfo. dbsetting. coId ;}/// <summary> /// user ID /// </Summary> Public static string userno {get {return baseinfo. dbsetting. userno ;}}............
 
 

3. Execute the SELECT query statement.

/// <Summary> /// execute the query command /// </Summary> /// <Param name = "commandstring"> SQL command </param> /// <param name = "datareader"> output datareader </param> Public static void runselectcommand (string connectionstring, string commandstring, out idatareader datareader) {idbcommand _ command; oledbconnection conn = getconnection (connectionstring) ;__ command = new oledbcommand (commandstring, Conn) ;__ command. commandtimeout = 0; // set The waiting time for command execution is indefinite if (_ command. Connection. State! = Connectionstate. open) {__ command. connection. open ();} datareader = _ command. executereader (system. data. commandbehavior. closeconnection) ;__ command. dispose (); // release _ command} // <summary> // transfer the connection object for creating a temporary table, you cannot create a new connection object each time (otherwise, the temporary table is automatically deleted after each SQL statement is executed) /// </Summary> /// <Param name = "commandstring"> SQL command </param> /// <Param name = "datatable"> output data table </Param> /// <Param name = "prams"> query the command parameter list </param> Public static void Runselectcommand (oledbconnection Conn, string commandstring, out datatable, Params idbdataparameter [] prams) {idbdataadapter _ dataadapter; dataset _ dataset; _ dataadapter = new oledbdataadapter (commandstring, conn); _ dataadapter. selectcommand. commandtimeout = 0; // set the waiting time for command execution to be indefinite if (prams! = NULL) {foreach (idbdataparameter pram in prams) {// If (pram. dbtype = dbtype. string) // {// pram. value = replacesqlescape (pram. value); //} _ dataadapter. selectcommand. parameters. add (PRAM) ;}}_ _ dataset = new dataset (); try {_ dataadapter. fill (_ dataset); datatable = _ dataset. tables [0];} catch (exception e) {Throw new exception (commandstring, e );}}

4. Execute the stored procedure.

# Region runstoredprocedure without out datatablepublic static void runstoredprocedure (string connstring, string procname) {using (oledbconnection conn = getconnection (connstring) {try {idbcommand command = Conn. createcommand (); command. commandtext = procname; command. commandtype = commandtype. storedprocedure; command. commandtimeout = 0; // set the waiting time for command execution to an indefinite command. executenonquery (); command. dispose ();} catch (ex Ception ex) {Throw new exception (procname + ":" + ex. message);} finally {Conn. dispose () ;}} public static void runstoredprocedure (string connstring, string procname, Params idbdataparameter [] paramsarray) {using (oledbconnection conn = getconnection (connstring )) {try {idbcommand command = Conn. createcommand (); command. commandtext = procname; command. commandtype = commandtype. storedprocedure; command. commandtim Eout = 0; // set the waiting time for command execution to the indefinite foreach (idbdataparameter Param in paramsarray) {command. parameters. add (PARAM);} command. executenonquery (); command. dispose ();} catch (exception ex) {Throw new exception (procname + ":" + ex. message);} finally {Conn. dispose () ;}}# endregion # region runstoredprocedure with out datatablepublic static void runstoredprocedure (string connstring, string procname, out datatable) {Ole Dbdataadapter adapter; dataset; using (oledbconnection conn = getconnection (connstring) {try {dataset = new dataset (); idbcommand command = Conn. createcommand (); command. commandtext = procname; command. commandtype = commandtype. storedprocedure; command. commandtimeout = 0; // set the waiting time for command execution to an indefinite adapter = new oledbdataadapter (oledbcommand) Command); adapter. fill (Dataset); adapter. dispose (); command. dispose (); Datatable = dataset. tables [0];} catch (exception ex) {Throw new exception (procname + ":" + ex. message);} finally {Conn. dispose () ;}} public static void runstoredprocedure (string connstring, string procname, out datatable, Params idbdataparameter [] paramsarray) {oledbdataadapter adapter; dataset; able = NULL; using (oledbconnection conn = getconnection (connstring) {try {dataset = new data Set (); idbcommand command = Conn. createcommand (); command. commandtext = procname; command. commandtype = commandtype. storedprocedure; command. commandtimeout = 0; // set the waiting time for command execution to the indefinite foreach (idbdataparameter Param in paramsarray) {command. parameters. add (PARAM);} adapter = new oledbdataadapter (oledbcommand) Command); adapter. fill (Dataset); adapter. dispose (); command. dispose (); If (Dataset! = NULL & dataset. tables. count> 0) {datatable = dataset. tables [0] ;}} catch (exception ex) {Throw new exception (procname + ":" + ex. message);} finally {Conn. dispose () ;}}# endregion
 

5. Execute the SQL statement and return the affected data rows.

// Return value indicates the number of records affected by the operation. Public static int runcommand (string connectionstring, string commandstring, Params idbdataparameter [] prams) {idbcommand _ command; int _ rtnrowcount =-1; using (oledbconnection conn = getconnection (connectionstring) {__ command = new oledbcommand (commandstring, Conn) ;__ command. commandtimeout = 0; // set the waiting time for command execution to be indefinite if (prams! = NULL) {foreach (idbdataparameter pram in prams) {__ command. Parameters. Add (PRAM) ;}} if (_ command. Connection. State! = Connectionstate. open) {__ command. connection. open ();} _ rtnrowcount = _ command. executenonquery () ;__ command. dispose ();} return _ rtnrowcount;} // <summary> // transmits the connection object for creating a temporary table, you cannot create a new connection object each time (otherwise, the temporary table is automatically deleted after each SQL statement is executed) /// </Summary> /// <Param name = "conn"> </param> /// <Param name = "commandstring"> </param> /// <Param name = "prams"> </param> Public static int runcommand (oledbconnection Conn, string commandstr ING, Params idbdataparameter [] prams) {idbcommand _ command; int _ rtnrowcount =-1; _ command = new oledbcommand (commandstring, Conn); _ command. commandtimeout = 0; // set the waiting time for command execution to be indefinite if (prams! = NULL) {foreach (idbdataparameter pram in prams) {_ command. Parameters. Add (PRAM) ;}} if (_ command. Connection. State! = Connectionstate. open) {_ command. connection. open () ;}_ _ rtnrowcount =__ command. executenonquery (); _ command. parameters. clear (); Return _ rtnrowcount ;}
 

 

 

 

 

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.