Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
this is c# code , the problem in the quotations for j1 .
please help .

C#
for (int j1=0;j1<y;j1++)
   {


          con.Open();
           using (SqlCommand command = new SqlCommand("alter table [empmaster] add [td['"+j1+"']] varchar(50) default 0 NOT NULL"))
           {
               command.Connection = con;
               command.ExecuteNonQuery();
           }
    }
Posted
Comments
Thomas Daniels 23-Dec-14 3:21am    
What exactly is the problem?
loai_maane 23-Dec-14 3:26am    
Unclosed quotation mark after the character string 'td['0'] varchar(50) default 0 NOT NULL'.
Incorrect syntax near 'td['0'] varchar(50) default 0 NOT NULL'.
this is the error
Sibasisjena 23-Dec-14 3:27am    
Hi loai_maane,

What exactly you are trying to do? You want to add column or what?
loai_maane 23-Dec-14 3:30am    
hello dear,
yes i created table and i want to add columns .
Sibasisjena 23-Dec-14 3:38am    
Hi loai_maane,
Please do check in sql server management studio before create dynamic query in c# code. That will help you a lot

ALTER TABLE takes a column name : http://msdn.microsoft.com/en-us/library/ms190273.aspx[^]
SQL
alter table [empmaster] add td1 varchar(50) default 0 NOT NULL

If you intend to add numbers to the end of the name then you should do :
C#
using (SqlCommand command = new SqlCommand("alter table [empmaster] add td"+j1+" varchar(50) default 0 NOT NULL"))
{
...
 
Share this answer
 
SQL
for (int j1=0;j1<y;j1++)
   {


          con.Open();
           using (SqlCommand command = new SqlCommand("alter table [empmaster] add [td"+j1+"] varchar(50) default 0 NOT NULL"))
           {
               command.Connection = con;
               command.ExecuteNonQuery();
           }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900