Methods for connecting to the database using VB. NET
Microsoft Visual Basic. NET is an updated version of Microsoft Visual Basic, which is based on. NET framework structure, so that users can easily create Microsoft Windows operating system and network applications.
Visual Basic. NET enables Visual development of network applications, network services, Windows applications, and server components. In addition, Visual Basic. NET provides XCOPY deployment for Windows applications, so developers no longer need to worry about the DLL version. Visual Basic has now become an object-oriented language and a language that supports inheritance. The Form Designer supports visual inheritance and contains many new features, such as automatic modification of the form size, resource localization, and accessibility support. Data tools support XML data internally. Data is bound to disconnected data during design. In addition, Visual Basic. NET is directly built on. . NET Framework Structure, so developers can make full use of all platform features, but also with other. . NET language interaction.
In this article, I will introduce Visual Basic. NET database programming and related knowledge.
1. First introduce ADO. NET and ODBC. NET
ADO. NET is improved by Microsoft ActiveX Data Objects (ADO). It provides the platform's interworking and shrinking Data access functions, and is an important tool used by Visual Basic. NET 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. . NET Framework. However, in. NET FrameWork SDK1.0 does not contain ODBC. NET. To Use ODBC. NET, go to the Microsoft website to download it. For details: http: // msdn.microsoft.com/library/default.asp? Url =/downloads/list/netdevframework. asp (the file name is odbc_net.msi). By default, the installation path is "C: Program FileMicrosoft.netodbc.net ". The installed component is named Microsoft. Data. Odbc. dll.
To add ODBC. NET Data Provider, follow these steps:
Start Visual Basic. NET development environment, select [tools]-> [data]-> [custom toolbox] in the menu bar, and select [. net framework component] Click the [browse] button and select Microsoft under the "C: Program FileMicrosoft.netodbc.net" directory. data. odbc. dll file. Next, select "OdbcCommand", "OdbcCommandBuilder", "OdbcConnection", and "OdbcDataApdater" in the [custom toolbox], and click [OK]. at this point. add ODBC to. NET. NET.
Ii. Next we will introduce the Data Provider)
ADO.. NET and ODBC. NET provides three data providers, among which ADO. NET provides two (The SQL Server. NET Data Provider and The ole db. NET Data Provider) ODBC. NET provides a (The ODBC. NET Data Provider ). Table 1 contains three data providers and their supported databases:
Data Provider)
Supported Databases
1. The SQL Server. NET Data Provider only supports SQL Server or later.
2. The ole db. NET Data Provider Access, Oracle and SQL Server
3. The ODBC. NET Data Provider Access, Oracle, SQL Server, MySql, VFP, etc.
Table 1 three data providers and their supported Databases
3. Next we will introduce the above data providers to connect to various databases.
1. Use The SQL Server. NET Data Provider to connect to The database
The SQL Server. NET Data Provider uses The SqlConnection class to connect to a database of SQL Server or later versions,
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 catconnect = database name; user id = sa; password = ;"
SqlConnection1 = New System. Data. SqlClient. SqlConnection (strConnect)
Sqlconnection1.open' open the database [Page]
SqlConnection1.close 'Close the connection and release resources
2. Use The ole db. NET Data Provider to connect to The database
As mentioned above, The ole db. NET Data Provider can be used to Access, Oracle, SQL Server, and other types of Data.
Database, how does it access these databases? The ole db. NET Data Provider is located in The namespace Sy
The OleDbConnection class under the stem. Data. OleDb class library connects these three different types of databases. The following is an example:
1) connect to 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 to the Access Database
Assume that the Access database to be connected is named "Example. mdb" and is 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 to the 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 connects to The database through The OdbcConnection class, which is located in The namespace