 |
|
สอบถามการตรวจสอบค่าที่ซ้ำกันในตาราง เมื่อมีการ Insert , Update , Delete vb.net |
|
 |
|
|
 |
 |
|
ตามหัวข้อเลยครับ ตอนนี้ผมได้แค่ Insert จะ Update กับ Delete ต่อ ไปไม่เป็นเลยครับ รบกวนด้วยครับ
Code (VB.NET)
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Conn As New OleDbConnection(strConnection)
Dim Comm As New OleDb.OleDbCommand()
Conn.Open()
Dim intNumRows As Integer
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Dim strSQL As String
strSQL = "SELECT count(*) from [GROUP_USERS] gu WHERE gu.[GROUP_USER_ID] = '" & Me.DataGridView1.Item(2, i).Value.ToString() & "' "
Comm = New OleDbCommand(strSQL, Conn)
intNumRows = Comm.ExecuteScalar()
If intNumRows > 0 Then
'MessageBox.Show("มี USER นี้แล้ว.")
'Else
Dim strSQL2 As String
strSQL2 = "INSERT INTO GROUP_USERS ([GROUP_USER],[USER_NAME],[Create_User],[Create_Date]) " &
" VALUES " &
" ('" & Me.TextBox1.Text & "','" & Me.DataGridView1.Item(1, i).Value.ToString() & "', '" & Label6.Text & "' , getdate() )"
Comm = New OleDbCommand
With Comm
.Connection = Conn
.CommandText = strSQL2
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
MessageBox.Show("เพิ่ม USER นี้แล้ว.")
Else
Dim strSQL3 As String
strSQL3 = ""
Me.DataView()
End If
End Sub
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2017-10-31 11:59:18 |
By :
stepartz |
View :
979 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If intNumRows > 0 Then
'MessageBox.Show("มี USER นี้แล้ว.")
'Else
Dim strSQL2 As String
strSQL2 = "INSERT INTO GROUP_USERS ([GROUP_USER],[USER_NAME],[Create_User],[Create_Date]) " &
" VALUES " &
" ('" & Me.TextBox1.Text & "','" & Me.DataGridView1.Item(1, i).Value.ToString() & "', '" & Label6.Text & "' , getdate() )"
Comm = New OleDbCommand
With Comm
.Connection = Conn
.CommandText = strSQL2
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
MessageBox.Show("เพิ่ม USER นี้แล้ว.")
Else
Dim strSQL3 As String
strSQL3 = " Update อะไรก็ว่ากันไป"
Comm = New OleDbCommand
With Comm
.Connection = Conn
.CommandText = strSQL3
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
MessageBox.Show("Update USER นี้แล้ว.")
Me.DataView()
End If
มีวิธีที่ง่าย และ ไม่เสียเวลา คือ bindingsource ร่วมกับ dataset
|
 |
 |
 |
 |
Date :
2017-10-31 12:31:40 |
By :
Dr.K |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าจะให้ลบทีหลัง ไม่ลบทันที ผมแนะนนำให้เก็บ ID ตัวที่ลบลง List หรือ Array ก่อนครับ จากนั้นพอ Save ค่อยไปตามลบทีหลัง
|
 |
 |
 |
 |
Date :
2017-10-31 16:40:48 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|