'การเรียกใช้
If Check(txtT1) = True Then
MessageBox.Show("บันทึกข้อมูลซ้ำ !!! กรุณาตรวจสอบอีกครั้ง", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Code (VB.NET)
'ฟังก์ชั่น
Public Function Check(ByVal Search As String) As Boolean
Dim strConn As String
strConn = DBConnString.strConn
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim da As New SqlDataAdapter
sb = New StringBuilder
sb.Remove(0, sb.Length())
sb.Append("SELECT T1 FROM Table1 ")
sb.Append(" WHERE (T1 = '" & Search & "')")
Dim Sqlmccode As String = sb.ToString()
Dim ds As New DataSet()
da = New SqlDataAdapter(Sqlmccode, Conn)
da.SelectCommand.CommandText = Sqlmccode
da.Fill(ds , "Table1")
If ds .Tables("Table1").Rows.Count <> 0 Then
Return True
Else
Return False
End If
End Function