【问题标题】:Is there a way to SELECT a database table using a variable?有没有办法使用变量选择数据库表?
【发布时间】:2021-09-06 21:44:52
【问题描述】:

我试过这个说法:

string query = "SELECT question FROM '" + GlobalVariables.dbQCode + "' WHERE [question_code] = '" + GlobalVariables.questionCode + "' ";

当我运行代码时,它给了我一个异常:

查询中有语法错误。查询子句不完整。

有没有办法可以使用我的变量?我希望它工作,因为我希望这段代码也能工作:

if (comboBox1.Text == "General Education"){
                GlobalVariables.subjectCode = "GenEd_English";
                GlobalVariables.dbQCode = "Gen_Ed_Question_Items";
                GlobalVariables.dbCCode = "Gen_Ed_Choice_Bank";
                if (comboBox2.Text == "English")
                {
                    GlobalVariables.subjectName = "ENGLISH";
                }
                
}

【问题讨论】:

标签: c# sql database ms-access


【解决方案1】:

单引号 (') 表示字符串文字。对象名称,例如表名称,应该用引号括起来:

string query = "SELECT question FROM " + GlobalVariables.dbQCode + " WHERE [question_code] = '" + GlobalVariables.questionCode + "' ";
// Quotes removed here --------------^-----------------------------^

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多