Dim sqlStr As String = "select * from Customer"
Dim tr As SqlTransaction
Dim da As New SqlDataAdapter(sqlStr, strCon)
Private Sub ShowAllData()
da.Fill(ds, "Customer")
dgvCustomer.DataSource = ds.Tables("Customer")
End Sub
Public Conn As New SqlConnection()
Public Com As New SqlCommand()
Public strCon As String = "Data Source=HOME-PC\SQLSERVER;Initial Catalog=MusicTreading;Integrated Security=True"
Public ds As New DataSet()
Public sb As New StringBuilder()
Private Sub Showdata()
Dim sqlDepartment As String
sqlDepartment = "SELECT * FROM AssetStaff"
Dim dr As SqlDataReader
Dim dt As DataTable
com = New SqlCommand()
With com
.CommandType = CommandType.Text
.CommandText = sqlDepartment
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dt = New DataTable()
dt.Load(dr)
dgvShowData.DataSource = dt
Else
dgvShowData.DataSource = Nothing
End If
End With
dr.Close()
End Sub
แล้วก็เรียก Sub ตอน Form Load
Code (VB.NET)
Private Sub frmAddStaff_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Showdata()
End Sub
ลองดูนะครับ ถ้าได้ไม่ได้ยังไงรบกวนบอกผมด้วย
ปล.ต้อง ConnectString ใน Form Load ด้วยนะครับ