1

i'm having a error message sent everytime i try to copy data from a data table to an sql database using Sqlbulkcopy. This is the code i'm using:

static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)
 {
 using (SqlConnection dbConnection = new SqlConnection(@"Data Source=DESKTOP-LNCGI78\SQLEXPRESS;Initial Catalog=Testdatabase;Integrated Security=SSPI;"))
 {
 dbConnection.Open();
 using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
 {
 s.DestinationTableName = "dbo.Table";
 foreach (var column in csvFileData.Columns)
 s.ColumnMappings.Add(column.ToString(), column.ToString());
 s.WriteToServer(csvFileData);
 }
 }
 }

This is the error :System.InvalidOperationException: 'Cannot access destination table. i checked permissions to write in the table, all seems fine and i tried to insert some rows in it with success. I don't know what i'm missing or what i did wrong.

mjwills
24.1k6 gold badges45 silver badges74 bronze badges
asked Oct 26, 2018 at 9:48
1

2 Answers 2

1

The word Table is a reserved word in SQL Server and as such you need to add brackets:

s.DestinationTableName = "dbo.[Table]";
answered Oct 26, 2018 at 10:02
Sign up to request clarification or add additional context in comments.

Comments

0

I had a similiar problem what I did is just s.DestinationTableName="[TableName]". So no dbo.[TableName] just [TableName]

answered Oct 26, 2018 at 10:07

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.