Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine("{0}", reader(0))
End While
End Using
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim str As String = "Data Source=.; Initial Catalog=pro_consumeble; Integrated Security=SSPI;"
Dim con As New SqlConnection(str)
con.Open()
Dim sq As String = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id =" & ComboBox1.Text & ""
Dim cm As New SqlCommand(sql, con)
Dim dr As SqlDataReader = cm.ExecuteReader()
While dr.Read
TextBox2.Text = "cus_fullname"
End While
End Sub
Private Sub combobox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles combobox1.SelectionChangeCommitted
Dim str As String = "Data Source=.; Initial Catalog=pro_consumeble; Integrated Security=SSPI;"
Dim con As New SqlConnection(str)
con.Open()
Dim sq As String = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id =" & ComboBox1.Text & ""
Dim cm As New SqlCommand(sql, con)
Dim dr As SqlDataReader = cm.ExecuteReader()
dr.Read()
TextBox2.Text = dr.item("cus_fullname")
con.Close()
End Sub
Module ModuleCode
Dim str As String = "Data Source=.; Initial Catalog=pro_consumeble; Integrated Security=SSPI;"
Dim con As New SqlConnection(str)
Public Sub open_connection()
Try
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Catch ex As Exception
Throw New Exception("การเชื่อมต่อฐานข้อมูลใช้งานไม่ได้ เกิดข้อผิดพลาด : " & ex.Message)
End Try
End Sub
End Module
การใช้งาน ก็ call open_connection()
Code (VB.NET)
Dim sq As String = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id =" & ComboBox1.Text & ""
Dim cm As New SqlCommand(sql, con)
Call open_connection()
Dim dr As SqlDataReader = cm.ExecuteReader()
dr.Read()
TextBox2.Text = dr.item("cus_fullname")
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id ='" & ComboBox1.Text & "'"
cmd = New SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
DR.Read()
Text_fullname_export.Text = DR.Item("cus_fullname")
End Sub
Imports System.Data.Sql
Imports System.Data.SqlClient
Module code
Friend cn As New SqlConnection("Data Source=.; Initial Catalog=pro_consumeble; Integrated Security=SSPI;")
'cmd sqlcommand
Friend cmd As New SqlCommand
'DA sqldataadapter
Friend DA As New SqlDataAdapter
'SQL = เป็นคำสั่ง
Friend sql As String
'SQL DATA SET
Friend DS As DataSet
Friend DR As SqlDataReader
'ฟังก์ชันเปิดฐานข้อมูล
Friend Sub connect_open()
Try
If cn.State = ConnectionState.Closed Then cn.Open()
Catch ex As Exception
End Try
End Sub
End Module
'ฟังก์ชันเปิดฐานข้อมูล
Friend Sub connect_open()
Try
If cn.State = ConnectionState.Open Then cn.Close()
cn.Open()
Catch ex As Exception
Throw New Exception("การเชื่อมต่อฐานข้อมูลใช้งานไม่ได้ เกิดข้อผิดพลาด : " & ex.Message)
End Try
End Sub
ไอดีมาตัวเดียว ต้องใช้ While เพื่อลูปดึงข้อมูลออกมาให้หมด
เช่น Code (VB.NET)
sql = "SELECT cus_id FROM customer"
cmd = New SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
While DR.Read()
combobox1.Item.Add (DR.Item("cus_id"))
end while
Private Sub combox()
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_status = 'Ture' "
cmd = New SqlClient.SqlCommand(sql, cn)
DR = cmd.ExecuteReader()
If DR.HasRows Then
While DR.Read()
ComboBox1.Items.Add(DR.Item("cus_id"))
Text_fullname_export.Text = (DR.Item("cus_fullname"))
End While
DR.Close()
End If
End Sub
Code (VB.NET)
Private Sub export_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
combox()
End Sub
Sub combox()
'แนะนำให้เปิด/ปิด Connection ในฟังชั่นนะครับ ลดปัญหาเวลา ตัวอื่นเรียกใช้แล้วมักฟ้อง error ต่างๆครับ
'แบบนี้ไม่ยุ่งกะใครดี ใช้เสร็จก็ปิด
Using connection As New SqlConnection(ConnectionString)
connection.Open()
Dim command As New SqlCommand("SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_status = 'Ture' ", connection)
Dim DataAdapter As New SqlDataAdapter(command)
Dim _dt As New DataTable
Try
DataAdapter.Fill(_dt) 'ดึงมาใช้เป็น DataTable ผมทำเป็นแค่นี้ 555
'ผูก ComboBox กับ datasource
ComboBox1.DataSource = _dt
ComboBox1.DisplayMember = "cus_id" 'เลือก column ที่จะโชว์ให้โลกเห็น
''ผูก Text_fullname_export กับ datasource
Text_fullname_export.DataBindings.Add(New System.Windows.Forms.Binding("Text", _dt, "cus_fullname", True))
Catch ex As Exception
End Try
End Using
End Sub
แบบนี้เลยครับ
การผูก control ต่างๆเข้ากับ DataSource ของเรา
Private Sub combox()
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_status = 'Ture' "
cmd = New SqlClient.SqlCommand(sql, cn)
DR = cmd.ExecuteReader()
If DR.HasRows Then
While DR.Read()
ComboBox1.Items.Add(DR.Item("cus_id"))
Text_fullname_export.Text = (DR.Item("cus_fullname"))
End While
DR.Close()
End If
End Sub
แท็กบ็อก มันแสดงได้ที่ละชื่อครับ ไม่ต้อง While มาใส่ แท็กบ็อก ก็ได้
Text_fullname_export.Text = (DR.Item("cus_fullname"))
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id ='" & ComboBox1.Text & "'"
cmd = New SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
DR.Read()
Text_fullname_export.Text = DR.Item("cus_fullname")
End Sub
รวมทั้งหมดในหน้านี้ Code (VB.NET)
Private Sub combox()
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_status = 'Ture' "
cmd = New SqlClient.SqlCommand(sql, cn)
DR = cmd.ExecuteReader()
If DR.HasRows Then
While DR.Read()
ComboBox1.Items.Add(DR.Item("cus_id"))
Text_fullname_export.Text = (DR.Item("cus_fullname"))
End While
DR.Close()
End If
End Sub
Private Sub export_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
combox()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id ='" & ComboBox1.Text & "'"
cmd = New SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
DR.Read()
Text_fullname_export.Text = DR.Item("cus_fullname")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_id ='" & ComboBox1.Text & "'"
cmd = New SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
DR.Read()
Text_fullname_export.Text = DR.Item("cus_fullname")
Catch ex As Exception
MsgBox( ex.Message)
End Try
End Sub
Private Sub combox() แก้ไขนิดหน่อย
Me.Combobox1.Item.Clear() เอาไว้เคลียร์ข้อมูลใน Combobox1 Code (VB.NET)
Private Sub combox()
Try
Me.Combobox1.Item.Clear()
sql = "SELECT cus_id, cus_fullname, cus_dept, cus_date, cus_status FROM customer WHERE cus_status = 'Ture' "
cmd = New SqlClient.SqlCommand(sql, cn)
Call connect_open()
DR = cmd.ExecuteReader()
If DR.HasRows Then
While DR.Read()
ComboBox1.Items.Add(DR.Item("cus_id"))
End While
End If
Catch ex As Exception
MsgBox( ex.Message)
End Try
End Sub