Visual Basic.NET connects several ways to various databases
Microsoft Visual Basic.net is an updated version of Microsoft Visual Basic. It is based on the .NET's framework structure that enables users to create a Microsoft Windows operating system and network applications. Using Visual Basic.NET, you can quickly implement visual development network applications, network services, Windows applications, and server-side components. In addition, Visual Basic.net provides Xcopy deployment for Windows applications, developers no longer need to worry about the version of the DLL. Visual Basic has now truly object-oriented and supporting inheritance. The form designer supports visual inheritance and includes many new features, such as automatically changing form sizes, resource localization, and accessibility support. Data type tools are in support XML data, and the data is bound to be disconnected when designing. In addition, Visual Basic.NET is built directly on the .NET framework structure, so developers can make full use of all platform features, or other .NET language interaction. In this article, I have to talk to you, introducing database programming and related knowledge of Visual Basic.net.
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.SQLCONNECTION DIM 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 = "provike = 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 = "proviker = 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.odbcConnection Dim 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.