 |
|
VB 2008 ขอ Code ตรวจสอบ User ซ้ำ ในตารางของ Mysql ทีครับ [Win (Windows App), VB.NET] |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim strConnString,strSQL As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn.ConnectionString = strConnString
objConn.Open()
Dim intNumRows As Integer
strSQL = "SELECT * FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' "
objCmd = New MySqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
IF intNumRows > 0 Then
Me.pnlAdd.Visible = False
Me.lblStatus.Visible = True
Me.lblStatus.Text = "CustomerID already exist."
Else
End IF
|
 |
 |
 |
 |
Date :
2012-06-11 16:26:56 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก่อนจะ insert เราก็ทำการเช็คข้อมูลในตารางก่อนสิครับ แนวคิดผมนะ
Code (VB.NET)
sql = "select คอลัมน์ from ตาราง where='"& ข้อมูลที่กรอกเข้ามา &"'"
MyCommand = New MySqlCommand(sql, Con)
drConnection = MyCommand.ExecuteReader
dtConnection = New DataTable
dtConnection.Load(drConnection)
if dtConnection.Rows.Count > 0 Then
msgbox("Username นี้มีคนใช้แล้ว")
End If
ประมาณนี้อ่ะครับ
|
 |
 |
 |
 |
Date :
2012-06-11 16:31:15 |
By :
NIFCO |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณนะค่ะ แต่มันติดอีกค่ะว่า มันนับ Username ของตัวมันเองด้วยนะค่ะ พอกดบันทึกมันก็จะขึ้นบอกว่ามีอยู่แล้ว ต้องแก้ยังงัยค่ะ
|
 |
 |
 |
 |
Date :
2013-01-24 01:51:33 |
By :
BB |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนหน่อยค่ะ ต้องการตรวจสอบ Username ซ้ำกัน ต้องใส่Code ตรวจสอบยังไงค่ะ ใส่ตรงไหน รบกวนหน่อยค่ะPrivate Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim sqlEmployee As String = ""
Dim comEmployee As SqlCommand = Conn.CreateCommand()
Dim tmpMSG As String = ""
If (txtEmpNumber.Text = "") Or (txtEmpName.Text = "") Or (txtEmpPosition.Text = "") Or (txtEmpTel.Text = "") Or (txtUser.Text = "") Or (txtPass.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลสมาชิกให้ครบ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtEmpNumber.Focus()
Exit Sub
End If
If IsNumeric(txtEmpTel.Text) = False Then
MessageBox.Show("กรุณาป้อนข้อมูลที่เป็นตัวเลขเท่านั้น !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtEmpTel.Focus()
txtEmpTel.SelectAll()
Exit Sub
End If
Try
Select Case ActionFlag
Case "Add"
sqlEmployee = "INSERT INTO Employee (EmployeeID,Emp_Number,Emp_Name,Emp_Tel,Emp_Sex,Emp_Position,Username,Password,Status_User)"
sqlEmployee &= " VALUES ('" & lblEmpID.Text & "',"
sqlEmployee &= "'" & txtEmpNumber.Text & "',"
sqlEmployee &= "'" & txtEmpName.Text & "',"
sqlEmployee &= "'" & txtEmpTel.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= "'M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= "'F',"
End If
sqlEmployee &= "'" & txtEmpPosition.Text & "',"
sqlEmployee &= "'" & txtUser.Text & "',"
sqlEmployee &= "'" & txtPass.Text & "',"
sqlEmployee &= "'1')"
Case "Edit"
sqlEmployee = "UPDATE Employee "
sqlEmployee &= " SET Emp_Number='" & txtEmpNumber.Text & "',"
sqlEmployee &= " Emp_Name='" & txtEmpName.Text & "',"
sqlEmployee &= " Emp_Tel='" & txtEmpTel.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= " Emp_Sex='M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= " Emp_Sex='F',"
End If
sqlEmployee &= " Emp_Position='" & txtEmpPosition.Text & "',"
sqlEmployee &= " Username='" & txtUser.Text & "',"
sqlEmployee &= " Password='" & txtPass.Text & "',"
sqlEmployee &= " Status_User='1'"
sqlEmployee &= " WHERE (EmployeeID='" & lblEmpID.Text & "')"
End Select
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If ActionFlag = "Add" Then
tmpMSG = "คุณต้องการเพิ่มข้อมูล ใช่หรือไม่ ?"
ElseIf ActionFlag = "Edit" Then
tmpMSG = "คุณต้องการแก้ไขข้อมูล ใช่หรือไม่ ?"
End If
If MessageBox.Show(tmpMSG, "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
With comEmployee
.CommandType = CommandType.Text
.CommandText = sqlEmployee
.Connection = Conn
.ExecuteNonQuery()
End With
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถเพิ่มสมาชิกใหม่ได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
MessageBox.Show("บันทึกลงฐานข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
grbSearch.Enabled = True
ControlStatus(False)
btnAdd.Enabled = True
btnEdit.Enabled = False
btnOK.Visible = False
btnCancel.Visible = False
ClearData()
cmdMoveFirst.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdMoveLast.Enabled = False
lblShowPositions.Text = ""
txtSearch.Text = ""
txtSearch.Focus()
End Sub
|
 |
 |
 |
 |
Date :
2013-03-14 17:20:38 |
By :
teaw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|