Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conStr As String = "Data Source=.\OSIRUS;" & _
"Initial Catalog=SA_TEST1;Integrated Security=SSPI;"
Dim connection As New SqlConnection(conStr)
Dim sql As String = "SELECT * FROM SaleTable"
Dim command As New SqlCommand(sql, connection)
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataSet()
adapter.Fill(data, "SaleTable")
DataGridView1.DataSource = data.Tables("SaleTable")
Dim headers() As String = {"รหัส", "ชื่อหมวด", "คำอธิบาย", "รูปภาพ", "5", "6"}
For i = 0 To headers.Length - 1
DataGridView1.Columns(i).HeaderText = headers(i)
Next
connection.Close()
End Sub
End Class
ส่วนรูปด้านล่างเป็น sql ที่ใช้เก็บข้อมูลค่ะ
Tag : .NET, Ms SQL Server 2008, VS 2010 (.NET 4.x)
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conStr As String = "Data Source=OSIRUS\SA;Initial Catalog=SA_TEST1;User ID=sa;Password=12345"
'Data Source = ชื่อเครื่องของ server
'Initial Catalog = ชื่อของ database
'User ID = user ของ sql
'Password = Password ของ sql
Dim connection As New SqlConnection(conStr)
Dim sql As String = "SELECT * FROM SaleTable"
Dim command As New SqlCommand(sql, connection)
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataSet()
adapter.Fill(data, "SaleTable")
DataGridView1.DataSource = data.Tables("SaleTable")
connection.Close()
End Sub
End Class