Click here to Skip to main content
14,695,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
create the dynamic tables in sql server through C# based on input given by the user.
when user giving his/her name the c# code has to create table in sql server in their user name
string crt_table = "create table"+ Textbob_dept.Text +"Faculty(FacultyID nchar(30),Username nchar(30),Password nchar(30))";

SqlCommand com_tbl = new SqlCommand(crt_table, cnn);

help me to understand with example in step by step process..
Updated 13-Nov-14 18:19pm
DamithSL 11-Nov-14 9:47am
any exceptions when you execute your sql statement?
you may need some spaces
string crt_table = "create table "+ Textbob_dept.Text +"Faculty (FacultyID nchar(30), [Username] nchar(30), [Password] nchar(30))";
Shweta N Mishra 11-Nov-14 11:25am
you should also check if the table exists, if not then only create.

Otherwise you will get an error.
Elaa_zx 11-Nov-14 21:44pm
table doesn't in that name..
i am creating it in new database
VC.J 12-Nov-14 2:03am
Any exception you have get while executing the code ?
Thava Rajan 12-Nov-14 2:52am
what kind of design it is?!!!
why you need to create a table for each faculty?
pradiprenushe 14-Nov-14 1:02am
Please check for details
Jörgen Andersson 14-Nov-14 1:44am
I'm with Thava here, if you need to create tables dynamically you have failed at normalization.
Read more about it here:
Add an extra column for the department instead.
Tomas Takac 14-Nov-14 2:01am
What is your question exactly? You apparently know that your code creates a table based on user's input. So what part is not clear? Do you want to get rid of the dynamic tables and you are asking how? Then you should probably explain why are you creating the tables dynamically in the first place.
Sinisa Hajnal 14-Nov-14 3:25am
Be aware of SQL injection attack potential. Never trust user input.

Also, what is the question here?

CREATE PROCEDURE sproc_CreateTableAtRuntime
           @TableName NVARCHAR(128)
          ,@Column1Name NVARCHAR(32)
          ,@Column1DataType NVARCHAR(32)
          ,@Column1Nullable NVARCHAR(32)


SET @SQString = 'CREATE TABLE ' + @TableName + '( '+ @Column1Name + ' ' + @Column1DataType + ' '+ @Column1Nullable +') ON PRIMARY '

EXEC (@SQLString)

Call this sp and Send the above parameters from c# code
In case if you need more columns add in the store procedure as I have given.
If you believe that column generation will be dynamic then then just send the columns and its data types from Code side.... Please let me know if nay quaries... If you are satisfied Please mark it as answer
Hameed Khan 17-Apr-16 2:59am
Hi.I have a question,I implement your stored procedure,table is created but table contain only one column.I pass values into the query which is coming from gridview

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