Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中使用数据读取器从多个表中读取数据?_Vb.net - Fatal编程技术网

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

如何在vb.net中使用数据读取器从多个表中读取数据?,vb.net,Vb.net,以下是我的代码,我无法执行数据读取器,它不断显示错误,因为已经有一个打开的数据读取器与此命令关联,必须先关闭。因此,是否有人可以帮助解决我代码的错误 Dim selectSql As String Dim selectSql2 As String conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Assignment.mdf

以下是我的代码,我无法执行数据读取器,它不断显示错误,因为已经有一个打开的数据读取器与此命令关联,必须先关闭。因此,是否有人可以帮助解决我代码的错误

    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

大多数数据库对象都需要关闭并释放(调用了它们的
.Dispose
方法)
使用…End-Using
块,即使出现错误,也要为您解决这个问题

对于Sql Server,请使用参数的
.Add
方法。看见 和 还有一个: 这是另一个

请注意,在创建第二个读卡器之前,第一个读卡器由
端关闭并使用
处理。这两个命令的连接都保持打开状态

我真的不喜欢在连接打开时更新用户界面,但我们将把它留到下一次

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

建议,为什么不在sql字符串中使用
internal JOIN
,将两个表中的连接值合并到一个
SqlDataReader
。例如:
在[Education Background].[Full Name]=[Employment History].[Full Name]上的[Education Background]内部加入[Employment History],其中[Full Name]=@fullname那么你不需要单独处理每个读者。这是否回答了你的问题。这与您的问题不完全相同,但问题本身显示了一种方式,它正在为旧系统寻找替代方案。很抱歉,我对vb.net还是很陌生,因此对于内部连接,只是select语句不同,而其他代码保持不变?或者你能帮我修改代码吗,谢谢
internaljoin
sql查询
,没有任何与
vb.net
相关的内容。根据prev.comment中提供的建议,您不再需要
cmd2
dr2
,所有内容都在
dr
中。如果我想从combobox中读取数据,如何检索数据,因为我尝试使用这些代码,但失败了。cboProficiency.Text=dr(“熟练程度”)。ToStringI在您的原始代码中没有看到cboProficiency。是,我没有在我的原始代码中添加该选项,但现在我想在组合框中检索数据。这是一个已经包含项目的现有组合框吗?是的,组合框包含项目,但我想读取数据并在其中显示,同时更新它。