1

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. 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 (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();

 } 
3
  • can u print all query1 and get the actual queries add in the question ? Commented Apr 10, 2014 at 11:03
  • can you check MessageBox.Show(atributes[3]); what value you get for this and say?? Commented Apr 10, 2014 at 11:25
  • OK sloved. I had bad indexing. Thank you :) Commented Apr 10, 2014 at 11:34

2 Answers 2

2

I think you are trying to add a column to the table.

You missed COLUMN keyword in the statement before column name that is being added.

"ALTER TABLE reflextime ADD COLUMN " + atributes[k] + " VARCHAR(10) NOT NULL"
Sign up to request clarification or add additional context in comments.

1 Comment

No need COLUMN keyword in ADD
2

You need to use ExecuteNonQuery instead of ExecuteScalar

And also check your each atributes[k] fro value exist or not

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();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.