C #. net access Operations

Source: Internet
Author: User
C #. net access operation class 1. Configure the web. config file: configure the database connection parameter configurationappSettingsconnectionStringsaddnameConnectionStringconnectionStringProviderMicrosoft. Jet. OLEDB.4.0; performancef: TeacherSystemApp_Datadb.mdb;

C #. net connection access operation class 1. Configure web. config File: configure the database connection parameter configuration appSettings/connectionStrings add name = "ConnectionString" connectionString = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = F: \ TeacherSystem \ App_Data \ db. mdb; Jet O

C #. net access Operations

1. Configure the web. config file: Configure database connection Parameters




ProviderName = "System. Data. OleDb"/>

2
Program Design starts:

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. cs code

Using System;
Using System. Data;
Using System. Data. OleDb;
Using System. Web;
Using System. Web. UI;
Using System. Configuration;

Namespace Com. LXJ. Database
{
///


/// Conn summary.
///
Public class ConnDbForAcccess
{
///
/// Connect to the database string
///
Private string connectionString;

///


/// Storage database connection (protection class, which can only be accessed by classes derived from it)
///
Protected OleDbConnection Connection;

///


/// Constructor: default database connection
///
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. etettings ["ConnectionString"]. ToString ();
//
Connection = new OleDbConnection (connectionString );
}

///


/// Constructor: database connection with Parameters
///
///
Public ConnDbForAcccess (string newConnectionString)
{
// ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + HttpContext. Current. Request. PhysicalApplicationPath + newConnectionString;
ConnectionString = newConnectionString;
Connection = new OleDbConnection (connectionString );
}

///


/// Obtain the connection string
///
Public string ConnectionString
{
Get
{
Return connectionString;
}
}

///


/// No results are returned when an SQL statement is executed, such as deletion, update, and insertion.
///
///
/// Operation success mark
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;
}

///


/// Execute the SQL statement and return the result to DataReader.
///
///
/// DataReader
Private OleDbDataReader ReturnDataReader (string strSQL)
{
Connection. Open ();
OleDbCommand command = new OleDbCommand (strSQL, Connection );
OleDbDataReader dataReader = command. ExecuteReader ();
Connection. Close ();

Return dataReader;
}

///


/// Execute the SQL statement and return the result to DataSet.
///
///
/// DataSet
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;
}

///


/// Execute a query statement and return the number of query results
///
///
/// SqlResultCount
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;
}

}//
}//

All right, the public classes for database access and database operations have been completed. Let's take a look at the code meanings. These are C # syntax knowledge. Now we can compile and generate projects, compile the project dll file Com. LXJ. the dll is copied to the bin directory of the Example project, and Com. LXJ. dll. I will introduce how to use it later. Take a break and have a mouthful of water .....

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.