The following example shows what a simple ado.net application which connects to the Northwind database and returns a list O F Categories would look like. The example writes the output to the console, or command prompt.
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.
SqlClient
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Public Class Sample
Public Shared Sub Main ()
Dim Nwindconn As SqlConnection = New SqlConnection ("Data source=localhost;" & _
"User Id=sa; Password=pwd;initial Catalog=northwind ")
Dim catcmd as SqlCommand = Nwindconn.createcommand ()
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories"
Nwindconn.open ()
Dim myreader As SqlDataReader = Catcmd.executereader ()
Do While Myreader.read ()
Console.WriteLine (VbTab & "{0}" & VbTab & "{1}", Myreader.getint32 (0), myreader.getstring (1))
Loop
Myreader.close ()
Nwindconn.close ()
End Sub
End Class
[C #]
Using System;
Using System.Data;
Using System.Data.SqlClient;
Class Sample
{
public static void Main ()
{
SqlConnection nwindconn = new SqlConnection ("Data source=localhost; User Id=sa; Password=pwd;initial Catalog=northwind ");
SqlCommand catcmd = Nwindconn.createcommand ();
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories";
Nwindconn.open ();
SqlDataReader myreader = Catcmd.executereader ();
while (Myreader.read ())
{
Console.WriteLine ("T{0}t{1}", Myreader.getint32 (0), myreader.getstring (1));
}
Myreader.close ();
Nwindconn.close ();
}
}
OLE DB
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Public Class Sample
Public Shared Sub Main ()
Dim nwindconn as OleDbConnection = New OleDbConnection ("Provider=sqloledb;data source=localhost;" & _
"User Id=sa; Password=pwd;initial Catalog=northwind ")
Dim catcmd as OleDbCommand = Nwindconn.createcommand ()
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories"
Nwindconn.open ()
Dim myreader as OleDbDataReader = Catcmd.executereader ()
Do While Myreader.read ()
Console.WriteLine (VbTab & "{0}" & VbTab & "{1}", Myreader.getint32 (0), myreader.getstring (1))
Loop
Myreader.close ()
Nwindconn.close ()
End Sub
End Class
[C #]
Using System;
Using System.Data;
Using System.Data.OleDb;
Class Sample
{
public static void Main ()
{
OleDbConnection nwindconn = new OleDbConnection ("Provider=sqloledb;data source=localhost; User Id=sa; Password=pwd;initial Catalog=northwind ");
OleDbCommand catcmd = Nwindconn.createcommand ();
Catcmd.commandtext = "Select CategoryID, CategoryName from Categories";
Nwindconn.open ();
OleDbDataReader myreader = Catcmd.executereader ();
while (Myreader.read ())
{
Console.WriteLine ("T{0}t{1}", Myreader.getint32 (0), myreader.getstring (1));
}
Myreader.close ();
Nwindconn.close ();
}
}