如何使用vb.net中的数据读取器从多个表中读取数据?

时间:2020-10-16 13:49:08

标签: vb.net

以下是我的代码,我无法执行数据读取器,它始终显示错误“已经存在与此Command关联的打开的DataReader,必须首先将其关闭。”那么任何人都可以帮助解决我的代码错误吗?

    Dim selectSql As String
    Dim selectSql2 As String

    conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30")

    selectSql = "Select [Field of Study Deg], [Major Deg], [Institute Or University Deg], [Grade Deg],[Graduation Date Deg], [Field of Study Dip], [Major Dip], [Institute Or University Dip], [Grade Dip], [Graduation Date Dip] From [Education Background] Where [Full Name] = @fullname"

    selectSql2 = "Select [Company Name 1], [Position Title 1], [Position Level 1], [Specialization 1], [Industry 1], [Duration 1], [Work Description 1], [Company Name 2], [Position Title 2], [Position Level 2], [Specialization 2], [Industry 2], [Duration 2], [Work Description 2] From [Employment History] Where [Full Name] = @fullname"

    conn.Open()

    'create the command 
    'the command is used for executing the SQL statement
    cmd = New SqlCommand(selectSql, conn)
    cmd.Parameters.AddWithValue("@fullname", txtFullname.Text)
    dr = cmd.ExecuteReader() 'executes the reader to read record

    cmd2 = New SqlCommand(selectSql2, conn)
    cmd2.Parameters.AddWithValue("@fullname", txtFullname.Text)
    dr2 = cmd2.ExecuteReader()

    If dr.HasRows And dr2.HasRows Then
        btnEdit.Enabled = True
        btnBrowse.Enabled = True

        'read the data from the record 
        dr.Read()
        dr2.Read()

        'display 
        txtField1.Text = dr("Field of Study Deg").ToString
        txtMajor1.Text = dr("Major Deg").ToString
        txtInstitute1.Text = dr("Institute Or University Deg").ToString
        txtGrade1.Text = dr("Grade Deg").ToString
        mskGradDate1.Text = dr("Graduation Date Deg").ToString

        txtCompany1.Text = dr2("Company Name 1").ToString
        txtPosTitle1.Text = dr2("Position Title 1").ToString
        txtPosLevel1.Text = dr2("Position Level 1").ToString
        txtSpecial1.Text = dr2("Specialization 1").ToString
        txtIndustry1.Text = dr2("Industry 1").ToString
        txtDuration1.Text = dr2("Duration 1").ToString
        txtWork1.Text = dr2("Work Description 1").ToString

    Else
        'record not found
        MessageBox.Show("Full name cannot be found. Please check the name again.", "Unable to Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If
    conn.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

大多数数据库对象都需要关闭和处理(调用它们的.Dispose方法)Using...End Using块,即使有错误,也可以为您解决这一问题。

对于Sql Server,请使用.Add方法作为参数。参见http://www.dbdelta.com/addwithvalue-is-evil/https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications 这是另一个 https://andrevdm.blogspot.com/2010/12/parameterised-queriesdont-use.html

请注意,在创建第二个阅读器之前,第一个阅读器已由End Using关闭并处置。这两个命令的连接均保持打开状态。

我真的不喜欢在连接打开时更新用户界面,但是我们会再等一次。

Private Sub OPCode()
    Dim selectSql = "Select [Field of Study Deg], [Major Deg], [Institute Or University Deg], [Grade Deg],[Graduation Date Deg], [Field of Study Dip], [Major Dip], [Institute Or University Dip], [Grade Dip], [Graduation Date Dip] From [Education Background] Where [Full Name] = @fullname"
    Dim selectSql2 = "Select [Company Name 1], [Position Title 1], [Position Level 1], [Specialization 1], [Industry 1], [Duration 1], [Work Description 1], [Company Name 2], [Position Title 2], [Position Level 2], [Specialization 2], [Industry 2], [Duration 2], [Work Description 2] From [Employment History] Where [Full Name] = @fullname"

    Using conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30")
        Using cmd As New SqlCommand(selectSql, conn)
            cmd.Parameters.Add("@fullname", SqlDbType.VarChar, 200).Value = txtFullname.Text
            conn.Open()
            Using dr = cmd.ExecuteReader
                dr.Read()
                txtField1.Text = dr("Field of Study Deg").ToString
                txtMajor1.Text = dr("Major Deg").ToString
                txtInstitute1.Text = dr("Institute Or University Deg").ToString
                txtGrade1.Text = dr("Grade Deg").ToString
                mskGradDate1.Text = dr("Graduation Date Deg").ToString
            End Using
        End Using
        Using cmd2 As New SqlCommand(selectSql2, conn)
            cmd2.Parameters.Add("@fullname", SqlDbType.VarChar, 200).Value = txtFullname.Text
            Using dr2 = cmd2.ExecuteReader
                dr2.Read()
                txtCompany1.Text = dr2("Company Name 1").ToString
                txtPosTitle1.Text = dr2("Position Title 1").ToString
                txtPosLevel1.Text = dr2("Position Level 1").ToString
                txtSpecial1.Text = dr2("Specialization 1").ToString
                txtIndustry1.Text = dr2("Industry 1").ToString
                txtDuration1.Text = dr2("Duration 1").ToString
                txtWork1.Text = dr2("Work Description 1").ToString
            End Using
        End Using
    End Using
End Sub