-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathsource2.vb
More file actions
67 lines (52 loc) · 1.99 KB
/
Copy pathsource2.vb
File metadata and controls
67 lines (52 loc) · 1.99 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 table As DataTable = GetCustomerData("Customers", 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 dataTableName As String, _
ByVal connectionString As String) As DataTable
Dim table As New DataTable(dataTableName)
Using connection As SqlConnection = New SqlConnection(connectionString)
Dim adapter 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(table, SchemaType.Mapped)
adapter.Fill(table)
Return table
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