如何在OleDbAdapter中将变量用作表名

时间:2017-10-28 05:17:21

标签: vb.net variables oledbdataadapter

我有一个数据库" NBCDB.accdb"其中包含5个以上的表格 表名是" Brgy,Citymun,GenClass等。" 如何在oledbadaptor中使用变量tablename ("从variabletablename&#34中选择*;) 我想根据选择的组合框来更改表名

感谢。

这是我的代码:

Private Sub cmbCity_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbCity.SelectedIndexChanged

    Brgytxt = cmbCity.Text
    myConnToAccess = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NBCDB.accdb")
    myConnToAccess.Open()
    ds = New DataSet
    tables = ds.Tables
    da = New OleDbDataAdapter("SELECT * from Brgy", myConnToAccess)
    da.Fill(ds, "Brgy")
    With ComboBox13
        .DataSource = ds.Tables("Brgy")
        .DisplayMember = Brgytxt
        .SelectedIndex = -1
    End With
    TextBox9.Text = Brgytxt
End Sub

2 个答案:

答案 0 :(得分:0)

请检查以下源代码,我已根据您的源代码进行了编辑

Private Sub cmbCity_SelectedIndexChanged(sender As System.Object,e As System.EventArgs)处理cmbCity.SelectedIndexChanged

Brgytxt = cmbCity.Text
myConnToAccess = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NBCDB.accdb")
myConnToAccess.Open()
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("SELECT * from " & cmbCity.Text", myConnToAccess)
da.Fill(ds, "Brgy")
With ComboBox13
    .DataSource = ds.Tables(cmbCity.Text)
    .DisplayMember = Brgytxt
    .SelectedIndex = -1
End With
TextBox9.Text = Brgytxt

End Sub

希望,这对你有用。 感谢

答案 1 :(得分:0)

感谢Tim和Mahesh。

这是我到目前为止所做的,它给了我错误信息。

warning/error message

Private Sub cmbCity_SelectedIndexChanged(sender As System.Object,e As System.EventArgs)处理cmbCity.SelectedIndexChanged

 // that is array what i have
var arr = ['egypt', 'london', 'spain'];

// that is strings what i have
var str = "hello from egypt";
var str2 = "hello from london and spain";

function search_in_string(_string, _array){
	var out_arr=[];
	for(var key in _array){
		if(_string.indexOf(_array[key]) !=-1){
			out_arr.push(_array[key]);
		}
	}
	return out_arr;
}

console.log(search_in_string(str, arr));
console.log(search_in_string(str2, arr));

da.Fill(ds.Brgytext)< - 这行也能接受变量吗? (我有一个名为Brgytext或cmbCity.text的表格。) TIA