Sqlclient: InvalidOperationException(SqlException) on SqlBulkCopy

Created on 26 Sep 2019  ·  5Comments  ·  Source: dotnet/SqlClient

when I insert some rows into table named "User" with SqlBulkCopy,I got a Exception

"System.InvalidOperationException:“无法访问目标表“User”。""(The target table "User" could not be accessed.)

and the inner exception is

"SqlException: 关键字 'User' 附近有语法错误。"(There is a syntax error near the keyword 'User'.)

Most helpful comment

@hlkitty Attached is the nupkg with the fix from PR #246. Please verify if the fix works for you.
Microsoft.Data.SqlClient.1.1.0-dev.zip

@v-kaywon I download this and replaced with the nupkg,and it seems to be working well

All 5 comments

The word user is a keyword in TSQL. The query syntax you're using is wrong, consider escaping the table name as [user] or choosing another table name. This isn't a driver issue it's a syntax error.

I'm looking towards a potential fix in NetCore here: https://github.com/dotnet/SqlClient/blob/e1e3be4b564eea6023ba5b1ccd00917adf3b37e9/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs#L535
and in NetFx here:
https://github.com/dotnet/SqlClient/blob/e1e3be4b564eea6023ba5b1ccd00917adf3b37e9/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs#L630
to change to:

updateBulkCommandText.AppendFormat("insert bulk {0} (", "[" + this.DestinationTableName + "]");

Somewhere on these lines. I'll give it a try and create a PR soon.

If you want to escape it you probably want to use make SqlCommand.ParseAndQuoteIdentifier internal and then call it, it uses the name parser to sort out multi dotted identifiers and avoid accidental double escaping.

@hlkitty Attached is the nupkg with the fix from PR #246. Please verify if the fix works for you.
Microsoft.Data.SqlClient.1.1.0-dev.zip

@hlkitty Attached is the nupkg with the fix from PR #246. Please verify if the fix works for you.
Microsoft.Data.SqlClient.1.1.0-dev.zip

@v-kaywon I download this and replaced with the nupkg,and it seems to be working well

Was this page helpful?
0 / 5 - 0 ratings