【发布时间】:2012-02-12 05:22:50
【问题描述】:
在读取 excel 表(传输表)后,我想使用 SqlBulkCopy 将该数据添加到新表(destinationTable),但出现错误:
Cannot access destination table 'test'
我尝试过使用默认表名和方括号,但没有奏效。
有什么建议吗?
private void writeToDBButton_Click(object sender, EventArgs e) {
MakeTable();
destinationTable.TableName = "test";
testDBDataSet.Tables.Add("test");
// Connects to the sql-server using Connection.cs
SqlConnection connection = Connection.GetConnection();
using (connection) {
connection.Open();
// Uses SqlBulkCopy to copy the data from our transferTable to the destinationTable
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) {
bulkCopy.DestinationTableName = destinationTable.TableName;
try {
// Write from the source to the destination.
bulkCopy.WriteToServer(transferTable);
this.dataGridView2.DataSource = destinationTable;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
connection.Close();
}
}
}
private void saveDBButton_Click(object sender, EventArgs e) {
this.Validate();
this.usersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.testDBDataSet);
}
private void MakeTable() {
for (int counter = 0; counter < columns; counter++) {
DataColumn dummy = new DataColumn();
dummy.DataType = System.Type.GetType("System.Double");
destinationTable.Columns.Add(dummy);
}
}
【问题讨论】:
-
你应该添加更多相关标签,例如
c#和一个数据库,例如sql-server,ms-access--sql是一个通用标签,access几乎没有意义。
标签: c# sql-server database sqlbulkcopy