0
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim bs As New BindingSource

    m_DataAdapter = New OleDbDataAdapter("SELECT * FROM Table1 ORDER BY ID", "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Users\rebel23\Desktop\sampledata.mdb")

    m_DataAdapter.Fill(m_DataSet)

    bs.DataSource = m_DataSet.Tables(0)
    BindingNavigator1.BindingSource = bs
    txtAnimal.DataBindings.Add("Text", bs, "TextBox6.text")
    txtSpecies.DataBindings.Add("Text", bs, "TextBox7.text")

在上面的代码中,我想更改“数据源”并将其声明为 TextBox1.text 以便用户在运行时决定数据源......我也希望 'Table1' 和 'ID' 相同,但如何要做到这一点??

4

2 回答 2

1
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim bs As New BindingSource
    Dim myTableName As String
    Dim myOrderingColumn As String
    Dim mybind1 As String
    Dim mybind2 As String

    myTableName = TextBox3.Text
    myOrderingColumn = TextBox4.Text
    mybind1 = TextBox6.Text
    mybind2 = TextBox7.Text

    m_DataAdapter = New OleDbDataAdapter("SELECT * FROM " & myTableName & " ORDER BY " & myOrderingColumn, "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TextBox5.Text)

    m_DataAdapter.Fill(m_DataSet)

    bs.DataSource = m_DataSet.Tables(0)
    BindingNavigator1.BindingSource = bs
    txtAnimal.DataBindings.Add("Text", bs, "" & TextBox6.Text & "")
    txtSpecies.DataBindings.Add("Text", bs, "" & TextBox7.Text & "")


End Sub

嘿,我得到了代码人!!!!它的工作....非常感谢您的支持和建议@matzone

于 2013-06-01T23:59:57.727 回答
0

我以不同的方式做到了..

Dim myConn = New OleDb.OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\rebel23\Desktop\sampledata.mdb;Jet OLEDB:Database Password=mypass")

Dim cmd As OleDbCommand 
Dim ds as DataSet = New DataSet

myConn.Open() '--> open conn
cmd = New OleDbCommand("SELECT * FROM Table1 ORDER BY ID", myConn)

cmd.Fill(ds, "predator") ' ---> named dataset as Predator

txtAnimal.DataBindings.Add("Text", ds.Tables("predator"), "'" & TextBox6.text & "'")
txtSpecies.DataBindings.Add("Text",ds.Tables("predator"), "'" & TextBox7.text & "'")

' ....
' .....

cmd.Dispose()
ds.Dispose()
cnn.Close()
于 2013-05-31T23:34:27.667 回答