 |
|
มีปัญหาตอนที่ทำการ เพิ่ม ลบ แก้ไข และยกเลิกข้อมูล คับ เมื่อคลิ๊กแล้วต้องปิดการรันไปก่อนแล้วรันใหม่ถึงจะเห็นผลลัพธ์ คับ พอจะมีวิธีไหนบ้างค่ะที่สามารถรีเฟรชหรืออัพเดตข้อมูลได้เลย โดยไม่ต้องงปิดการรันแล้วรันใหม่ รบกวนหน่อยน่ะคับ |
|
 |
|
|
 |
 |
|
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class FrmCustomer
Private Sub FormCustomer_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 BtnCanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCanel.Click
txtIdCus.Text = ""
txtNameCus.Text = ""
txtSexCus.Text = ""
txtAddressCus.Text = ""
txtTelCus.Text = ""
End Sub
Private Sub addCus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddCus.Click
Dim Query As String
Query = "INSERT INTO customer"
Dim con As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=tree;User ID=root;Password=123456;")
Dim sql As MySqlCommand = New MySqlCommand(Query, con)
Query = "INSERT INTO customer(CustomerID,CustomerName,Sex,Address,Telephone)VALUES("
Query = Query + txtIdCus.Text + ",'" + txtNameCus.Text + "','" + txtSexCus.Text + "','" + txtAddressCus.Text + "','" + txtTelCus.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 BtnEditCus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEditCus.Click
Dim Query As String
con.Open()
Query = "UPDATE Customer SET CustomerName ='" + txtNameCus.Text + "'"
Query = Query + ",Sex = '" + txtSexCus.Text + "'"
Query = Query + ",Address = '" + txtAddressCus.Text + "'"
Query = Query + ",Telephone = '" + txtTelCus.Text + "'"
Query = Query + " WHERE CustomerID = '" + txtIdCus.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
con.Close()
End Sub
Private Sub BtndelCus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtndelCus.Click
Dim Query As String
con.Open()
Dim xx As Integer
xx = DataGridView1.CurrentRow.Index
'MsgBox(DataGridView1.Item(0, xx).Value)
Dim zz As String
zz = DataGridView1.Item(0, xx).Value.ToString
Query = "Delete FROM customer WHERE CustomerId =" + 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
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
DataGridView1.Rows.Add(sqlTable.Rows(i)("CustomerID"), (sqlTable.Rows(i)("CustomerName")), (sqlTable.Rows(i)("Sex")),
(sqlTable.Rows(i)("Address")), (sqlTable.Rows(i)("Telephone")))
Next
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
txtIdCus.Text = DataGridView1.Item(0, i).Value
txtNameCus.Text = DataGridView1.Item(1, i).Value
txtSexCus.Text = DataGridView1.Item(2, i).Value
txtAddressCus.Text = DataGridView1.Item(3, i).Value
txtTelCus.Text = DataGridView1.Item(4, i).Value
End Sub
Private Sub BtnSeachCus_Click(sender As System.Object, e As System.EventArgs) Handles BtnSeachCus.Click
Dim sqlQuery As String = "select * from customer WHERE CustomerID = "
sqlQuery += txtIdCus.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
For i = 0 To sqlTable.Rows.Count - 1
DataGridView1.Rows.Add(sqlTable.Rows(i)("CustomerID"), (sqlTable.Rows(i)("CustomerName")), (sqlTable.Rows(i)("Sex")),
(sqlTable.Rows(i)("Address")), (sqlTable.Rows(i)("Telephone")))
Next
End Sub
Tag : .NET, MySQL, VB.NET
|
|
 |
 |
 |
 |
Date :
2012-03-28 17:37:30 |
By :
tomza |
View :
978 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
FrmCustomer_Load(sender,e)
|
 |
 |
 |
 |
Date :
2012-03-28 17:46:48 |
By :
l3kaing |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วตอน insert,update,delete
เราเอา loadfile() ไปใส่ไม่ได้หรอคะ
**ไม่รู้ช่วยได้ป่าว ลองดูนะคะ
|
 |
 |
 |
 |
Date :
2012-03-28 17:47:25 |
By :
por_punggo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คนล่ะคนคับ อิอิ
|
 |
 |
 |
 |
Date :
2012-03-28 19:37:41 |
By :
tomza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลักษณะการเขียนโค้ดเหมือนกันมาก ^^
|
 |
 |
 |
 |
Date :
2012-03-28 20:33:55 |
By :
sodamax |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เรียนด้วยกัลคับ   
ตอนนี้ได้แล้วครับ ขอบคุณมาก ๆๆ น่ะครับ
แล้วจะมารบกวนพี่อีกน่ะครับบบ   
|
 |
 |
 |
 |
Date :
2012-03-29 00:15:40 |
By :
tomza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|