C # access connection

Source: Internet
Author: User

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 ;}} // } // 

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.