Skip to content
Advertisement

C# Get correct filpath string for SQL update Statement

I have created a small WinForms Application (.Net 4.8, SQL Server 18). Now I want to update a picture in my database. For that I have created this method:

The picturepath will be set like that from a textbox:

and it will be passed to the method like:

If the method gets executed, I get the error ‘Picture not found’.

I think there is a problem with the file path because, if I execute the UPDATE statement directly in database with the filepath (copy from dircetory search) it is working fine.

So can you please help me, how I can pass the filepath from textbox in correct form to the paramter for the SQL query.

Advertisement

Answer

The filename must be specified as a string literal instead of parameter for OPENROWSET so you’ll need to specify that value in the SQL statement string:

Note the table name and file name values must be obtained from trusted sources and validated since the values are not parameterized. Also, avoid AddWithValue.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement