【发布时间】:2013-10-03 20:44:26
【问题描述】:
我想在一个循环中使用 C# 创建两个 SQL 表。每个表都是不同的,并且其列名存储在一个数组中。每个列名数组其实都是从一个csv文件的头部获取的。
### fnames is an array of file paths (2 csv files)
foreach string f in fnames)
{
## snip
using (StreamReader rdr = new StreamReader(f))
{
string header = read.line(); ## This is the array of table columns
}
string tab = Path.GetFileNameWithoutExtension(f);
string query = @"create table "+ tab + ..."; #I am not sure how to write the column names and types dynamically
}
想象一下:
- 表 1 的列是:日期(datetime)、值(int)
- 表 2 的列是:日期 (datetime)、ID (varchar(255))、返回 (int)
请注意,这两个表具有不同类型的不同列。 您对如何实现这一点有什么建议吗?
谢谢!
【问题讨论】:
-
header究竟包含什么? -
例如:
header = {"Date", "ID"}用于表 1。 -
这完全取决于您如何确定列的类型,确定如何从标题字符串中获取列名和类型列表,然后返回并处理表。
-
@Mariam 那你怎么知道类型?
-
这正是问题所在。让我们简化一下,让每个表的第一列为
datetime,所有其他列为varchar(255)。那么有没有办法做到这一点?