C # language is still a more common thing, here we mainly introduce C # implementation of database access, including the main role of the introduction program.
Visual C # is Microsoft's highly recommended next-generation program development language, and he has a very important companion--. Net Framework SDK, which encapsulates many class libraries in his. Visual C # To achieve a lot of expansion capabilities, you must rely on his partner. Processing the database in Visual C # is an important manifestation of its functionality. When Visual C # is doing database processing, a namespace that is frequently used in the. Net FrameWork SDK is System.Data.Oledb. This namespace encapsulates a number of related class (classes) for database processing. This article uses two concrete examples to illustrate how C # implements access to the database.
A The programming and operating environment is:
Microsoft Windows 2000 Professional Edition,. Net FrameWork SDK BETA 2, Microsoft Access Data Component 2.6 (MADC2.6)
Two The main function of the program
This article mainly has two source code, one explains how C # realizes to the database access, the local database chooses Microsoft Company's Acess 2000; the second shows how to access the database in Visual C #, and the remote database chooses Microsoft's products--sql Server 7.0.
Three Thinking in the process of example design
1. First to import namespaces
2. Establish a data connection to the database
3. On this data connection, create an SQL statement to return the required dataset
4. Open the data connection, execute the SQL statement, return the desired dataset
5. Close data set, close data connection
Four First example First.cs--Open a local database (My.mdb)
Important steps in the program:
1. Import namespace Space
In this example, three namespaces are imported, namely System, System.Data.OleDb, System.Windows.Forms. The System namespace is imported because of the console class in which the program is used. The System.Windows.Forms namespace is imported because the application class is used in the program.
2. Establish a connection to the database
Establishing a connection uses the class--oledbconnection in the System.Data.OleDb namespace. You can complete the connection to the local database by using the following statement:
string strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Application.StartupPath + "\\my.mdb" ;
OleDbConnection aConnection = new OleDbConnection ( strConnect ) ;