Click here to Skip to main content
15,881,139 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
EX:
SQL
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..
Posted
Updated 13-Nov-14 17:19pm
v4
Comments
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?

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

AS

DECLARE @SQLString NVARCHAR(MAX)
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
 
Share this answer
 
Comments
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