Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create Sql table through the click of button......
I use this command:-

C#
con.Open();

SqlCommand cmd = new SqlCommand("create table @table (@col1 @datatype1, @col2 @datatype2, @col3 @datatype3, @col4 @datatype4, @col5 @datatype5 )", con);

cmd.Parameters.Add(new SqlParameter("@table",textBox6.Text)); 
cmd.Parameters.Add(new SqlParameter("@col1", textBox1.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col2", textBox2.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col3", textBox3.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col4", textBox4.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col5", textBox5.Text.ToUpper()));

cmd.Parameters.Add(new SqlParameter("@dataType1", comboBox1.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType2", comboBox3.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType3", comboBox5.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType4", comboBox7.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType5", comboBox9.SelectedItem.ToString()));

cmd.ExecuteNonQuery();
con.Close();


Please Check this coding.................
Posted
Updated 18-Dec-13 2:12am
v3
Comments
[no name] 18-Dec-13 7:34am    
What is the problem you are facing..?
Member 10447111 18-Dec-13 12:44pm    
Incorrect syntax near '@table'.
Ganesh Raja 18-Dec-13 7:56am    
What is the error?
Member 10447111 18-Dec-13 12:44pm    
Incorrect syntax near '@table'.
Maciej Los 18-Dec-13 8:16am    
What about permissions?

Do you want to allow users to create unknown number of tables?

Please, read my comment to the question first.

If you want to execute SQL command to create table, you need to grant PERMISSIONS[^] to the current user.

Personally, i would not recommend to allow users to create unknown number of tables. Who will be able to manage them?

[EDIT]
You can't create query string in that way:
SQL
create table @table (@col1 @datatype1, @col2 @datatype2, @col3 @datatype3, @col4 @datatype4, @col5 @datatype5

Above code will never compiled!

You can create dynamic SQL[^] code, which is passed into EXEC or EXECUTE[^] method.

SQL
DECLARE @sql VARCHAR(255) = 'SELECT * FROM TableName'
EXEC(@sql)


For further information, please see:
Writing Secure Dynamic SQL in SQL Server[^]
Building Dynamic SQL In a Stored Procedure[^]
Two Handy Techniques for Creating Dynamic SQL in Stored Procedures[^]
Understanding how SQL Server executes a query[^]
How to Generate T-SQL Code by Using Dynamic SQL?[^]

Remember, you've been warned!

[EDIT]
 
Share this answer
 
v3
Comments
Member 10447111 18-Dec-13 12:52pm    
can u please tell me how to grant PERMISSIONS???????
Maciej Los 18-Dec-13 15:20pm    
Have a look here: GRANT Object Permissions[^]
Remember, you've been warned!
Maciej Los 18-Dec-13 15:34pm    
See updated answer!
Jashobanta 18-Dec-13 16:03pm    
Hey, when you are connecting you should have the permissions in your db for creating a database. you can go to Security-->logins-->select the user-->properties-->server roles.. you should be either listed in the dbcreator or serveradmin or sysadmin... by default sa account has sysadmin.. if you are not running it in enterprise, then you will have permissions by default...
Maciej Los 18-Dec-13 16:10pm    
Do you think i don't know it?
Please, carefully read my answer.
DO NOT DO IT IN THIS WAY.

Try creating a stored proc that can do this. Create the below stored procedure in your sql and do all the validations from your end on the same in the proc.

SQL
create proc createTable
(
@tableName varchar(30),
@column1 varchar(30),
@column1DataType varchar(30),
@column2 varchar(30),
@column2DataType varchar(30)
)
AS
DECLARE @CreateTableString NVARCHAR(MAX)
BEGIN
	IF EXISTS (SELECT NAME FROM sys.tables WHERE name=@tableName)
		BEGIN	
			PRINT ('TABLE ALREADY EXISTS');
                        RETURN -1;
		END
	ELSE
		BEGIN
			SET @CreateTableString='CREATE TABLE '+@tableName +'('+@column1+' ' +@column1DataType+','+@column2+' ' +@column2DataType+')'
			EXEC (@CreateTableString);
                        PRINT ('TABLE CREATED SUCCESSFULLY');
		END
END


Now from your front end, you have to create sql parameters with all values as strings and then execute it. They way it should be passed is like :-

SQL
EXEC createTable 'test1','test1c1','varchar(30)','test1c2','varchar(30)'


I guess this solves the issue well. I have tried running it in my system and it is fine.

Vote it and mark it as solution if it solves your problem..
Regards
Jashobanta
 
Share this answer
 
Comments
Maciej Los 18-Dec-13 16:07pm    
Do not repost answers! Instead of reposting, improve previous question.
Jashobanta 18-Dec-13 16:14pm    
I have not reposted, I have mentioned 2 different approaches to do the same. One is using entity framework which will be created based on your requirements and class structures..
But I can feel that you want to create numerous tables on button clicks.. which though I don't know why someone would do.. in this case, second approach will work fine, assuming that you know how to call a parameterized stored proc from ado.net.. and if it solves the issue, do mark it as solution..
Jashobanta 18-Dec-13 16:20pm    
sorry.. did not notice that question has been asked by someone else..my mistake Maciej, these basic suggestions are not for you. You can ignore em.. you can suggest any corrections though..
Maciej Los 18-Dec-13 16:22pm    
OK
If you want to create a table in such a manner, you might be getting errors as creating table needs some specific permissions on the database. But there is a way to do this. You can use Entity Framework Code first approach for this. You can create classes in your program and entity framework will be creating all the tables and relational mappings in your database at runtime.

Go through the below links and you can do it easily I guess.. not a tough thing to do..

http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx[^]

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx[^]

Vote it and mark it as solution if it solves your problem..
Regards
Jashobanta
 
Share this answer
 
Comments
Member 12033779 5-Jan-17 1:59am    
Hi dont want create at runtime
i want to create on button click
please help me
C#
SqlCommand cmd = new SqlCommand("create table @table (@col1,@datatype1, @col2, @datatype2, @col3 ,@datatype3, @col4 ,@datatype4, @col5 ,@datatype5 )", con);
 
Share this answer
 
Comments
Member 10447111 18-Dec-13 12:54pm    
Incorrect syntax near '@table'.
Maciej Los 18-Dec-13 15:23pm    
You can't build query in that way ;(
[EDIT]
Please, see my updated answer!
[/EDIT]
Jashobanta 18-Dec-13 16:08pm    
I don't think Grant or Deny can be used to allow an user for executing DML commands, not for DDL commands..
you can deny an user to do a select on a table or insert on a table but you cannot allow an user to create a table if the user is not listed in either sysadmin or dbcreator group..

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