简体   繁体   English

无法在C#中动态创建SQL表-“无效的列名”

[英]Unable to create SQL Table dynamically in c# — “invalid column name”

im trying to create a table with the name of a dynamically retrieved value 即时通讯试图创建一个具有动态检索值的名称的表

here is the code 这是代码

 string nok="";
        while (reader.Read())
            {
                noo = reader.GetInt32(3);
                nok = noo.ToString();
                MessageBox.Show(noo.ToString());
            }
        con.Close();
            var commandStr = "If not exists (select name from sysobjects where name = C"+nok+") CREATE TABLE C"+nok+"(A char(50),B char(50),C char(50),D char(50),E char(50),F char(50),G char(50),H char(50),I char(50),J char(50),K char(50),L char(50),M char(50),N char(50),O char(50))";
            MessageBox.Show(commandStr);   
        con.Open();    
        using (SqlCommand command = new SqlCommand(commandStr, con))
                command.ExecuteNonQuery();

but im getting an error invalid column name with that dynamic value 但是我收到一个错误的无效列名称与该动态值

You didn't wrap the string in quotes. 您没有将字符串用引号引起来。

When referencing the table as an identifier, quotes aren't needed. 作为标识符引用时,不需要引号。 Because it's an object name: 因为它是一个对象名称:

... CREATE TABLE C"+nok+"(A char(50), ...

becomes: 变成:

... CREATE TABLE C1(A char(50), ...

But when referencing the table's name as a value in the WHERE clause, it isn't an object identifier. 但是,当在WHERE子句中将表引用为时,它不是对象标识符。 It's a column value. 这是列值。 The name column holds string values, so it needs to be compared with a string: name列包含字符串值,因此需要与字符串进行比较:

... where name = 'C"+nok+"') CREATE ...

becomes: 变成:

... where name = 'C1') CREATE ...

The name of the table schould be between 'name' 表的名称应在“名称”之间

var commandStr = "If not exists (select name from sysobjects where name = 'C"+nok+"') CREATE TABLE C"+nok+"(A char(50),B char(50),C char(50),D char(50),E char(50),F char(50),G char(50),H char(50),I char(50),J char(50),K char(50),L char(50),M char(50),N char(50),O char(50))";

Easier way to check if a table/object is present 检查表/对象是否存在的更简便方法

IF OBJECT_ID('C" + nok + "') IS NULL CREATE TABLE ...

The problem is here: 问题在这里:

select name from sysobjects where name = C"+nok+"

When you run this in oracle the statement executed will be: 当您在oracle中运行此语句时,执行的语句将是:

select name from sysobjects where name = CWHATEVER

Since CWHATEVER is not in quotes it will be considered a column name instead of a string value. 由于CWHATEVER不在引号中,因此它将被视为列名而不是字符串值。 For this to work it needs to be in single quotes: 为此,它必须用单引号引起来:

select name from sysobjects where name = 'C"+nok+"'

However, this opens you up to SQL Injection. 但是,这使您可以进行SQL注入。 I would strongly advise you to use sql parameters instead. 我强烈建议您改用sql参数

该值需要用单引号引起来:

... (select name from sysobjects where name = 'C"+nok+"') ...

You shouldn't just add 'nok' into your SQL statement. 您不应该只是在SQL语句中添加“ nok”。 You need to use a parameter. 您需要使用一个参数。 Try something like this. 尝试这样的事情。 I just took a small snippit: 我只是拍了个小片段:

commandStr = "if not exists (select name from sysobjects where name = 'c@nok')";

And then later, when you have the command text, replace the parameter: 然后,当您拥有命令文本时,替换参数:

command.Parameters.AddWithValue("@nok", nok);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM