C# 以编程方式创建 SQL Server 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10033758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create SQL Server table programmatically
提问by wingman55
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)
我需要在 C# 中以编程方式创建一个 SQL Server 2008 表,这样表的列应该从列列表中生成(每个列名是表中一行的名称)
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.
我想创建一个包含columnsName. 但是由于列表大小不是恒定的,我需要遍历列表以生成表列。
采纳答案by Trisped
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 builderto concatenate the query and then execute the query.
在 C# 中,使用字符串生成器连接查询,然后执行查询。
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.
注意:tableName、columnNames 和 columnTypes 将替换为您从中获取数据的任何内容。从您的描述来看,您似乎是从查询中获取列值,因此您可能会使用 while 循环遍历结果以构建查询,而不是使用 for 循环和数组。如果您需要使用此方法的示例,请告诉我,我今晚会制作一个。
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.
如果您在创建表的语法方面遇到问题,您可以尝试在 MS SQL Server Management Studio 中创建表(或示例表),然后右键单击该表并选择脚本表作为\创建到\新查询编辑器窗口。这将向您显示用于构建查询的脚本。
回答by ficuscr
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
http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model

