SqlBulkCopy cannot access table

前端 未结 10 1058
渐次进展
渐次进展 2021-01-07 22:02

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:

         


        
相关标签:
10条回答
  • 2021-01-07 22:26

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

    Hope this helps

    0 讨论(0)
  • 2021-01-07 22:28

    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.

    0 讨论(0)
  • 2021-01-07 22:29

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

    0 讨论(0)
  • 2021-01-07 22:36

    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.

    0 讨论(0)
  • 2021-01-07 22:39

    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])

    0 讨论(0)
  • 2021-01-07 22:39

    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.

    0 讨论(0)
提交回复
热议问题