将连接字符串中的“数据源”声明为 VB.NET 中的“变量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16865411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:49:32  来源:igfitidea点击:

declare "data source" in a connection string as 'variable' in VB.NET

vb.netvariablesconnectionruntimedatasource

提问by ?u??d Rebel

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")

In the above code i want to change the "data source" and declare it as TextBox1.text So that the user will decide the data source at run time... Also i want the same for 'Table1' and 'ID' but how to do that??

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

回答by ?u??d Rebel

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

hey i got the code man !!!! its working .... thanks a lot for your support and suggestions @matzone

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

回答by matzone

I did it in different way ..

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

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()