Database-simple instance using datareader (two methods)

Source: Internet
Author: User

Ado. Net contains two types of libraries:

    1. Sqloledb
    2. 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

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.