Images Created with C# - How to Insert them in BLOB via SQL

前端 未结 1 1637
情歌与酒
情歌与酒 2021-01-23 08:28

Let\'s say if I capture a screen shot. There is my code for it

int sWidth = 1600, sHeight = 1200;

Bitmap B_M_P = Bitmap(sWidth, sHeight);

Graphics gfx = Graphi         


        
相关标签:
1条回答
  • 2021-01-23 09:00

    Your are correct that you just need to turn it in to a byte array. The datatype of your table should be VarBinary(max).

    Note that there is a Image datatype, but that datatype will be removed in a future version of Microsoft SQL Server, so Microsoft is recommending everyone switch to VarBinary for future compatibility.


    BLOBs and VarBinary(max) are the same thing. From Understanding VARCHAR(MAX) in SQL Server 2005

    To solve this problem, Microsoft introduced the VARCHAR(MAX), NVARCHAR(MAX), and VARBINARY(MAX) data types in SQL Server 2005. These data types can hold the same amount of data BLOBs can hold (2 GB) and they are stored in the same type of data pages used for other data types. When data in a MAX data type exceeds 8 KB, an over-flow page is used. SQL Server 2005 automatically assigns an over-flow indicator to the page and knows how to manipulate data rows the same way it manipulates other data types. You can declare variables of MAX data types inside a stored procedure or function and even pass them as variables. You can also use them inside string functions.

    Microsoft recommend using MAX data types instead of BLOBs in SQL Server 2005. In fact, BLOBs are being deprecated in future releases of SQL Server.

    That Image data type I mentioned at the top is a example of the old-style BLOB datatype.


    Here is a example of how the code should be

    using(var cmd = SqlCommand("INSERT INTO EMPLOYEE (pic) VALUES (@pic);", connection)
    {
        cmd.Parameters.Add("@pic", temp);
        cmd.ExecuteNonQuery();
    }
    

    However there are some Gotchas if you are working with large files, see this SO answer and its links for in depth look at storing large data objects in the database.

    0 讨论(0)
提交回复
热议问题