【发布时间】:2013-11-09 23:34:18
【问题描述】:
最后,我想从 Access 数据库中获取 OLE 类型的图像并将其放入图片框中。在 C# 和 MS Access 2010 中使用 Visual Studio 2012。我的解决方案是与 Web 无关的应用程序。
所以这是查询代码。我正在构建一个对象 (Equipamento),其中包含一个 System.Drawing.Image 属性,这是问题的焦点。
OleDbConnection l = OleDbConnectionDAO.createConnection();
Equipamento eq = new Equipamento();
try
{
OleDbDataAdapter adapter = new OleDbDataAdapter(
"SELECT * FROM [APP_Equipamento_Geral] WHERE COD_ETIQ like '%"
+ codigo
+ " %'",
l);
DataSet ds = new DataSet();
adapter.Fill(ds, "[APP_Equipamento_Geral]");
string s = ds.Tables["[APP_Equipamento_Geral]"].Columns[16].ColumnName;
foreach (DataRow row in ds.Tables["[APP_Equipamento_Geral]"].Rows)
{
eq.NInventario = row["Codigo"].ToString();
eq.Modelo = row["MODELO"].ToString();
eq.Marca = row["Marca_"].ToString();
eq.GamaMedida = row["Gama Medida"].ToString();
if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
{
byte[] b = new byte[0];
b = (byte[])row["FOTO"];
eq.Img = getImageFromBytes(b);//Error caught here
}
//if (row["FOTO"] != DBNull.Value && row["FOTO"] != null)
//{
// byte[] b = (byte[])row["FOTO"];
// MemoryStream ms = new MemoryStream(b);
// eq.Img = Image.FromStream(ms); //Error caught here
//}
}
}
这里是辅助方法:
private Image getImageFromBytes(byte[] myByteArray)
{
System.IO.MemoryStream newImageStream
= new System.IO.MemoryStream(myByteArray, 0, myByteArray.Length);\
return Image.FromStream(newImageStream, true);
}
最后一段注释的代码是我的另一个尝试,它也给出了
参数无效
错误。有什么解决办法吗?
注意:如果我取出图像部分一切正常。
【问题讨论】:
-
图像如何进入数据库?根据它的编写方式,它可能不是普通的图像。
-
我不知道它是怎么放进去的。我只能访问它所在的表,仅此而已。在表格上显示“Microsoft Word 图片”,在结构视图中显示“OLE 对象”
标签: c# image oledb ms-access-2010