sara adly
sara adly

Reputation: 297

How to get Binary Image in SQL

I can use this function in c# to get the image binary

public static byte[] ImageToBinary(string imagePath)
{
            var fileStream = new FileStream(HttpContext.Current.Server.MapPath("~/" + imagePath), FileMode.Open, FileAccess.Read);
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);
            fileStream.Close();
            return buffer;
}

but I need to make the same in sql

I use this query

(SELECT * FROM OPENROWSET(BULK N'C:\Users\0000001_computers_415.jpeg', SINGLE_BLOB) AS CategoryImage)

But I get an error:

Msg 4861, Level 16, State 1, Line 1
Cannot bulk load because the file "C:\Users\0000001_computers_415.jpeg" could not be opened. Operating system error code 3(The system cannot find the path specified.).

Upvotes: 1

Views: 1601

Answers (1)

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67291

Your SQL Server can only find files of it's own context. You must either copy the file into a directory on your SQL Server's machine to access it with local rights or you must define a shared path and let your SQL Server read from there.

Your c:\users\... is very likely your own local machine's drive.

Upvotes: 5

Related Questions