The .NET replaces the previous ADO in ADO.NET. How to apply ADO in .NET how to apply ADO in .NET? I have been exploited, I found that ADO in .NET can be used as ADO, and it is very easy to use. Now combined with Visual Basic.Net powerful class development capabilities, I developed ADO into components, and the effect is ideal. The way I use is as follows:
Start Visual Studio.net. Select the Visual Basic project in the new project, select the class library in the template, enter the class library name in the name, such as DataAccess. Once, enter the class library development environment, rewrite the class1 to adoaccess.
Add a reference to the project menu, select the COM page, find Microsoft ActionX Data Object 20 Library or higher. The COM option is Microsoft to distinguish the current .NET, compatible with previous development methods, all components before .NET can be found in the COM page.
Write the IMPORTS AdoDb.connection; imports adoDb.recordset; imports adoDb.recordset; imports adodb.cursorlocationenum; imports system.dbnull 4 reference statements, hereby reference ADO, ADO macro definition, setting a source of null values.
Define a partial connection object variable in the class. Private mcnndb as new adoDb.connection () then defines the process of connecting to the Access database. Access database is specified by aduseclient, access the database through Microsoft.jet.OleDb data access, Microsoft.jet.Oledb has a variety of versions, where 4.0 is the highest. Version, it can access Access 2000, so the data provider here is specified as Microsoft.jet.OleDb.4.0. After you specify the local database and the provider, you can open a database, and implement it with the OPEN method. The complete code is as follows:
'Effect: Access database connection' parameters: DBName database name Public Sub ConnAccess (ByVal DBName As String) Dim strDB As String mCnnDB.CursorLocation = adUseClient mCnnDB.Provider = "Microsoft.Jet.OLEDB.4.0" mCnnDB.Open (DBName, " ")") End Sub
Set DNS in ODBC, specify the username and password in the following Connectionstring, and finally open with the open method. Since the ODBC is divided by the username and no username, we must implement it separately, with a function name overload function, we write two synonyms, the complete code is as follows:
'Role: Connect ODBC Database (Do not specify user and password)' parameters: dsnname is ODBC Name Public Sub ConnoDBC (Byval DSNName As String) mcnndb.Provider = "MSDasql" mcnndb.connectionstring = "data source = '" & dsnname & Data Source =' "'" Mcnndb.open () end sub' effect: Connect the ODBC database (you need to specify the user and password) 'parameter: dsnname odbc name, userid user name, userpwd user password public sub connodbc (Byval DSNName As String, Byval userid As String Byval userPwd as string) mcnndb.provider = "msdasql" mcnndb.connectionstring = "data source = '" & dsnname & "' user id = '" & userid & ""; "& _" password =' "& userpwd & "Mcnndb.open () End Sub