C # access connection operation [transfer, organize]
--------------------------------------------------------------------------------
1. Configure the Web. config file: Configure database connection Parameters
<Configuration> <etettings/> <connectionstrings> <Add name ="Connectionstring"Connectionstring ="Provider = Microsoft. Jet. oledb.4.0; Data Source = F: \ teachersystem \ app_data \ dB. mdb; Jet oledb: Database Password = 123"Providername ="System. Data. oledb"/> </Connectionstrings>
2,ProgramDesign start:
1. Create access databases and data tables as required
2. Write a public class for database access and operations. This class can be reused in any system developed in the future.
(1) create a C # class library project named "com. lxj" and set the project properties: The Assembly name and default namespace are "com. lxj"
(2) Create the database directory under the project directory, and create the C # class file conndbforaccess. CS under the database directory.
Add reference: system. Web. dll
(3) Compile the conndbforaccess. CSCode
Using System; Using System. Data; Using System. Data. oledb; Using System. Web; Using System. Web. UI; Using System. configuration; Namespace Com. lxj. Database { /// <Summary> /// Conn summary. /// </Summary> Public class Conndbforacccess { /// <Summary> /// Database connection string /// </Summary> Private string Connectionstring; /// <Summary> /// Storage database connection (protection class, which can only be accessed by classes derived from it) /// </Summary> Protected Oledbconnection Connection;/// <Summary> /// Constructor: default database connection /// </Summary> Public Conndbforacccess (){ String Connstr; connstr = configurationmanager. connectionstrings [ "Connectionstring" ]. Connectionstring. tostring (); // Connstr = system. configuration. configurationsettings. etettings ["connectionstring"]. tostring (); // read from web. config Configuration Connectionstring = connstr;// Connectionstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ httpcontext. current. request. physicalapplicationpath + connstr; // connectionstring = system. configuration. configurationsettings. appsettings ["connectionstring"]. tostring ();// Connection = New Oledbconnection (Connectionstring );} /// <Summary> /// Constructor: database connection with Parameters /// </Summary> /// <Param name = "newconnectionstring"> </param> Public Conndbforacccess ( String Newconnectionstring ){ // Connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + httpcontext. Current. Request. physicalapplicationpath + newconnectionstring; Connectionstring = newconnectionstring; connection = New Oledbconnection (Connectionstring );} /// <Summary> /// Obtain the connection string /// </Summary> Public String Connectionstring { Get { Return Connectionstring ;}} /// <Summary> /// No results are returned when an SQL statement is executed, such as deletion, update, and insertion. /// </Summary> /// <Param name = "strsql"> </param> /// <returns> Operation success mark </Returns> Public bool Exesql ( String Strsql ){ Bool Resultstate = False ; Connection. open (); Oledbtransaction Mytrans = connection. begintransaction ();Oledbcommand Command = New Oledbcommand (Strsql, connection, mytrans ); Try {Command. executenonquery (); mytrans. Commit (); resultstate = True ;} Catch {Mytrans. rollback (); resultstate = False ;} Finally {Connection. Close ();} Return Resultstate ;} /// <Summary> /// Execute the SQL statement and return the result to datareader. /// </Summary> /// <Param name = "strsql"> </param> /// <returns> Datareader </Returns> Private Oledbdatareader Returndatareader ( String Strsql) {connection. open (); Oledbcommand Command = New Oledbcommand (Strsql, connection ); Oledbdatareader Datareader = command. executereader (); connection. Close (); Return Datareader ;} /// <Summary> /// Execute the SQL statement and return the result to dataset. /// </Summary> /// <Param name = "strsql"> </param> /// <returns> Dataset </Returns> Public Dataset Returndataset ( String Strsql) {connection. open ();Dataset Dataset = New Dataset (); Oledbdataadapter Oledbda = New Oledbdataadapter (Strsql, connection); oledbda. Fill (dataset, "Objdataset" ); Connection. Close (); Return Dataset ;} /// <Summary> /// Execute a query statement and return the number of query results /// </Summary> /// <Param name = "strsql"> </param> /// <returns> Sqlresultcount </Returns> Public int Returnsqlresultcount ( String Strsql ){ Int Sqlresultcount = 0; Try {Connection. open (); Oledbcommand Command = New Oledbcommand (Strsql, connection );Oledbdatareader Datareader = command. executereader (); While (Datareader. Read () {sqlresultcount ++;} datareader. Close ();} Catch {Sqlresultcount = 0 ;} Finally {Connection. Close ();} Return Sqlresultcount ;}} // } //