Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException(SqlException) on SqlBulkCopy #221

Closed
xinbingyiran opened this issue Sep 26, 2019 · 5 comments · Fixed by #246
Closed

InvalidOperationException(SqlException) on SqlBulkCopy #221

xinbingyiran opened this issue Sep 26, 2019 · 5 comments · Fixed by #246
Assignees

Comments

@xinbingyiran
Copy link

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

@Wraith2
Copy link
Contributor

Wraith2 commented Sep 26, 2019

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.

@roji roji transferred this issue from dotnet/corefx Sep 26, 2019
@cheenamalhotra
Copy link
Member

I'm looking towards a potential fix in NetCore here:

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

and in NetFx here:
updateBulkCommandText.AppendFormat("insert bulk {0} (", this.DestinationTableName);

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.

@cheenamalhotra cheenamalhotra self-assigned this Sep 26, 2019
@Wraith2
Copy link
Contributor

Wraith2 commented Sep 26, 2019

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.

@yukiwongky
Copy link
Contributor

yukiwongky commented Oct 7, 2019

@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

@xinbingyiran
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants