21

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);
 }
}
asked Jan 17, 2012 at 11:47
1
  • You should add more relevant tags, such as c# and a database, eg sql-server, ms-access -- sql is a generic tag and access is almost meaningless. Commented Jan 17, 2012 at 12:37

11 Answers 11

16

My issue was a bit different, turns out my table name was a reserved keyword in SQL so I had to do the following:

bulkCopy.DestinationTableName = $"{schema}.[{tableName}]";

Where schema is the target schema and tableName the target table name

From the documentation

DestinationTableName is a three-part name [database].[owningschema].[name]. You can qualify the table name with its database and owning schema if you choose. However, if the table name uses an underscore ("_") or any other special characters, you must escape the name using surrounding brackets as in ([database].[owningschema].[name_01])

answered Jun 21, 2017 at 13:58
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, this was the problem for me also. From msdn: DestinationTableName is a three-part name (<database>.<owningschema>.<name>). You can qualify the table name with its database and owning schema if you choose. However, if the table name uses an underscore ("_") or any other special characters, you must escape the name using surrounding brackets as in ([<database>.<owningschema>.<name_01>]).
thanks @bluedot I have updated my answer with you comment details
I decided to change the accepted answer to this one as it is more complete and references the official docs. Also, judging by the upvotes, it seems to be of more help in general than the previously accepted answer.
7

Check that user that connects to db has

GRANT ALTER ON [dbo].[TABLE_XXX] TO [appuser] 

as suggested in answer by Jhilden on MSDN forum.

answered Nov 20, 2013 at 13:49

1 Comment

This didn't solve my issue. I still can't access the tables even though the user is database owner
5

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.

answered Feb 14, 2012 at 20:31

1 Comment

according to Microsoft docs, the bulk copy needs Select and Insert permission.
2

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.

answered Jan 17, 2012 at 22:13

1 Comment

My connectionstring is: string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestDB.mdf;User Instance=True;Integrated Security=True;";
2

I had the same issue stating

tablename-object not found or insufficient privileges.

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:

  • remove Identity from the destination table
  • grant Alter right on destination table for that user
  • not use KeepIdentity

(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).

answered Jan 23, 2020 at 14:36

Comments

1

Bulkcopy expects the table to exists in the database. Also you should have access to this database or table.

answered Dec 7, 2015 at 12:13

Comments

1

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.

answered Aug 9, 2016 at 6:02

Comments

1

Andrij Ferents answer is valid.

The destination table must exist before calling SQLBulkCopy. It is a common newbie mistake.

E_net4
30.6k13 gold badges119 silver badges155 bronze badges
answered Jun 21, 2019 at 1:14

Comments

0

In my case, it's not a permission problem, but a special char in the table name problem ( parenthesis and & ).

Hope this helps

answered Sep 21, 2015 at 11:53

Comments

0

In my case, the problem was because of an existing Identity column

answered Sep 25, 2019 at 7:13

Comments

0

I resolved this error by changing the temp table to #TmpTable, once I changed the name in DestinationTableName it worked!!

bulkcopy.DestinationTableName = "#TmpTable";

Previously I had the temp table as "#TmpTableUpd"

answered Jun 12, 2024 at 6:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.