Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本框名称创建表时语法不正确_C#_Mysql_Sql - Fatal编程技术网

C# 从文本框名称创建表时语法不正确

C# 从文本框名称创建表时语法不正确,c#,mysql,sql,C#,Mysql,Sql,我试图根据textbox1中给出的名称创建一个表。以下代码中出现错误: “Ramesh”附近的语法不正确 这里Ramesh是textbox中的值 string Customername = Textbox1.text SqlCommand cmd7 = new SqlCommand("CREATE TABLE '" + CustomerName + "' (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discoun

我试图根据textbox1中给出的名称创建一个表。以下代码中出现错误:

“Ramesh”附近的语法不正确

这里Ramesh是textbox中的值

string Customername = Textbox1.text 
SqlCommand cmd7 = new SqlCommand("CREATE TABLE '" + CustomerName + "' (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discount int , DateTime datetime)",connection

从查询中表名的两侧删除

string Customername = Textbox1.text 
SqlCommand cmd7 = new SqlCommand("CREATE TABLE " + CustomerName + " (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discount int , DateTime datetime)",connection

从查询中表名的两侧删除

string Customername = Textbox1.text 
SqlCommand cmd7 = new SqlCommand("CREATE TABLE " + CustomerName + " (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discount int , DateTime datetime)",connection

表名不需要单引号

SqlCommand cmd7 = new SqlCommand("CREATE TABLE " + CustomerName + " (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discount int , DateTime datetime)",connection);
但奇怪的是,不要对MySQL使用
SqlCommand
。使用和相关的类

另外,我想说的是使用参数化查询,但由于您不能参数化列名,并且看起来您将列名作为输入,所以在将其放入查询之前,请使用强验证或使用白名单


您可以阅读:

表名不需要单引号

SqlCommand cmd7 = new SqlCommand("CREATE TABLE " + CustomerName + " (ItemCode int,Quantity int,PricePerQuantity int,Brand char(50),Discount int , DateTime datetime)",connection);
但奇怪的是,不要对MySQL使用
SqlCommand
。使用和相关的类

另外,我想说的是使用参数化查询,但由于您不能参数化列名,并且看起来您将列名作为输入,所以在将其放入查询之前,请使用强验证或使用白名单


您可以阅读:

错误的直接原因是不应将表名放在撇号中。大概是这样的:

// put IDisposable into using
using (SqlCommand cmd7 = new SqlCommand(
  // Keep SQL readable; "$" - C# 6.0 feature
  $@"CREATE TABLE {Textbox1.text}(
       ItemCode int,
       Quantity int, 
       PricePerQuantity int,
       Brand char(50),
       Discount int, 
       DateTime datetime)", 
  connection)) {

  cmd7.ExecuteNonQuery(); // execute and create the table
}

错误的直接原因是不应将表名放在撇号中。大概是这样的:

// put IDisposable into using
using (SqlCommand cmd7 = new SqlCommand(
  // Keep SQL readable; "$" - C# 6.0 feature
  $@"CREATE TABLE {Textbox1.text}(
       ItemCode int,
       Quantity int, 
       PricePerQuantity int,
       Brand char(50),
       Discount int, 
       DateTime datetime)", 
  connection)) {

  cmd7.ExecuteNonQuery(); // execute and create the table
}

谢谢是的,我试过使用这个,错误消失了,但表不是在数据库中创建的。谢谢。是的,我试着使用这个,错误消失了,但表并没有在数据库中创建。