创建了C#图片 - 如何通过SQL插入他们BLOB [英] Images Created with C# - How to Insert them in BLOB via SQL

查看:159
本文介绍了创建了C#图片 - 如何通过SQL插入他们BLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我是否捕捉屏幕快照的话。还有就是我对于它的代码

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 = Graphics.FromImage((Image)B_M_P);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));            

B_M_P.Save("img.jpg", ImageFormat.Jpeg);



代替本保存到一个形象,我想能送这个给我的SQL或MySQL和将它们存储在数据库中为BLOB。

instead of saving this to an Image, i wanna be able to send this to my SQL or MySQL and store them in the database as BLOB.

我知道 LINQ 以及查询数据库。我不知道是中间部分。

i know the LINQ as well to query the DB. What i don't know is the intermediate portion.


  • 什么样的​​数据类型将用于BLOB列插入,我的
    的猜测是,这将是一个字节[]

  • What kind of Data Type will be used to INSERT in a BLOB column, my guess is it will be a Byte[]?

如果它是一个字节,则转换是很容易的。

if it is a 'Byte', then the conversion is pretty easy.

ImageConverter imgc = new ImageConverter();
Byte[] temp = (byte[])imgc.ConvertTo(B_M_P,typeof(byte[]));



,这样以后我就可以准备好我的查询

so that afterwards i can prepare my query

"INSERT INTO EMPLOYEE (pic) VALUES ('"+temp+"');"




  • 如果没有,那么什么是类型 &安培; 如何转换

    • If not then WHAT IS THE TYPE & HOW TO CONVERT
    • 推荐答案

      您是正确的,你只需要把它在为一个字节数组。你的表的数据类型应该是 VARBINARY(最大值)

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

      请注意,有一个图片数据类型,但数据类型将在Microsoft SQL Server的未来版本中删除,因此微软建议大家改用VARBINARY为未来的兼容性。

      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.

      BLOB和VARBINARY(最大)是一样的。从SQL Server 2005中

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

      要解决这个问题,微软推出了VARCHAR(MAX),
      NVARCHAR(MAX)和VARBINARY(MAX)数据类型在SQL Server 2005这些
      数据类型可以保持数据的BLOB相同量可容纳(2 GB)和
      它们都存储在同一类型用于其它数据
      类型的数据页。当在MAX数据类型的数据超过8 KB,过流页面使用
      。 SQL Server 2005的自动分配的过流量指示器至
      中的网页,并知道如何操纵数据行它
      操纵其他数据类型的方式相同。你可以声明MAX数据
      类型的变量的存储过程或函数内部,甚至通过他们为
      变量。您也可以在里面字符串函数中使用它们。

      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.

      微软建议使用MAX的数据类型,而不是2005年。事实上,它将BLOB正在$的未来版本弃用SQL Server中的BLOB b $ b SQL Server中。

      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.

      图片数据我在上述类型顶部是旧式的BLOB数据类型的例子。

      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();
      }
      

      但也有一些陷阱,如果你是大文件时,请参见这个答案的SO 及其在深入了解在数据库中存储大量数据对象的链接为。

      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.

      这篇关于创建了C#图片 - 如何通过SQL插入他们BLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆