【发布时间】:2016-12-24 01:08:33
【问题描述】:
所以我有这段代码可以将数据表上传到数据库表。
public bool TableCreator(DataTable dt, string tableName)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
/*#### Method needs to include adding apropreate amount of colunm headers ####*/
string colunmHeader = " (TestColunmHeader varchar(50));";
//Opens connection to the DataBase
con.Open();
//Creates a new Table in the Database
using (SqlCommand command = new SqlCommand("CREATE TABLE " + tableName + colunmHeader, con))
command.ExecuteNonQuery();
UploadConnection(dt, tableName);
return true;
}
catch (Exception)
{
return false;
}
}
}
如果我“硬编码”列名和数量,这很有效,但我需要在数据库中创建的新表具有与 dataTable 相同的列数。
我已经尝试了一些事情,但我只是缺乏执行此操作的知识。
【问题讨论】:
-
这显示了我拥有的批量插入,它根据我需要的应用程序中的数据表在 sql 数据表中创建列名。这就是它被否决的原因吗?