Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim ds As New DataSet
Dim conn As OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Mone\My Documents\Northwind2007.accdb") ' ใสชื่อที่อยูของดาต้านะครับ
If conn.State = ConnectionState.Open Then conn.Close()
conn.Open()
Loads()
End Sub
Private Sub Loads()
ds.Clear()
Dim cmd = New OleDbCommand("SELECT [ID],[Company],[Last Name],[Job Title],[City] FROM Customers", conn)
Dim adp = New OleDbDataAdapter(cmd)
adp.Fill(ds, "Customers")
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "Customers"
End Sub
Imports System.Data.SqlClient
Public Class Form1
Dim cn As SqlConnection
Dim Da As SqlDataAdapter
Dim Dt As DataTable
Dim com As SqlCommand
Dim sql As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn = New SqlConnection
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = Module1.strcon
.Open()
End With
End Sub
End Class
สร้าง Module ขึ้นมา
Code (VB.NET)
Module Module1
Public strcon As String = "Data Source= server name ใน sql เวลาเปิด sql จะเห็น; Initial Catalog=ชื่อฐานข้อมูล; Integrated Security=True"
End Module