通过Windows窗体搜索SQL

时间:2014-02-08 22:15:06

标签: sql vb.net visual-studio

我已经搜索了很长一段时间,但我只是不确定'如何'提出这个问题,以便谷歌知道我的意思。我有一个应用程序,我正在视觉工作室开发。现在它只是一个基本的Windows窗体,带有一个用户输入数据的搜索文本框和一个搜索按钮,用于搜索sql数据库并返回匹配的行。最终用户根据输入的学号搜索学生。我已经用两个虚拟学生填充了sql db,以便测试数据。当我输入第一个学号并单击搜索时,它可以正常工作。当我输入第二个数字时,再次正常工作。当我单击框中没有数据的搜索时,工作正常并抛出我的自定义错误消息。当我输入一个无效的数字,一个不在数据库中的数字时,没有任何反应。它仍然显示我刚刚成功检索到的前一条记录并清除文本框并将光标放回框中并使其聚焦(应该如此)但不告诉我它无法在数据库中找到该特定数字。我似乎无法弄清楚我的代码中如何或在哪里解决这个问题。

背景:我是自学编程语言(youtube片段,哈佛的免费在线'如何',谷歌,阅读大量书籍等)并掌握语法等基本原理,但没有我想要的地方附近。我喜欢开发软件作为一种爱好,并喜欢学习如何做到这一点。非常感谢任何帮助。温柔一点。 : - )

我已故意删除了敏感信息,并将其替换为“?”。这是我目前的代码:

    Private Sub SearchButton_Click(sender As System.Object, e As System.EventArgs) Handles SearchButton.Click

    If stunumtxtbox.Text = "" Then
        MsgBox("Please enter a student number.", MsgBoxStyle.Exclamation)
        stunumtxtbox.Select()
    Else
        Try
            con.ConnectionString = "Data Source=?\?;Initial Catalog=?;Persist Security Info=True;User ID=?;Password=?"
            con.Open()
        Catch se As SqlException
            MsgBox(se.Message)
        Finally

            Try
                Dim dt As New DataTable
                Dim ds As New DataSet
                Dim da As New SqlDataAdapter
                ds.Tables.Add(dt)
                da = New SqlDataAdapter("SELECT DISTINCT * FROM Student_Info WHERE studentId = '" & stunumtxtbox.Text & "'", con)
                da.Fill(dt)
                For Each DataRow In dt.Rows
                    If stunumtxtbox.Text = dt.Rows(0)("studentId").ToString Then
                        fnametxtbox.Text = dt.Rows(0)("firstName").ToString
                        mnametxtbox.Text = dt.Rows(0)("midleInitial").ToString
                        lnametxtbox.Text = dt.Rows(0)("lastName").ToString
                        addresstxtbox.Text = dt.Rows(0)("addressStreet").ToString
                        address2txtbox.Text = dt.Rows(0)("addressOption").ToString
                        citytxtbox.Text = dt.Rows(0)("addressCity").ToString
                        statetxtbox.Text = dt.Rows(0)("addressState").ToString
                        ziptxtbox.Text = dt.Rows(0)("addressZip").ToString
                        countrytxtbox.Text = dt.Rows(0)("addressCountry").ToString
                        celltxtbox.Text = dt.Rows(0)("contactcellphone").ToString
                        studentidtxtbox.Text = dt.Rows(0)("studentId").ToString
                    Else
                    End If
                Next
                con.Close()
            Catch se As SqlException
                MsgBox(se.Message)
            Finally
                stunumtxtbox.Clear()
                stunumtxtbox.Select()
                Try
                    StudentNameTextBox.Text = lnametxtbox.Text + "," + " " + fnametxtbox.Text + " " + mnametxtbox.Text
                Catch ex As Exception
                    MsgBox(ex.Message)
                Finally
                    fnamelabel.Visible = True
                    mnamelabel.Visible = True
                    lnamelabel.Visible = True
                    addressstreetlabel.Visible = True
                    address2label.Visible = True
                    addresscitylabel.Visible = True
                    addressstatelabel.Visible = True
                    addressziplabel.Visible = True
                    addresscountrylabel.Visible = True
                    celllabel.Visible = True
                    studentidlabel.Visible = True
                    editbutton.Enabled = True
                    editbutton.Visible = True
                End Try
            End Try
        End Try
    End If
End Sub

3 个答案:

答案 0 :(得分:0)

简化您的工作。如果您的数据表没有行,则表示您没有匹配项。

Dim count = da.Fill(dt)
If count = 0 then
  ' you have no matches
  ' do you logic for no matches here
Else
  ' fill data
 fnametxtbox.Text = dt.Rows(0)("firstName").ToString
 mnametxtbox.Text = dt.Rows(0)("midleInitial").ToString
 lnametxtbox.Text = dt.Rows(0)("lastName").ToString
 addresstxtbox.Text = dt.Rows(0)("addressStreet").ToString
 address2txtbox.Text = dt.Rows(0)("addressOption").ToString
 citytxtbox.Text = dt.Rows(0)("addressCity").ToString
 statetxtbox.Text = dt.Rows(0)("addressState").ToString
 ziptxtbox.Text = dt.Rows(0)("addressZip").ToString
 countrytxtbox.Text = dt.Rows(0)("addressCountry").ToString
 celltxtbox.Text = dt.Rows(0)("contactcellphone").ToString
 studentidtxtbox.Text = dt.Rows(0)("studentId").ToString
End If

Try/Catches适用于可能爆炸的事情,这会如何爆炸?简单连接不应该需要Try/Catch。我们在VB中使用&进行连接。外部的一个不会捕获所有异常,因为你只能将它的捕获能力缩小到SqlException

 Try
   StudentNameTextBox.Text = lnametxtbox.Text + "," + " " + fnametxtbox.Text + " " + mnametxtbox.Text
 Catch ex As Exception
   MsgBox(ex.Message)

答案 1 :(得分:0)

您的代码正在按照您的要求执行操作。如果它不是数据库中的ID,则填充并返回零行。这不是一个例外,因此它继续到连接关闭然后清除并选择您的搜索框。您需要检查返回的零行并将异常抛出到catch或只是从if / else中检出零的文本框。没有一种方法可以做到这一点......

-m

P.S。到' user3288293 '

你还对此感兴趣吗,还可以看看这里的最后一个答案,它有效。

答案 2 :(得分:0)

谢谢@DonA和@Michael帮助我!!使用您的信息和一些谷歌,我设计了以下代码,并很高兴地说它有效。 : - )

Private Sub SearchButton_Click(sender As System.Object, e As System.EventArgs) Handles SearchButton.Click

    If stunumtxtbox.Text = "" Then
        MsgBox("Please enter a student number.", MsgBoxStyle.Exclamation)
        stunumtxtbox.Select()
    Else
        Try
            Using connection As New SqlConnection("Data Source=?;Initial Catalog=?;Persist Security Info=True;User ID=?;Password=?")
                connection.Open()
                Dim dt As New DataTable
                Dim ds As New DataSet
                Dim da As New SqlDataAdapter
                ds.Tables.Add(dt)
                da = New SqlDataAdapter("SELECT DISTINCT * FROM Student_Info WHERE studentId = '" & stunumtxtbox.Text & "'", connection)
                Dim count = da.Fill(dt)

                If count = 0 Then
                    MsgBox("Student ID not found.", MsgBoxStyle.Critical)
                Else
                    fnamelabel.Visible = True
                    mnamelabel.Visible = True
                    lnamelabel.Visible = True
                    addressstreetlabel.Visible = True
                    address2label.Visible = True
                    addresscitylabel.Visible = True
                    addressstatelabel.Visible = True
                    addressziplabel.Visible = True
                    addresscountrylabel.Visible = True
                    celllabel.Visible = True
                    studentidlabel.Visible = True
                    EditStudentToolStripMenuItem.Enabled = True

                    fnametxtbox.Enabled = False
                    fnametxtbox.Visible = True
                    fnametxtbox.BorderStyle = BorderStyle.FixedSingle
                    fnametxtbox.TabStop = True
                    fnametxtbox.TabIndex = 1
                    fnametxtbox.BackColor = Color.White
                    fnametxtbox.ForeColor = Color.Black

                    mnametxtbox.Enabled = False
                    mnametxtbox.Visible = True
                    mnametxtbox.BorderStyle = BorderStyle.FixedSingle
                    mnametxtbox.TabStop = True
                    mnametxtbox.TabIndex = 2
                    mnametxtbox.BackColor = Color.White
                    mnametxtbox.ForeColor = Color.Black

                    lnametxtbox.Enabled = False
                    lnamelabel.Visible = True
                    lnametxtbox.BorderStyle = BorderStyle.FixedSingle
                    lnametxtbox.TabStop = True
                    lnametxtbox.TabIndex = 3
                    lnametxtbox.BackColor = Color.White
                    lnametxtbox.ForeColor = Color.Black

                    addresstxtbox.Enabled = False
                    addresstxtbox.Visible = True
                    addresstxtbox.BorderStyle = BorderStyle.FixedSingle
                    addresstxtbox.TabStop = True
                    addresstxtbox.TabIndex = 4
                    addresstxtbox.BackColor = Color.White
                    addresstxtbox.ForeColor = Color.Black

                    address2txtbox.Enabled = False
                    address2txtbox.Visible = True
                    address2txtbox.BorderStyle = BorderStyle.FixedSingle
                    address2txtbox.TabStop = True
                    address2txtbox.TabIndex = 5
                    address2txtbox.BackColor = Color.White
                    address2txtbox.ForeColor = Color.Black

                    citytxtbox.Enabled = False
                    citytxtbox.Visible = True
                    citytxtbox.BorderStyle = BorderStyle.FixedSingle
                    citytxtbox.TabStop = True
                    citytxtbox.TabIndex = 6
                    citytxtbox.BackColor = Color.White
                    citytxtbox.ForeColor = Color.Black

                    statetxtbox.Enabled = False
                    statetxtbox.Visible = True
                    statetxtbox.BorderStyle = BorderStyle.FixedSingle
                    statetxtbox.TabStop = True
                    statetxtbox.TabIndex = 7
                    statetxtbox.BackColor = Color.White
                    statetxtbox.ForeColor = Color.Black

                    ziptxtbox.Enabled = False
                    ziptxtbox.Visible = True
                    ziptxtbox.BorderStyle = BorderStyle.FixedSingle
                    ziptxtbox.TabStop = True
                    ziptxtbox.TabIndex = 8
                    ziptxtbox.BackColor = Color.White
                    ziptxtbox.ForeColor = Color.Black

                    countrytxtbox.Enabled = False
                    countrytxtbox.Visible = True
                    countrytxtbox.BorderStyle = BorderStyle.FixedSingle
                    countrytxtbox.TabStop = True
                    countrytxtbox.TabIndex = 9
                    countrytxtbox.BackColor = Color.White
                    countrytxtbox.ForeColor = Color.Black

                    celltxtbox.Enabled = False
                    celltxtbox.Visible = True
                    celltxtbox.BorderStyle = BorderStyle.FixedSingle
                    celltxtbox.TabStop = True
                    celltxtbox.TabIndex = 10
                    celltxtbox.BackColor = Color.White
                    celltxtbox.ForeColor = Color.Black

                    studentidtxtbox.Enabled = False
                    studentidtxtbox.Visible = True
                    studentidtxtbox.BorderStyle = BorderStyle.FixedSingle
                    studentidtxtbox.TabStop = True
                    studentidtxtbox.TabIndex = 11
                    studentidtxtbox.BackColor = Color.White
                    studentidtxtbox.ForeColor = Color.Black

                    fnamelabel.Visible = True
                    mnamelabel.Visible = True
                    lnamelabel.Visible = True
                    addressstreetlabel.Visible = True
                    address2label.Visible = True
                    addresscitylabel.Visible = True
                    addressstatelabel.Visible = True
                    addressziplabel.Visible = True
                    addresscountrylabel.Visible = True
                    celllabel.Visible = True
                    studentidlabel.Visible = True
                End If

                For Each DataRow In dt.Rows
                    If stunumtxtbox.Text = dt.Rows(0)("studentId").ToString Then
                        fnametxtbox.Text = dt.Rows(0)("firstName").ToString
                        mnametxtbox.Text = dt.Rows(0)("midleInitial").ToString
                        lnametxtbox.Text = dt.Rows(0)("lastName").ToString
                        addresstxtbox.Text = dt.Rows(0)("addressStreet").ToString
                        address2txtbox.Text = dt.Rows(0)("addressOption").ToString
                        citytxtbox.Text = dt.Rows(0)("addressCity").ToString
                        statetxtbox.Text = dt.Rows(0)("addressState").ToString
                        ziptxtbox.Text = dt.Rows(0)("addressZip").ToString
                        countrytxtbox.Text = dt.Rows(0)("addressCountry").ToString
                        celltxtbox.Text = dt.Rows(0)("contactcellphone").ToString
                        studentidtxtbox.Text = dt.Rows(0)("studentId").ToString
                        StudentNameTextBox.Text = lnametxtbox.Text + "," + " " + fnametxtbox.Text + " " + mnametxtbox.Text
                    Else
                        fnamelabel.Visible = False
                        mnamelabel.Visible = False
                        lnamelabel.Visible = False
                        addressstreetlabel.Visible = False
                        address2label.Visible = False
                        addresscitylabel.Visible = False
                        addressstatelabel.Visible = False
                        addressziplabel.Visible = False
                        addresscountrylabel.Visible = False
                        celllabel.Visible = False
                        studentidlabel.Visible = False
                    End If
                Next
            End Using
        Catch se As SqlException
            MsgBox(se.Message)
        Finally
        End Try
    End If

    stunumtxtbox.Clear()
    stunumtxtbox.Select()

End Sub