Create SQL Server table programmatically
Asked Answered
I

2

5

I need to programmatically create a SQL Server 2008 table in C# such that the columns of the table should be generated from a list of columns (each column name is the name of a row in the table)

My question is what is the command string to loop through the list of columns and creates the table's recorded:

List<string> columnsName = ["col1","col2","col3"]

I want to create a table with the columns in the columnsName. But since the list size in not constant, I need to loop through the list to generate the table columns.

Impotence answered 5/4, 2012 at 18:12 Comment(5)
0_o I don't know of any RDBMS where rows have names. In every RDBMS that I'm aware of, columns have names...Murther
Welcome to StackOverflow. When you're asking about something that varies widely (like support for specific SQL syntax), you should provide a more specific tag along with the generic one. In this case, since DBMSs vary widely in their SQL syntax, it's important to know what database you're using (MySQL, SQL Server, etc.). Some don't even allow creation of a database using SQL (DDML) statements. Please edit your question to add the tag for the database engine you're using, as it's pretty difficult to provide one answer that covers all of them. Thanks. :)Jardine
You can generate an SQL table from columns in DataTable. Is this any good?Maidinwaiting
I updated the question to be more precisely.Impotence
@Impotence Please let us know if you need more help or mark an answer as correct.Fighterbomber
F
12

The simple answer is

CREATE TABLE table_name
(
    column_name1 data_type,
    column_name2 data_type,
    column_name3 data_type,
    ....
)

from w3Schools.com

In C# use a string builder to concatenate the query and then execute the query.

StringBuilder query = new StringBuilder();
query.Append("CREATE TABLE ");
query.Append(tableName);
query.Append(" ( ");

for (int i = 0; i < columnNames.Length; i++)
{
    query.Append(columnNames[i]);
    query.Append(" ");
    query.Append(columnTypes[i]);
    query.Append(", ");
}

if (columnNames.Length > 1) { query.Length -= 2; }  //Remove trailing ", "
query.Append(")");
SqlCommand sqlQuery = new SqlCommand(query.ToString(), sqlConn);
SqlDataReader reader = sqlQuery.ExecuteReader();

Note: tableName, columnNames, and columnTypes would be replaced with what ever you are getting the data from. From your description it sounds like you are getting the column values from a query, so rather than using a for loop and arrays you will probably be using a while loop to iterate through the results to build the query. Let me know if you need an example using this method and I will make one tonight.

If you are having trouble with the syntax for creating the table you can try creating the table (or a sample table) in MS SQL Server Management Studio, then right click the table and select Script Table as\Create To\New Query Editor Window. This will show you the script it would use to build the query.

Fighterbomber answered 5/4, 2012 at 21:32 Comment(0)
K
1

Are you looking to implement something like an Entity–attribute–value model?

http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model

Karakoram answered 5/4, 2012 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.