简体   繁体   English

在c#错误中MySql alter table

[英]MySql alter table in c# error

I am trying to perform 我正在努力表演

ALTER TABLE

command in my app, but when running, I am getting this error 我的应用程序中的命令,但运行时,我收到此错误

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(10) NOT NULL' at line 1

Here is my code: 这是我的代码:

for (int k = 0; k < dlzkaTab; k++)
{
 string query1 = "ALTER TABLE reflextime ADD " + atributes[k] + " VARCHAR(10) NOT NULL";
 MySqlCommand cmd = new MySqlCommand(query1, conect);
 cmd.ExecuteScalar();
 }

Can anyone please help me? 谁能帮帮我吗?

EDIT: 编辑:

Here is full code. 这是完整的代码。 In the firs for loop I am reading first row from xls file and I am putting it into array atributes. 在第一个for循环中,我正在从xls文件中读取第一行,我将它放入数组属性中。 As you can see, I was trying to print out every loaded cell. 如您所见,我试图打印出每个加载的单元格。 It worked well (It was printing correct values). 它运作良好(它打印正确的值)。 However after this for loop the array is printing nothing (empty messagebox). 但是在此for循环之后,数组不打印任何内容(空消息框)。

  for (int j = 2; j < colCount; j++)
  {
   string atr = xlRange.Cells[1, j].Text;
   atributes[j]=atr;
   MessageBox.Show(atributes[j]);
  }

 MessageBox.Show("Súbor načítaný");

 int dlzkaTab = atributes.Length;

 MessageBox.Show(atributes[1]);  //empty messagebox

 for (int k = 0; k < dlzkaTab; k++)
 {
 string query1 = "ALTER TABLE reflextime ADD COLUMN " + atributes[k] + " VARCHAR(10) NOT NULL";
 MySqlCommand cmd = new MySqlCommand(query1, conect);
 cmd.ExecuteScalar();

 } 

I think you are trying to add a column to the table. 我想你正在尝试在表中add一列。

You missed COLUMN keyword in the statement before column name that is being added. 您在添加的列名之前的语句中错过了COLUMN关键字。

"ALTER TABLE reflextime ADD COLUMN " + atributes[k] + " VARCHAR(10) NOT NULL"

You need to use ExecuteNonQuery instead of ExecuteScalar 您需要使用ExecuteNonQuery而不是ExecuteScalar

And also check your each atributes[k] fro value exist or not 并检查您的每个属性[k]值是否存在

Try this 尝试这个

 for (int k = 0; k < dlzkaTab; k++)
 {
 string query1 = "ALTER TABLE reflextime ADD " + atributes[k] + " VARCHAR(10) NOT NULL";
 MySqlCommand cmd = new MySqlCommand(query1, conect);
 cmd.ExecuteNonQuery();
 }

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

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