VB.NET connects several ways to various databases

xiaoxiao2021-03-06  89

First, first introduce ADO.NET and ODBC.NET

ADO .NET is improved by Microsoft ActiveX Data Objects (ADO), it provides platform interoperability and contraction data access, which is an important tool for database programming for database programming. ADO.NET uses some ADO objects, such as Connection and Command objects, and introduces new objects. The main new ADO.NET objects include DataSet, DataReader, and DataAdapter.

ODBC.NET is another very useful class library for database development in the .NET framework. However, in the .NET Framework SDK1.0 version does not contain odbc.net, please use ODBC.NET to Microsoft website to download, specific download address: http://msdn.microsoft.com/library/default.asp? Url = /downloads/list/netdevframework.asp (file name is ODBC_NET.MSI) By default, the installation path is "c: / program file / microsoft.net / odbc.net". The installed component is named Microsoft.Data.odbc.dll file.

Steps to add ODBC .NET DATA Provider:

Start the Visual Basic.NET development environment, select [Tools] -> [Data] -> [Custom Toolbox] in the menu bar, select [.NET Framework Components] in the pop-up [.NET Framework component] click [ Browse] button, select the Microsoft.Data.odbc.dll file in the "C: / Program File / Microsoft.net / ODBC.NET" directory. Next, "ODBCCOMMAND", "ODBCCCMMAMMAILDER", "ODBCCONNECTION", "ODBCDataApdater" are selected in [Custom Toolbox], click [OK]. At this point, the ODBC .NET is added to Visual Basic.NET.

Second, the next introduction of the data provider (Data Provider)

ADO.NET and ODBC.NET provide three data providers, where ADO.NET provides two (the OLE DB .NET DATA Provider) ODBC .NET provides one (the OLE DB .NET DATA Provider) ODBC .NET DATA Provider. Table 1 is three data providers and their supported databases:

Data Provider

Supported database

I

The SQL Server .NET Data Provider

Support for SQL Server7.0 or higher

Ii

The OLE DB .NET DATA Provider

Access, Oracle and SQL Server, etc.

Iii

The ODBC .NET DATA Provider

Access, Oracle, SQL Server, MySQL, VFP, etc.

Table 1 Three data providers and their supported databases

Third, next, introduce the above data providers to connect various databases.

1, connect the database with the SQL Server .NET Data Provider

The SQL Server .NET Data Provider is a database that uses the SQLConnection class to connect SQL Server7.0 or higher.

The SQLConnection class is located under the namespace system.data.sqlclient.

Connection code:

DIM SQLCONNECTION1 AS SQLCLIENT.SQLCONNECTIONDIM STRCONNECT AS STRING = "Data Source = server name; Initial Catalog = Database name; user ID = sa; password =;"

SqlConnection1 = new system.data.sqlclient.sqlConnection (StrConnect)

SqlConnection1.open 'Opens Database

SqlConnection1.close 'Close connection, release resources

2, connect the database with the OLE DB .NET DATA Provider

Top above, using the OLE DB .NET Data Provider to access Access, Oracle, and SQL Server, etc.

So, how do it access these databases? The OLE DB .NET DATA Provider is through the namespace SY

The OLEDBConnection class under the STEM.Data.OleDb class is to connect to these three different types of databases. Below examples:

1) Connect the SQL Server database

DIM OLEDBCONNECTION1 AS OLEDB.OLEDBCONNECTION

Dim strconnect as sting = "provider = SQLOLEDB; PERSIST security info = false; data source = server name; Initial catalog = database name; user ID = sa; password =;"

OLEDBCONNECTION1 = new system.data.oledb.oledbconnection (StrConnect)

2) Connect the Access database

Suppose you want to connect the Access database name "eXample.mdb", stored in the D: / DATA / directory.

DIM OLEDBCONNECTION1 AS OLEDB.OLEDBCONNECTION

DIM STRCONNECT AS STING = "provider = microsoft.jet.Oledb.4.0; data source = d: / data / example.mdb"

OLEDBCONNECTION1 = new system.data.oledb.oledbconnection (StrConnect)

3) Connect Oracle Database

DIM OLEDBCONNECTION1 AS OLEDB.OLEDBCONNECTION

Dim strconnect as sting = "provider = msdaora; data source = server name; user ID = user ID; password = password;"

OLEDBCONNECTION1 = new system.data.oledb.oledbconnection (StrConnect)

3, use the ODBC .NET DATA Provider to connect to the database

The ODBC .NET DATA Provider connection database is implemented by the ODBCConnection class, this class is in the namespace

Microsoft.Data.odbc, and the namespace Microsoft.Data.odbc is encapsulated under the microsoft.data.odbc.dll file.

Due to limited space, only the method of connecting SQL Server and Oracle databases, the basic class of connection methods for other databases

I don't talk more.

1) Connect the SQL Server database

DIM ODBCDBCONNETION1 AS Microsoft.Data.odbcConnectionDim strconnect as sting = "driver = {sql server}; server = server name; uid = sa; pwd =; database = database name;"

odbcdbconnetic1 = new microsoft.data.odbcConnection (StrConnect)

2) Connect Oracle Database

Dim OdbcdbConNetion1 as Microsoft.Data.odbcConnection

DIM STRCONNECT AS STING = "driver = {Microsoft Odbc for oracle}; server = server name; UID = sa; pwd =;"

odbcdbconnetic1 = new microsoft.data.odbcConnection (StrConnect)

Fourth, summary

Through this article, the reader basically masters the method of connecting various databases in ADO.NET and ODBC.NET in Visual Basic.net.

. The above three driving directions are different for different databases: the performance of their performance is also different: The SQL Server .NET DATA Provider

The highest efficiency; the efficiency of the OLE DB .NET DATA Provider; the ODBC .NET DATA Provider is the slowest.

Which data drive is used to connect to which data driver is considered from work efficiency.

转载请注明原文地址:https://www.9cbs.com/read-108506.html

New Post(0)