带访问数据库的Vb.net:您如何帮助我简化这些代码?

时间:2013-09-28 06:38:01

标签: vb.net ms-access

这是我的代码:

    'Set up connection string
    Dim cnString As String

    cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\testingDB.mdb"

    Dim sqlQRY As String = "SELECT * " & _
        "FROM   users " & _
        "WHERE  firstName = '" & TextBox1.Text & "' " & _
        "  AND  lastName  = '" & TextBox2.Text & "'"

    'Create connection
    Dim conn As OleDbConnection = New OleDbConnection(cnString)

    Try
        ' Open connection
        conn.Open()

        'create data adapter
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlQRY, conn)

        'create dataset
        Dim ds As DataSet = New DataSet

        'fill dataset
        da.Fill(ds, "user")

        'get data table
        Dim dt As DataTable = ds.Tables("user")

        'display data
        Dim row As DataRow

        If TextBox1.Text = dt.Rows(0).Item(1) And TextBox2.Text = dt.Rows(0).Item(2) And dt.Rows(0).Item(5) = 10 Then
            MsgBox("10")
        ElseIf TextBox1.Text = dt.Rows(0).Item(1) And TextBox2.Text = dt.Rows(0).Item(2) And dt.Rows(0).Item(5) = 9 Then
            MsgBox("9")
        ElseIf TextBox1.Text = dt.Rows(0).Item(1) And TextBox2.Text = dt.Rows(0).Item(2) Then
            MsgBox("SUCCESS")
        Else
            MsgBox("fail")
        End If




    Catch ex As OleDbException
        MsgBox("Error: " & ex.ToString & vbCrLf)
    Finally
        ' Close connection
        conn.Close()
    End Try

我正在尝试用相同的结果简化它。正如你所看到的if-else语句很混乱,但它可以100%运行。我确实希望if-else语句简单,并且与上面相同。

4 个答案:

答案 0 :(得分:1)

编辑以检查行(5)的值以获取DbNull值 尝试使用if-else语句:

        Dim row As DataRow = dt.Rows(0)
        Dim r5 as Object = row(5)
        If IsDbNull(r5) then r5 = 0

        If TextBox1.Text = row(1) And TextBox2.Text = row(2) Then
            Select Case r5
                Case 10, 9 : MsgBox(r5)
                Case Else : MsgBox("SUCCESS")
            End Select
        Else
            MsgBox("fail")
        End If

答案 1 :(得分:0)

Dim cmd as New OledbCommand
Dim sqlQRY As String = "SELECT  count(*) " & _
        "FROM   users " & _
        "WHERE  firstName = @fname " & _
        "  AND  lastName  = @lname"

cmd.parameters.add("@fname",TextBox1.Text)
cmd.parameters.add("@lname",TextBox2.Text)

Dim Cnt as Integer
cmd = new oledbcommand(sqlQRy,con)
Cnt = Convert.ToInt32( cmd.ExecuteScalar())
if  Cnt>=1 then
msgbox("Success")

else if 

msgbox("Failure")

end if 

答案 2 :(得分:0)

  1. 您必须知道您的SQL可以在逻辑上返回超过1行。您需要确保在读取要以此方式测试的数据时,SQL使用主键并仅返回1行,否则在添加更多数据时可能会出现问题。我建议您首先确保在执行if测试之前返回完整的1行(请参阅我对@SenthilKumar的评论)。当没有找到任何行并且不执行测试时,您需要向用户发送适当的消息。

  2. 如果您只有1行,则必须确保数据不为空(如果适用,这取决于列的定义方式)。

  3. 请记住,来自UI的数据可以用空格填充或者是大小写混合。您可能需要注意这一点。

  4. 您的IF语句不是太复杂。我不确定您要测试的是什么,但可以观察到您使用的是以下条件:

  5. TextBox1.Text = dt.Rows(0).Item(1)And TextBox2.Text = dt.Rows(0).Item(2)and dt.Rows(0).Item(5) TextBox1.Text = dt.Rows(0).Item(1)And TextBox2.Text = dt.Rows(0).Item(2)and dt.Rows(0).Item(5) TextBox1.Text = dt.Rows(0).Item(1)And TextBox2.Text = dt.Rows(0).Item(2)

    你可以看到表达式 dt.Rows(0).Item(1)和TextBox2.Text = dt.Rows(0).Item(2)很常见。您可以将值赋给布尔表达式并在测试中使用它。

    您也不应该在尝试范围内包含非数据库处理。

答案 3 :(得分:0)

更简单......

If TextBox1.Text = dt.Rows(0).Item(1) And TextBox2.Text = dt.Rows(0).Item(2)
    MsgBox("SUCCESS - " & format(dt.Rows(0).Item(5))) 
Else
    MsgBox("fail")
End If
相关问题