Ado. Net contains two types of libraries:
- Sqloledb
- SQL
ProgramInterface
Program list
1 Imports System. Data
2 Imports System. Data. oledb
3 Imports System. Data. sqlclient
4 Public Class Form1
5 Dim Strconnect As String = " Provider = sqloledb.1; Integrated Security = sspi; persist Security info = false; initial catalog = lztest; Data Source = SVCTAG-4T7582X " ' Using udl files to write connection strings
6
7 Private Sub Button#click ( Byval Sender As System. object, Byval E As System. eventargs) Handles Button1.click
8 ' Using sqloledb
9 ' The connection object is an oledbconnection object.
10 ' The command object is an oledbcommand object.
11 ' The datareader object is an oledbdatareader object.
12 Dim Myconnect As New Oledbconnection ()
13 Myconnect. connectionstring = Strconnect
14 Try
15 Myconnect. open () ' Open Database
16 Dim Mycommand As New Oledbcommand ( " Select * from employees " , Myconnect) ' Open Table
17 Dim Mydatareader As Oledbdatareader
18 Mydatareader = Mycommand. executereader ()
19 Listbox1.items. Clear ()
20 While (Mydatareader. Read ())
21 Listbox1.items. Add (mydatareader. getstring ( 1 )) ' PS: The name column is the second column, and the column number starts from 0. Therefore, enter 1 here.
22 End While
23 Catch Ex As Exception
24 Msgbox (EX. tostring (), msgboxstyle. abortretryignore, " Exception " )
25 Finally
26 If Myconnect. State = Connectionstate. Open Then
27 Myconnect. Close ()
28 End If
29 End Try
30 End sub
31
32 Dim Strconn2 As String = " Server = SVCTAG-4T7582X; Integrated Security = sspi; persist Security info = false; database = lztest "
33
34 Private Sub Button2_click ( Byval Sender As System. object, Byval E As System. eventargs) Handles Button2.click
35 ' To use the sqlconnection class library, you must first introduce system. Data. sqlclient
36 ' PS: The provider value is not required for the connection string, and other keywords are different.
37 ' The connection object is a sqlconnection object.
38 ' The command object is a sqlcommand object.
39 ' The datareader object is a sqldatareader object.
40 Dim Myconn As Sqlconnection = New Sqlconnection ()
41 Myconn. connectionstring = Strconn2
42 Try
43 Myconn. open ()
44 Dim Command1 As Sqlcommand = New Sqlcommand ( " Select * from employees " , Myconn)
45 Dim Datareader1 As Sqldatareader
46 Datareader1 = Command1.executereader ()
47 Listbox1.items. Clear ()
48 While (Datareader1.read ())
49 Listbox1.items. Add (datareader1.getvalue ( 1 ). Tostring ())
50 End While
51 Catch Ex As Exception
52 Msgbox (EX. tostring ())
53 Finally
54 If Myconn. State = Connectionstate. Open Then
55 Myconn. Close ()
56 End If
57 End Try
58 End sub
59 End Class
60