The motivation I wrote this program is that when I want to access the Msaccess database with C Sharp, I have no way to get any information and reference materials. All materials available online are called SQL, so we will be divided into two steps. This app, first we will show how to connect to the Msaccess database and see how complicated it. Finally, we have completed this program.
Leisure is less oriented, let us start the topic. The process of connecting to the database has undergone great changes than the previous ADO connection process. The following chart is appropriate (I hope) OLEDBCONNECTION -> OLEDBCommand -> OLEDBDATAREADER. Now those who are familiar with ADO can see the similarities between the two but to make those who have no good adaptive ADOs understand, the following is some explanations.
OLEDBConnection -> represents a single connection of the database, which can give you the ability to manipulate the database according to the function of the underlying database. One thing must be remembered, although the OLEDBConnection object has a scope of action, it will not be shut down automatically. So, you The Close () method of calling this object will have to be displayed.
OLEDBCOMMAND -> This is like the same usual Command object as we used in ADO. You can call the SQL stored procedure or SQL query statement through this object.
OLEDBDATAREADER -> This class has a very important importance because it provides an actual access to the bottom of the database. When you call the OLEDBCOMMAND EXECUTEREADEREADER method, it will be created, .NET Beta2 SDK said not to create directly This class object.
Now you can see more about these primary objects in the documentation of .NET Beta 2, which is to point out how to access the source code of the database in the program.
Using system.data.OLEDB;
Class oledbtest {
Public static void main () {// Create a database connection OLEDBCONNECTION ACONNECTION = New OLEDBCONNECTION ("provider = microsoft.jet.OleDb.4.0; data source = c: //db1.mdb");
// Create a Command object and save the SQL query statement oledbcommand acommand = new oledbcommand ("select * from emp_test", aconnection; "Try {aconnection.open ();
// Create a DataReader object to connect to the form OLEDBDATAREADER AREADER = Acommand.executeReader (); console.writeline ("this is the return data from emp_test table);
// Cycle traversal database while (areader.read ()) {console.writeline (Areader.Getint32 (0) .tostring ());}
// Turn off the Reader object aReader.close ();
// Turn off the connection, this is very important aconnection.close ();
// Some usual abnormal processes Catch (OLEDBEXCEPTION E) {Console.Writeline ("Error: {0}", E.Errors [0] .Message);}}}
Steps to successfully run this program 1. Create a database named DB1.mdb with MsAccess 2. Create a form called Emp_Test 3. Make it contain the following data field EMP_CODE INT EMP_NAME TEXT EMP_EXT TEXT 4. Save the above code to 5. 5. Make sure the database is in C: / and make sure that MDAC2.6 or updated version has been installed 6. Compile operation
Let us now understand some details of what we see in the constructor of the OLEDBConnection object, here you see something such as "provider =". Below is some driver types compatible with ADO.NET. SQLoldDB - -> Microsoft Ole DB Provider for SQL Server, MSDara -> Microsoft Ole DB Provider for Oracle, Microsoft.jet.Oledb.4.0 -> Ole DB Provider for Microsoft Jet You can choose any one of them but they will need to pass different Parameters, such as JET.OLDB. Need to pass the name of the MDB file and SQLOLDB needs to pass the username and password. All of these drivers are in the System.Data.oledb namespace, so you must include it, and other and OLEDB Provider for ODBC Not compatible, that is, you can't use these drivers in the VB6.0 program to access the database, so don't find out why they want to put these databases in C: /.
When you use Microsoft SQL Server 7.0 or update, the following is some guidelines given by Microsoft: Recommended in .NET Data Provider in the following cases, use Microsoft Data to use Microsoft Data Engine (MSDE) or iCrosoft SQL Server 7.0 or updated single-layer application. It is recommended to use Ole DB Provider for SQL Server (SQLOLEDB) and OLE DB .NET DATA Provider. For Microsoft SQL Server 6.5 and earlier versions, You must use the OLE DB.NET Data Provider and Ole DB.Net Data Provider. It is recommended to use Microsoft SQL Server 6.5 and earlier versions or Oracle's intermediates to use OLE DB .NET DATA Provider. For Microsoft SQL Server 7.0 Or update version, recommend SQL Server .NET Data Provider. Recommended single-layer applications use Microsoft Access databases. Do not recommend an intermediate layer program simultaneously using OLE DB .NET Data Provider and Microsoft Access databases. No longer support OLE DB Provider for ODBC MSDASQL)