如何查找在vb.net中的sql数据库表中是否存在在文本框中输入的文本 [英] how to find whether the text entered in textbox is present in sql database table in vb.net

查看:165
本文介绍了如何查找在vb.net中的sql数据库表中是否存在在文本框中输入的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找在vb.net的sql数据库表中是否存在在文本框中输入的文本.我有一个文本框.如果我在移至下一个文本框时在文本框中输入值,则需要查找输入的值是否存在于表中.如果是,则无法找到msgbox或显示记录..

谢谢.

How to find whether the text entered in textbox is present in sql database table in vb.net. I have a text box.if i enter value in textbox while moving to next textbox, i need to find whether the value entered is present in table or not. If yes diplay msgbox or display record not found..

Thank you.

推荐答案

您需要创建一个返回布尔值的函数.此函数将检查数据库以查看字符串是否存在.
这样的东西

You need to create a function that returns a boolean. This function would check the database to see if the string is present.
Something like this

Private Function CheckIfPresent(ByVal textToCheck As String)

        Using con As SqlConnection = New SqlConnection("your connection string")
            Dim cmd As SqlCommand = New SqlCommand("SELECT textField FROM yourTable WHERE textField = @textField", con)
            cmd.Parameters.AddWithValue("@textField", textToCheck)
            con.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            Dim IsThere As Boolean = False
            If dr.HasRows Then
                IsThere = True
            End If
            dr.Close()
            Return IsThere
        End Using

    End Function


然后在TextBox的LostFocus事件中,只需调用此函数


and then in your TextBox''s LostFocus event you just call this function

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        Dim textToCheck As String = TextBox1.Text
        If CheckIfPresent(textToCheck) Then
            'Do work if text is there
        Else
            'Do work if text not found
        End If
    End Sub




希望对您有帮助




Hope this helps


这篇关于如何查找在vb.net中的sql数据库表中是否存在在文本框中输入的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆