SqlBulkCopy 无法访问表
读入 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);
}
}
After reading in an excel-sheet (to transferTable), I want to add that data to a new table (destinationTable) using SqlBulkCopy, but I'm getting the error:
Cannot access destination table 'test'
I've tried using the default tablename and using square brackets, but that didn't work.
Any suggestions?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我通过将临时表更改为 #TmpTable 解决了此错误,一旦我更改了 DestinationTableName 中的名称,它就起作用了!
以前我的临时表为“#TmpTableUpd”
I resolved this error by changing the temp table to #TmpTable, once I changed the name in DestinationTableName it worked!!
Previously I had the temp table as "#TmpTableUpd"
我的问题有点不同,结果我的表名是 SQL 中的保留关键字,所以我必须执行以下操作:
其中
schema
是目标架构,tableName
是目标表名称来自 文档
My issue was a bit different, turns out my table name was a reserved keyword in SQL so I had to do the following:
Where
schema
is the target schema andtableName
the target table nameFrom the documentation
检查连接到数据库的用户是否
按照 Jhilden 在 MSDN 论坛。
Check that user that connects to db has
as suggested in answer by Jhilden on MSDN forum.
我最近遇到了同样的错误,并在谷歌搜索答案时发现了这篇文章。我能够通过向正在执行大容量复制命令的用户授予对目标表的插入和选择权限来解决该问题。
最初,我只向用户授予插入权限,并收到“无法访问目标表”错误。
I recently ran into this same error and came across this post while googling for an answer. I was able to solve the problem by giving the user that is executing the bulk copy command insert and select permissions on the destination table.
Originally I had only granted insert permission to the user and got the 'Cannot access destination table' error.
执行此代码的用户似乎没有正确访问数据库的权限。
* 检查以便用户获得访问权限。
* 检查您用于连接数据库的连接字符串。
It seems that the user who executes this code don't have proper access to the database.
* Check so that the user got access.
* Check the connectionstring your using to connect to the database.
我有同样的问题说明
它在我的帐户上运行良好,但在最终用户帐户上运行良好,并出现此错误。事实证明,如果您使用
SqlBulkCopyOptions.KeepIdentity
作为选项运行批量复制,连接用户需要 Grant Alter 权限,如果没有,您将收到此不太有用的错误消息。选项之一是:
(这是 Fosna 答案的扩展 但考虑到我花了时间来确定根本原因,我认为可能值得让这个解决方案更明确一些)。
I had the same issue stating
It worked fine on my account but not on the end users account, where it gave this error. It turned out that if you run bulkcopy with
SqlBulkCopyOptions.KeepIdentity
as option, the connection user needs the Grant Alter right, if he doesn't, you will get this not very helpful error message.options one has:
(this is an extension of Fosna's answer but given the time it took me to identify the root cause I thought it might be worth to make this solution a bit more explicit).
Bulkcopy 期望该表存在于数据库中。您还应该有权访问该数据库或表。
Bulkcopy expects the table to exists in the database. Also you should have access to this database or table.
有趣的是,如果您有一个纯数字的表名,也会发生这种情况。以一个或多个字母字符开头的表名称,效果很好。
Interestingly, this also happens if you have a table name which is purely numeric. Start the table name with one or more alpha characters and it works just fine.
Andrij Ferents 答案有效。
调用 SQLBulkCopy 之前目标表必须存在。这是新手常见的错误。
Andrij Ferents answer is valid.
The destination table must exist before calling
SQLBulkCopy
. It is a common newbie mistake.就我而言,这不是权限问题,而是表名问题中的特殊字符(括号和 & )。
希望这有帮助
In my case, it's not a permission problem, but a special char in the table name problem ( parenthesis and & ).
Hope this helps
就我而言,问题是因为现有的 Identity 列
In my case, the problem was because of an existing Identity column