在C#中从combobox获取sql表名称 [英] getting sql tables name from combobox in c#
问题描述
我应该从那个形式的组合框中获取连接到c#形式的sql中表名称之一的名称.我该怎么办?它的代码是什么?
i should get the name of one of the tables name in sql that is connected to the c# form from a combobox in that form .what should i do?how is it''s code?
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cnn.Open();
cmd.CommandText = "Insert into library.dbo.'"+cmbsubjectbox1.SelectedItem.ToString()+"' (number,title,[pub year],publisher,subject,[aut num])" +
"values('"+txtBookNum.Text+"','"+txtBookTitle.Text+"','"+txtPubYear.Text+"','"+txtPublisher.Text+"','"+str+"','"+txtAuNum.Text+"')";
SqlCommand newcmd = new SqlCommand();
newcmd.Connection = cnn;
newcmd.CommandText = "Insert into library.dbo.author ([aut num],fname,lname)values('" + txtAuNum.Text + "','" + txtAufName.Text + "','" + txtAulName.Text + "')";
newcmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
cnn.Close();
newcmd正确完成.但是cmd无法完成.这部分代码有错误
cmd.ExecuteNonQuery();
并且错误是'农业'附近的语法不正确."
农业"是组合框选择的表的名称.
而且我不知道如何纠正此错误
[digimanus在回答此问题时添加了代码]
newcmd was done correctly.but cmd can''t be done.and this part of code has error
cmd.ExecuteNonQuery();
and the error is ''Incorrect syntax near ''agriculture''.''
"agriculture" is the name of table that was selected by combobox.
and i dont know how to correct this error
[digimanus added the code from a reply to this question]
推荐答案
我这样绑定组合框:
I bind combobox like this:
cmbxDB.DisplayMember = "Name";
cmbxDB.ValueMember = "Name";
cmbxDB.DataSource = DBConnect.Instantiate.Run.GetAsyncDataTable(sqlQuery, CommandType.Text);
// Loads the tablenames from my db
要读取所选值,我使用:
To read the selected value i use:
String sqlQuery = String.Format(CentralLib.Queries.DatabaseTables,cmbxDB.Text, cmbxTables.Text);
因此使用了Text属性
So the Text property is used
这篇关于在C#中从combobox获取sql表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!