mysql中imagin的类型_请问如何取出SQL数据库中的image类型的数据?感谢回复!! - 技术问答...

本人试图使用如下代码取出sql数据库中的image类型的数据,

但是提示“指定的转换无效”。

1、请问如何取出sql数据库中的image类型的数据?

2、请问sql数据库中的image类型存放的是不是图片在外存的绝对地址?

image类型数据的真正含义是什么?

感谢您的回复!!

--

//打开连接

con.open();

string   sql="select   id,img   from   test   where   id="+convert.toint32(this.textbox1.text)+"";

da2=new   sqldataadapter(sql,con);

//用相关的数据填充数据容器dataset

da2.fill(ds,"ds2");

this.picturebox2.image   =   (system.drawing.image)ds.tables["ds2"].rows[0][1];

con.close();

参考文章:

发表者:twodays

how   to:在   visual   c#   中直接将一个图片从数据库复制到   picturebox   控件

适用于

有关本文的   microsoft   visual   basic   .net   版本,请参阅   317670。

本任务的内容

摘要

要求

示例

缺陷

参考

概要

本分步指南介绍如何将存储在数据库中的图像直接复制到   windows   窗体上的   picturebox   控件,而无须将此图像保存到文件。

在   microsoft   visual   basic   6.0   中,如想直接在   picturebox

控件中显示数据库中的图像而不经过将二进制大对象   {blob)   数据保存到文件这一中间步骤,唯一的方法是将   picturebox

绑定到一个数据源,如   activex   data   objects   (ado)   数据控件或记录集。若不将图像保存到文件以供

loadpicture   语句使用,则没有办法以编程方式将   blob   加载到控件。

在本文中,我们将使用   system.io   基类中的   memorystream   对象将图像数据直接从数据库复制到   picturebox   控件。

返回页首

要求

下表概括了推荐使用的硬件、软件、网络结构以及所需的   service   pack:

安装在兼容   microsoft   windows   操作系统中的   microsoft   visual   studio   .net

用于测试的可用   microsoft   sql   server   实例或可用   microsoft   access   数据库

本文假定您熟悉下列主题:

visual   c#   .net   windows   窗体应用程序

数据库中的二进制大对象   (blob)   存储

ado.net   数据访问

返回页首

示例

使用以下结构创建一个   sql   server   或   access   表:create   table   blobtest

(

blobid   int   identity   not   null,

blobdata   image   not   null

)

打开   visual   studio   .net,然后新建一个   visual   c#   windows   应用程序项目。

从工具箱向默认的   form1   添加一个   picturebox   和两个   button   控件。将   button1

的   text   属性设置为   file   to   database,并将   button2   的   text

属性设置为   database   to   picturebox。

在窗体的代码模块顶部插入   using   语句:using   system.data.sqlclient;

using   system.io;

using   system.drawing.imaging;

将以下数据库连接字符串的声明添加到   public   class   form1

:system.windows.forms.form   类声明中,并根据需要调整连接字符串:         string   strcn

=   "data   source=localhost;integrated   security=sspi;initial

catalog=mydata";

将下面的代码插入   button1   (file

to   database)   的   click

事件过程中。根据需要调整到一个可用示例图像文件的可用路径。此代码可将图像文件从磁盘读入   byte   数组,然后使用一个参数化的

command   对象将数据插入数据库。try

{

sqlconnection   cn   =   new   sqlconnection(strcn);

sqlcommand   cmd   =     new   sqlcommand("insert   into   blobtest   (blobdata)   values   (@blobdata)",   cn);

string   strblobfilepath   =   @"c:\blue   hills.jpg";//modify   this   path   as   needed.

//read   jpg   into   file   stream,   and   from   there   into   byte   array.

filestream   fsblobfile   =     new   filestream(strblobfilepath,filemode.open,   fileaccess.read);

byte[]   bytblobdata   =   new   byte[fsblobfile.length];

fsblobfile.read(bytblobdata,   0,   bytblobdata.length);

fsblobfile.close();

//create   parameter   for   insert   command   and   add   to   sqlcommand   object.

sqlparameter   prm   =   new     sqlparameter("@blobdata",

sqldbtype.varbinary,   bytblobdata.length,   parameterdirection.input,

false,

0,   0,   null,   datarowversion.current,   bytblobdata);

cmd.parameters.add(prm);

//open   connection,   execute   query,   and   close   connection.

cn.open();

cmd.executenonquery();

cn.close();

}catch(exception   ex)

{messagebox.show(ex.message);}

将下面的代码插入   button2   (database   to   picturebox)   的   click

事件过程。此代码将行从数据库中的   blobtest   表检索到一个数据集,复制最新添加的图像到   byte   数组,然后到

memorystream   对象,接着将   memorystream   加载到   picturebox   控件的   image

属性。try

{

sqlconnection   cn   =   new   sqlconnection(strcn);

cn.open();

//retrieve   blob   from   database   into   dataset.

sqlcommand   cmd   =   new   sqlcommand("select   blobid,   blobdata   from   blobtest   order   by   blobid",   cn);

sqldataadapter   da   =   new   sqldataadapter(cmd);

dataset   ds   =   new   dataset();

da.fill(ds,   "blobtest");

int   c   =   ds.tables["blobtest"].rows.count;

if(c>0)

{       //blob   is   read   into   byte   array,   then   used   to   construct   memorystream,

//then   passed   to   picturebox.

byte[]   byteblobdata   =     new   byte[0];

byteblobdata   =   (byte[])(ds.tables["blobtest"].rows[c   -   1]["blobdata"]);

memorystream   stmblobdata   =   new   memorystream(byteblobdata);

picturebox1.image=   image.fromstream(stmblobdata);

}

cn.close();

}

catch(exception   ex)

{messagebox.show(ex.message);}

按   f5   键编译并运行该项目。

单击   file   to   database   按钮将至少一个示例图像加载到数据库。

单击   database   to   picturebox   按钮将保存的图像显示在   picturebox   控件中。

如果想能够直接将图像从   picturebox   控件插入数据库,则请添加第三个   button   控件,并将下面的代码插入其

click   事件过程。此代码将图像数据从   picturebox   控件检索到   memorystream   对象,将

memorystream   复制到一个   byte   数组,然后使用一个参数化的   command   对象将   byte

数组保存到数据库。try

{

sqlconnection   cn   =   new   sqlconnection(strcn);

sqlcommand   cmd   =   new   sqlcommand("insert   into   blobtest   (blobdata)   values   (@blobdata)",   cn);

//save   image   from   picturebox   into   memorystream   object.

memorystream   ms     =   new   memorystream();

picturebox1.image.save(ms,   imageformat.jpeg);

//read   from   memorystream   into   byte   array.

byte[]   bytblobdata   =   new   byte[ms.length];

ms.position   =   0;

ms.read(bytblobdata,   0,   convert.toint32(ms.length));

//create   parameter   for   insert   statement   that   contains   image.

sqlparameter   prm   =   new   sqlparameter("@blobdata",

sqldbtype.varbinary,   bytblobdata.length,   parameterdirection.input,

false,

0,   0,null,   datarowversion.current,   bytblobdata);

cmd.parameters.add(prm);

cn.open();

cmd.executenonquery();

cn.close();

}catch(exception     ex)

{messagebox.show(ex.message);}

运行该项目。单击   database   to   picturebox   按钮以显示刚才在   picturebox

控件中保存过的图像。单击新添加的按钮将此图像从   picturebox   保存到数据库。然后再次单击   database   to

picturebox   按钮以确认图像已正确保存。

返回页首

缺陷

此测试不适用于   access   和   sql   server

中的罗斯文示例数据库的雇员表中的照片列。存储在照片列中的位图图像用由   visual   basic   6.0   ole

container   控件创建的标题信息进行了包装。

如果需要使用   access   数据库测试此代码,则需要在

access   表中创建一个   ole   object   类型的列,并使用   microsoft   jet   4.0

provider   中的   system.data.oledb   名称空间代替   system.data.sqlclient

名称空间。

发表者:twodays

how   to:在   visual   c#   .net   中通过使用   ado.net   读写   blob   数据

适用于

本文的发布号曾为   chs309158

有关本文的   microsoft   visual   basic   .net   版本,请参见   308042。

有关本文的   microsoft   visual   j#   .net   版本,请参见   320629。

本文引用下面的   microsoft   .net   框架类库名称空间:

system.data.sqlclient

system.io

本任务的内容

概要

要求

创建项目

概要

在   ado.net   中,datareader   列、dataset   列或   command   参数不能使用

getchunk   和   appendchunk   方法。本文介绍如何使用   visual   c#   .net

读写二进制大对象   (blob)   字段。

返回页首

要求

下面的列表列出了推荐使用的硬件、软件、网络结构以及所需的   service   pack:

microsoft   windows   2000   professional、windows   2000

server、windows   2000   advanced   server   或   windows   nt   4.0

server

microsoft   visual   studio   .net

microsoft   sql   server

返回页首

创建项目

在您的   sql   server   罗斯文数据库中添加一个名为   myimages   的表。在该表中包含以下字段:

标识字段,名为"id",类型为   int。

字段,名为"description",类型为   varchar,长度为   50。

字段,名为"imgfield",类型为   image。

启动   visual   studio   .net,然后新建一个   visual   c#   windows   应用程序项目。

将两个   button   控件从工具箱拖到默认窗体   form1   上。

在"属性"窗口中,将   button1   的   text   属性更改为保存到数据库,将   button2   的   text   属性更改为保存到文件。

将下面的代码添加到"代码"窗口顶部:   using   system.data;

using   system.data.sqlclient;

using   system.io;

双击   button1,然后将以下代码添加到   button1_click   事件处理程序中:   {

sqlconnection   con   =   new   sqlconnection("server=darkover;uid=sa;pwd=password1;database=northwind");

sqldataadapter   da   =   new   sqldataadapter("select   *   from   myimages",   con);

sqlcommandbuilder   mycb   =   new   sqlcommandbuilder(da);

dataset   ds   =   new   dataset("myimages");

da.missingschemaaction   =   missingschemaaction.addwithkey;

filestream   fs   =   new   filestream(@"c:\winnt\gone   fishing.bmp",   filemode.openorcreate,   fileaccess.read);

byte[]   mydata=   new   byte[fs.length];

fs.read(mydata,   0,   system.convert.toint32(fs.length));

fs.close();

da.fill(ds,"myimages");

datarow   myrow;

myrow=ds.tables["myimages"].newrow();

myrow["description"]   =   "this   would   be   description   text";

myrow["imgfield"]   =   mydata;

ds.tables["myimages"].rows.add(myrow);

da.update(ds,   "myimages");

con.close();

}

双击   button2,然后将以下代码添加到   button2_click   事件处理程序中:   {

sqlconnection   con   =   new   sqlconnection("server=darkover;uid=sa;pwd=password1;database=northwind");

sqldataadapter   da   =   new   sqldataadapter("select   *   from   myimages",   con);

sqlcommandbuilder   mycb   =   new   sqlcommandbuilder(da);

dataset   ds   =   new   dataset("myimages");

byte[]   mydata=   new   byte[0];

da.fill(ds,   "myimages");

datarow   myrow;

myrow=ds.tables["myimages"].rows[0];

mydata   =     (byte[])myrow["imgfield"];

int   arraysize   =   new   int();

arraysize   =   mydata.getupperbound(0);

filestream   fs   =   new   filestream(@"c:\winnt\gone   fishing2.bmp",   filemode.openorcreate,   fileaccess.write);

fs.write(mydata,   0,arraysize);

fs.close();

}

按   f5   键编译并运行该应用程序。

单击"保存到数据库",将位于   c:\winnt\gone   fishing.bmp   的图像加载到   sql   server   image   字段。

单击"保存到文件",将   sql   server   image   字段的数据保存回文件中。

发表者:suosuoyyy

这段代码我侧过的呀,现在你已经把image字段读到byte[]

aaa里面呢,现在就是要根据aaa来构造bitmap嘛,你自己new一下,好像有很多种重载方法,根据要求该一下就行了,不一定要用

memorystream,好像可以直接传byte[],自己试试!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值