|
 |
|
เช็คเงื่อนไข ID ถ้ามี Update ถ้าไม่มี Insert (ID exist Update else insert) for VB2013 Mysql |
|
 |
|
|
 |
 |
|
สำหรับ VB2013 ครับผม โค๊ดผิดพลาดประการใดขออภัย มั่วล้วนๆ นำไปปรังปรุง ประยุกต์ใช้กันเองนะครับ
Code (VB.NET)
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim strConnString, strSQL As String
Me.IDM.Text = Session("strUser").ToString
Dim intNumRows = ""
strConnString = "Server=localhost;User Id=root; Password=XXXX; Database=XXXX; Pooling=false"
Dim Con As New MySqlConnection(strConnString)
strSQL = "SELECT * FROM feeder_sched WHERE (KeyID like '%" & IDM.Text & "%' ) "
Con.Open()
Dim Command As New MySqlCommand(strSQL, Con)
Try
intNumRows = Command.ExecuteScalar
Catch ex As Exception
MsgBox("Error")
End Try
If intNumRows Is Nothing Then
strSQL = "INSERT INTO feeder_sched (hour_1,min_1,hour_2,min_2,hour_3,min_3,weight_1,weight_2,weight_3,KeyID) " & _
" VALUES " & _
" ('" & Me.Hour1.Text & "','" & Me.Min1.Text & "', " & _
" '" & Me.Hour2.Text & "','" & Me.Min2.Text & "','" & Me.Hour3.Text & "','" & Me.Min3.Text & "','" & Me.weight1.Text & "','" & Me.weight2.Text & "','" & Me.weight3.Text & "','" & Me.IDM.Text & "')"
objConn.ConnectionString = strConnString
objConn.Open()
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
Me.Label10.Visible = True
Me.Label10.Text = "Set time Successful"
Catch ex As Exception
Me.Label10.Visible = True
Me.Label10.Text = "Failed Please press Update"
End Try
objConn.Close()
objConn = Nothing
Else
strSQL = "UPDATE feederphone.feeder_sched SET hour_1='" & Hour1.Text & "',min_1='" & Min1.Text & "',hour_2='" & Hour2.Text & "',min_2='" & Min2.Text & "',hour_3='" & Hour3.Text & "',min_3='" & Min3.Text & "',weight_1='" & weight1.Text & "',weight_2='" & weight2.Text & "',weight_3='" & weight3.Text & "',KeyID='" & IDM.Text & "' WHERE KeyID = '" & IDM.Text & "'"
objConn.ConnectionString = strConnString
objConn.Open()
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
Me.Label10.Visible = True
Me.Label10.Text = "Updated"
Catch ex As Exception
Me.Label10.Visible = True
Me.Label10.Text = "Failed Please check you NetWork"
End Try
objConn.Close()
objConn = Nothing
End If
End Sub
Tag : .NET, MySQL, VB.NET

|
|
 |
 |
 |
 |
Date :
2017-02-07 02:49:46 |
By :
kosspeed |
View :
851 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สรุปทำได้แล้วใช่ไหม๊ครับ
|
 |
 |
 |
 |
Date :
2017-02-07 11:54:44 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|