 |
|
ขอโค้ดปุ่มค้นหา vb2010 myqlconnector ให้โชว์ข้อมูลใน datagridview |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Public Class customer
Private Sub customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If con.State = ConnectionState.Closed Then
con.Open()
End If
loadfile()
con.Close()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
txtcus_id.Text = ""
txtprefix.Text = ""
txtfullname.Text = ""
txtsex.Text = ""
txttel.Text = ""
txtaddress.Text = ""
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Query As String
Query = "INSERT INTO customer"
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=bakery;User ID=root;Password=root;")
Dim sql As MySqlCommand = New MySqlCommand(Query, con)
Query = "INSERT INTO customer(cus_id,prefix,fullname,sex,birth,address,tel)VALUES("
Query = Query + txtcus_id.Text + ",'" + txtprefix.Text + "','" + txtfullname.Text + "','" + txtsex.Text + "','" + Datebirth.Text + "','" + txtaddress.Text + "','" + txttel.Text + "')"
con.Open()
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
'lblMsg.Text = "Record is Successfully Inserted"
'Else
'lblMsg.Text = "Record is not Inserted"
'End If
con.Close()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim Query As String
con.Open()
Dim xx As Integer
xx = DataGridView2.CurrentRow.Index
'MsgBox(DataGridView2.Item(0, xx).Value)
Dim zz As String
zz = DataGridView2.Item(0, xx).Value.ToString
Query = "Delete FROM customer WHERE cus_id =" + zz
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then
' lblMsg.Text = "Record is Successfully Deleted"
'Else
' lblMsg.Text = "Record is not Deleted"
'End If
con.Close()
End Sub
Private Sub Datebirth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Datebirth.Click
'Dim time As Date = Date.Now()
'Datebirth.Value = time.ToString("yyyy-MM-dd")
'Datebirth.Value= ToString("dd-MM-yyyy")
End Sub
Public Sub loadfile()
Dim sqlQuery As String = "select * from customer"
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCmd As New MySqlCommand
Dim sqlTable As New DataTable
Dim i As Integer
With sqlCmd
.CommandText = sqlQuery
.Connection = con
End With
With sqlAdapter
.SelectCommand = sqlCmd
.Fill(sqlTable)
End With
For i = 0 To sqlTable.Rows.Count - 1
DataGridView2.Rows.Add(sqlTable.Rows(i)("cus_id"), (sqlTable.Rows(i)("prefix")), (sqlTable.Rows(i)("fullname")),
(sqlTable.Rows(i)("sex")), (sqlTable.Rows(i)("birth")), (sqlTable.Rows(i)("address")), (sqlTable.Rows(i)("tel")))
Next
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim Query As String
con.Open()
Query = "UPDATE customer SET fullname ='" + txtfullname.Text + "'"
Query = Query + ",prefix = '" + txtprefix.Text + "'"
Query = Query + ",sex = '" + txtsex.Text + "'"
Query = Query + ",birth = '" + Datebirth.Value + "'"
Query = Query + ",address = '" + txtaddress.Text + "'"
Query = Query + ",tel = '" + txttel.Text + "'"
Query = Query + " WHERE cus_id = '" + txtcus_id.Text + "'"
Dim cmd As MySqlCommand = New MySqlCommand(Query, con)
MsgBox(Query)
Dim i As Integer = cmd.ExecuteNonQuery()
'If (i > 0) Then 'lblMsg.Text = "Record is Successfully Updated"
' lblMsg.Text = "Record is not Updated"
'End If .
End Sub
Private Sub DataGridView2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
Dim i As Integer
i = DataGridView2.CurrentRow.Index
txtcus_id.Text = DataGridView2.Item(0, i).Value
txtprefix.Text = DataGridView2.Item(1, i).Value
txtfullname.Text = DataGridView2.Item(2, i).Value
txtsex.Text = DataGridView2.Item(3, i).Value
txtaddress.Text = DataGridView2.Item(5, i).Value
txttel.Text = DataGridView2.Item(6, i).Value
End Sub
End Class
Tag : .NET
|
|
 |
 |
 |
 |
Date :
2012-03-26 15:56:29 |
By :
แอน |
View :
2216 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปัญหาของคุณคืออะไรครับ ว่าแต่คุณไป ,Open() ใน Page_Load() แต่เอา
Code (VB.NET)
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=bakery;User ID=root;Password=root;")
ไปไว้่ข้างล่างทำไมครับ 
|
 |
 |
 |
 |
Date :
2012-03-26 16:14:09 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วต้องใส่ไว้ตรงไหนค่ะ
แล้วปุ่มค้นหามีไหมค่ะ
|
 |
 |
 |
 |
Date :
2012-03-27 10:30:54 |
By :
แอน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่คือโค้ดปุ่มค้นหาที่ได้ค่ะ แต่พอใส่ id เข้าไปกดปุ่มค้นหามันกลับเพิ่ม id ที่ใส่ขึ้นมาซ้ำอีกตัว
แล้วจะทำยังไง
-ให้ข้อมูลมันไม่ซ้ำ
-แล้วกดค้นหา id ตัวไหนให้มันขึ้นโชว์ข้อมูลเป็นแท็ปสีฟ้า
Code (VB.NET)
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
Dim sqlQuery As String = "select * from customer WHERE cus_id = "
sqlQuery += txtcus_id.Text
MsgBox(sqlQuery)
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCmd As New MySqlCommand
Dim sqlTable As New DataTable
Dim i As Integer
With sqlCmd
.CommandText = sqlQuery
.Connection = con
End With
With sqlAdapter
.SelectCommand = sqlCmd
.Fill(sqlTable)
End With
DataGridView2.Rows.Add(sqlTable.Rows(i)("cus_id"), (sqlTable.Rows(i)("prefix")), (sqlTable.Rows(i)("fullname")),
(sqlTable.Rows(i)("sex")), (sqlTable.Rows(i)("birth")), (sqlTable.Rows(i)("address")), (sqlTable.Rows(i)("tel")))
End Sub
|
 |
 |
 |
 |
Date :
2012-03-28 11:59:03 |
By :
แอน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|