-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource1.vb
More file actions
67 lines (52 loc) · 2 KB
/
Copy pathsource1.vb
File metadata and controls
67 lines (52 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Option Strict On
Option Explicit On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Module Module1
Sub Main()
Dim str As String = GetConnectionString()
Dim dataSet As DataSet = GetCustomerData("CustomerData", str)
Dim table As DataTable
Dim column As DataColumn
Dim row As DataRow
For Each table In dataSet.Tables
Console.WriteLine(table.TableName)
For Each column In table.Columns
Console.Write(" {0}", column.ColumnName)
Next
Console.WriteLine()
For Each row In table.Rows
For Each column In table.Columns
Console.Write(" {0}", row(column))
Next
Console.WriteLine()
Next
Next
End Sub
'<Snippet1>
Private Function GetCustomerData(ByVal dataSetName As String, _
ByVal connectionString As String) As DataSet
Dim dataSet As New DataSet(dataSetName)
Using connection As SqlConnection = New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter( _
"SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", _
connection)
Dim mapping As DataTableMapping = adapter.TableMappings.Add( _
"Table", "Customers")
mapping.ColumnMappings.Add("CompanyName", "Name")
mapping.ColumnMappings.Add("ContactName", "Contact")
connection.Open()
adapter.FillSchema(dataSet, SchemaType.Source, "Customers")
adapter.Fill(dataSet)
Return dataSet
End Using
End Function
'</Snippet1>
Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);Initial Catalog=Northwind;" _
& "Integrated Security=SSPI;"
End Function
End Module