Source code The following example shows what a simple ado.net application that connects to the Northwind database and returns a Li St of Categories would look like. The example writes the output to the console, or command prompt.<br>
<br>
The following example shows what a simple ado.net application which connects to the Northwind database and returns a list O F Categories. The example writes the output to the console, or command prompt.<br>
<br>
Sqlclient<br>
[Visual basic]<br>
Imports system<br>
Imports system.data<br>
Imports system.data.sqlclient<br>
Imports microsoft.visualbasic<br>
<br>
Public Class sample<br>
<br>
Public Shared Sub Main () <br>
Dim Nwindconn As SqlConnection = New SqlConnection ("Data source=localhost;" & _<br>
"User Id=sa; Password=pwd;initial catalog=northwind ") <br>
<br>
Dim catcmd as SqlCommand = Nwindconn.createcommand () <br>
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories" <br>
<br>
Nwindconn.open () <br>
<br>
Dim myreader As SqlDataReader = Catcmd.executereader () <br>
<br>
Do While Myreader.read () <br>
Console.WriteLine (VbTab & "{0}" & VbTab & "{1}", Myreader.getint32 (0), myreader.getstring (1)) <br>
Loop<br>
<br>
Myreader.close () <br>
Nwindconn.close () <br>
End Sub<br>
End Class<br>
[c#]<br>
Using system;<br>
Using system.data;<br>
Using system.data.sqlclient;<br>
<br>
Class sample<br>
{<br>
public static void Main () <br>
{<br>
SqlConnection nwindconn = new SqlConnection ("Data source=localhost; User Id=sa; Password=pwd;initial catalog=northwind ");<br>
<br>
SqlCommand catcmd = Nwindconn.createcommand ();<br>
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories";<br>
<br>
Nwindconn.open ();<br>
<br>
SqlDataReader myreader = Catcmd.executereader ();<br>
<br>
while (Myreader.read ()) <br>
{<br>
Console.WriteLine ("\t{0}\t{1}", Myreader.getint32 (0), myreader.getstring (1));<br>
}<br>
<br>
Myreader.close ();<br>
Nwindconn.close ();<br>
}<br>
}<br>
Oledb<br>
[Visual basic]<br>
Imports system<br>
Imports system.data<br>
Imports system.data.oledb<br>
Imports microsoft.visualbasic<br>
<br>
Public Class sample<br>
<br>
Public Shared Sub Main () <br>
Dim nwindconn as OleDbConnection = New OleDbConnection ("Provider=sqloledb;data source=localhost;" & _<br>
"User Id=sa; Password=pwd;initial catalog=northwind ") <br>
<br>
Dim catcmd as OleDbCommand = Nwindconn.createcommand () <br>
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories" <br>
<br>
Nwindconn.open () <br>
<br>
Dim myreader as OleDbDataReader = Catcmd.executereader () <br>
<br>
Do While Myreader.read () <br>
Console.WriteLine (VbTab & "{0}" & VbTab & "{1}", Myreader.getint32 (0), myreader.getstring (1)) <br>
Loop<br>
<br>
Myreader.close () <br>
Nwindconn.close () <br>
End Sub<br>
End Class<br>
[c#]<br>
Using system;<br>
Using system.data;<br>
Using system.data.oledb;<br>
<br>
Class sample<br>
{<br>
public static void Main () <br>
{<br>
OleDbConnection nwindconn = new OleDbConnection ("Provider=sqloledb;data source=localhost; User Id=sa; Password=pwd;initial catalog=northwind ");<br>
<br>
OleDbCommand catcmd = Nwindconn.createcommand ();<br>
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories";<br>
<br>
Nwindconn.open ();<br>
<br>
OleDbDataReader myreader = Catcmd.executereader ();<br>
<br>
while (Myreader.read ()) <br>
{<br>
Console.WriteLine ("\t{0}\t{1}", Myreader.getint32 (0), myreader.getstring (1));<br>
}<br>
<br>
Myreader.close ();<br>
Nwindconn.close ();<br>
}<br>
}<br>
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