This creates a DataSet; you can also use a DataTable.
---------------------------------
Dim ds As DataSet
'open the connection
Using cnn As New SqlConnection(M y.Settings.PTCo nnectionString)
cnn.Open()
'define the command
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "SELECT * FROM Product"
'define the data adapter and fill the data table
Dim da As New SqlDataAdapter( cmd)
ds = New DataSet
da.Fill(ds, "Product")
End Using
For Each dr As DataRow In ds.Tables("Prod uct").Rows
Dim ProductID As Integer = CType(dr.Item(" ProductID"), Integer)
Dim ProductName As String = dr.Item("Produc tName").ToStrin g
Dim ProductNumber As String = dr.Item("Produc tNumber").ToStr ing
'Dim Price As Nullable(Of Decimal)
'If .Item("Price") IsNot DBNull.Value Then
' Price = CType(.Item("Pr ice"), Decimal)
'End If
Dim Description As String = dr.Item("Descri ption").ToStrin g
'Dim ProductType As Integer = CType(.Item("Pr oductType"), Integer)
'Dim StockType As String = .Item("StockTyp e").ToString
Console.WriteLi ne(String.Forma t("ProductID {0}, " & _
"ProductNam e {1}, {2}ProductNumbe r {3}, " & _
"Descriptio n {4}", ProductID, ProductName, _
ControlChars.Cr Lf, ProductNumber, Description))
Next
-------------------------------
You can also load a dataset with multiple tables.
To do this, your sql would have multiple sql statements
in yhour commandtext:
SELECT * FROM PRODUCT; SELECT * FROM CUSTOMERS
and then you name the tables like this:
ds.Tables(0).Ta bleName = "Product"
ds.Tables(1).Ta bleName = "Customers"
Robin S.
---------------------------------------
"SmartbizAustra lia" <to*@smartbiz.c om.auwrote in message
news:11******** ************@j4 4g2000cwa.googl egroups.com...
Hi,
This seems to be a neglected bit of info as everyone gets carried away
with data binding examples instead.
Can simply use the datareader as below:
Private Sub PopulateControl s1()
Dim sSql As String
Dim cn As SqlConnection
cn = New SqlConnection(m _sConnection)
cn.Open()
sSql = "Select top 10 * from Person.Contact"
Dim sqlCmd As New SqlCommand(sSql , cn)
Dim r As SqlDataReader = sqlCmd.ExecuteR eader()
'Get the first line
r.Read()
Me.Label1.Text = "First Name"
Me.TextBox1.Tex t = r.Item("FirstNa me")
'Now get the new line
r.Read()
Me.Label2.Text = "First Name"
Me.TextBox2.Tex t = r.Item("FirstNa me")
cn.Close()
End Sub
but would love to do the same with a dataset instead.
But how do you walk through each row and select particular columns in a
dataset?