检查电话号码是否存在于ms表中 [英] check if the phone number exist in ms table

查看:155
本文介绍了检查电话号码是否存在于ms表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道电话号码是否存在于ms数据库中?

我正在使用此代码,但出现错误,我想我需要将所有电话号码作为数组获取,并在数组中搜索是否存在该号码.

how i can know if the phone number exist in ms database or not ?

I''m using this code but i have error , i think i need to get all phone number as array and search in array if the number exist or not .

Dim number As String
    con.Open()
    Dim sql As String = "select cphone from cust "
    Dim cmd As New OleDbCommand(sql, con)
    Dim dr As OleDbDataReader
    dr = cmd.ExecuteReader
    While dr.Read
        number = dr(5)
    End While
    con.Close()

    If TextBox5.Text = number Then
        MsgBox("exist")
    Else
        MsgBox("NOT-exist")
    End If

推荐答案

您的错误是什么?现在,我可以告诉您,除非您要查找的电话号码是表格中的最后一个条目,否则您将永远找不到它.您应该做这样的事情:

What is your error? I can tell you now that unless the phone number you are looking for is the last entry in the table, you will never find it. You should be doing something like this:

Dim number As String
Dim found As Boolean
con.Open()
Dim sql As String = "select cphone from cust "
Dim cmd As New OleDbCommand(sql, con)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader

found = false

While dr.Read
    number = dr(5)
    
    If TextBox5.Text = number Then
        found = True
        Exit While
    End If
End While
con.Close()

If found Then
    MsgBox("exist")
Else
    MsgBox("NOT-exist")
End If


让数据库执行为您服务:
Let the database do the work for you:
Using con As New OleDbConnection("YOUR CONNECTION STRING HERE")
    Using cmd As New OleDbCommand("SELECT TOP 1 cphone FROM cust WHERE cphone = ?", con)
        
        ' OleDb doesn't use named parameters, so only the order matters here:
        cmd.Parameters.AddWithValue("p0", TextBox5.Text)
        
        con.Open()
        Dim result As Object = cmd.ExecuteScalar()

        If result IsNot Nothing Then
            MsgBox("exist")
        Else
            MsgBox("NOT-exist")
        End If
    End Using
End Using


这篇关于检查电话号码是否存在于ms表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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