简体   繁体   中英

Syntax error when defining a parameter for BULK INSERT

In the following C# code:

string filePath = @"C:\Users\Me\Documents\note.txt"
var SqlSaveCommand = new SqlCommand(@"BULK INSERT table FROM @filepath", con);
SqlSaveCommand.Parameters.Add(new SqlParameter("@filepath", filePath));

I am getting a syntax error near @filepath when executing the sql command.

However the following does work as expected:

BULK INSERT proxylist FROM 'C:\Users\Me\Documents\note.txt'

What am I doing wrong in specifying the @filepath parameter?

You can't parameterize the path for BULK INSERT . You will see the same error in Management Studio if you say:

BULK INSERT dbo.tablename FROM @wherever...

Also typically you need some WITH options there, eg to specify ROWTERMINATOR, FIELDTERMINATOR etc.

Why not build the whole command in your program?

string filePath = @"C:\Users\Me\Documents\note.txt"
string cmd = @"BULK INSERT proxylist FROM '" + @filepath + "';";
var SqlSaveCommand = new SqlCommand(cmd, con);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM