首页 > 编程 > .NET > 正文

ASP.NET如何存取SQL Server数据库图片

2024-07-10 13:03:00
字体:
来源:转载
供稿:网友

    sql server提供了一个特别的数据类型:image,它是一个包含binary数据的类型。下边这个例子就向你展示了如何将文本或照片放入到数据库中的办法。在这篇文章中我们要看到如何在sql server中存储和读取图片。 
   
     1、建立一个表:
  
    在sql server中建立这样结构的一个表:
  
  列名 类型 目的
  id integer 主键id
  imgtitle varchar(50) 图片的标题
  imgtype varchar(50) 图片类型. asp.net要以辨认的类型
  imgdata image 用于存储二进制数据
  
    2、存储图片到sql server数据库中
  
    为了能存储到表中,你首先要上传它们到你的web 服务器上,你可以开发一个web form,它用来将客户端中textbox web control中的图片入到你的web服务器上来。将你的 enctype 属性设置为:myltipart/formdata. 
   
  stream imgdatastream = file1.postedfile.inputstream;
  int imgdatalen = file1.postedfile.contentlength;
  string imgtype = file1.postedfile.contenttype;
  string imgtitle = textbox1.text;
  byte[] imgdata = new byte[imgdatalen];
  int n = imgdatastream.read(imgdata,0,imgdatalen);
  string connstr=((namevaluecollection)context.getconfig("appsettings"))["connstr"];
  
  sqlconnection connection = new sqlconnection(connstr);
  
  sqlcommand command = new sqlcommand
           ("insert into imagestore(imgtitle,imgtype,imgdata)
           values ( @imgtitle, @imgtype,@imgdata )", connection );
  
  sqlparameter paramtitle = new sqlparameter
           ("@imgtitle", sqldbtype.varchar,50 );
  
  paramtitle.value = imgtitle;
  command.parameters.add( paramtitle);
  
  sqlparameter paramdata = new sqlparameter( "@imgdata", sqldbtype.image );
  paramdata.value = imgdata;
  command.parameters.add( paramdata );
  
  sqlparameter paramtype = new sqlparameter( "@imgtype", sqldbtype.varchar,50 );
  paramtype.value = imgtype;
  command.parameters.add( paramtype );
  
  connection.open();
  int numrowsaffected = command.executenonquery();
  connection.close();
  
    3、从数据库中恢复读取
  
    现在让我们来从sql server中读取我们放入的数据吧!我们将要输出图片到你的浏览器上,你也可以将它存放到你要的位置。
  
  private void page_load(object sender, system.eventargs e)
  {
   string imgid =request.querystring["imgid"];
   string connstr=((namevaluecollection)
   context.getconfig("appsettings"))["connstr"];
   string sql="select imgdata, imgtype from imagestore where id = " + imgid;
   sqlconnection connection = new sqlconnection(connstr);
   sqlcommand command = new sqlcommand(sql, connection);
   connection.open();
   sqldatareader dr = command.executereader();
   if(dr.read())
   {
    response.contenttype = dr["imgtype"].tostring();
    response.binarywrite( (byte[]) dr["imgdata"] );
   }
   connection.close();
  }
  
    要注意的是response.binarywrite 而不是response.write.
  
    下面给大家一个用于c# winform的存入、读取程序。其中不同请大家自己比较!(为了方便起见,我将数据库字段简化为二个:imgtitle和imgdata。
  
  using system;
  using system.drawing;
  using system.collections;
  using system.componentmodel;
  using system.windows.forms;
  using system.data;
  using system.io;
  using system.data.sqlclient;
  
  namespace windowsapplication21
  {
   /// <summary>
   /// form1 的摘要说明。
   /// </summary>
   public class form1 : system.windows.forms.form
   {
    private system.windows.forms.button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private system.componentmodel.container components = null;
    private string connectionstring = "integrated security=sspi;initial catalog=;data source=localhost;";
    private sqlconnection conn = null;
    private sqlcommand cmd = null;
    private system.windows.forms.button button2;
    private system.windows.forms.picturebox pic1;
    private system.windows.forms.openfiledialog openfiledialog1;
    private string sql = null;
    private system.windows.forms.label label2;
    private string nowid=null;
  
   public form1()
   {
    //
    // windows 窗体设计器支持所必需的
    //
    initializecomponent();
    conn = new sqlconnection(connectionstring);
  
    //
    // todo: 在 initializecomponent 调用后添加任何构造函数代码
    //
   }
  
   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void dispose( bool disposing )
   {
    if (conn.state == connectionstate.open)
     conn.close();
    if( disposing )
    {
     if (components != null)
     {
      components.dispose();
     }
    }
    base.dispose( disposing );
  
   }
 

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表