• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

使用asp.net和C#如何存取SqlServer2000中的Image(16)类型的字段

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing;

public partial class ImgRead : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
     {
         ImgDataRead();
     }
    
private void ImgDataRead()
     {
        
int ImgID = Convert.ToInt32(Request.QueryString["id"]);

         SqlConnection Con
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

         String SqlCmd
= "SELECT * FROM ImageStore WHERE ID = @ImageID";
         SqlCommand CmdObj
= new SqlCommand(SqlCmd, Con);
         CmdObj.Parameters.Add(
"@ImageID", SqlDbType.Int).Value = ImgID;
         Con.Open();
         SqlDataReader SqlReader
= CmdObj.ExecuteReader();
         SqlReader.Read();
         Response.ContentType
= (string)SqlReader["ImageContentType"];//设定输出文件类型       
        
//输出图象文件二进制数制
        
         Response.OutputStream.Write((
byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));
         Response.BufferOutput
= true;
         Con.Close();   

     }
}
写入image数据
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
     Int32 FileLength
= 0;
    
protected void Page_Load(object sender, EventArgs e)
     {

     }
    
protected void Button1_Click(object sender, EventArgs e)
     {
         HttpPostedFile UpFile
= UP_File.PostedFile;//HttpPostedFile对象,用于读取图象文件属性   
         FileLength = UpFile.ContentLength;

        
try
         {
            
if (FileLength == 0)
             {
                 lblMessage.Text
= "<b>请选择您要上传的文件</b>";
             }
            
else
             {
                 Byte[] FileByteArray
= new byte[FileLength];   //图象文件临时储存Byte数组   
                 Stream StreamObj = UpFile.InputStream;//建立数据流对像   
                
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度   
                 StreamObj.Read(FileByteArray, 0, FileLength);

                 SqlConnection Con
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
                 String SqlCmd
= "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
                 SqlCommand CmdObj
= new SqlCommand(SqlCmd, Con);
                 CmdObj.Parameters.Add(
"@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
                 CmdObj.Parameters.Add(
"@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录文件类型       
                
//把其它单表数据记录上传       
                 CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
                
//记录文件长度,读取时使用       
                 CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
                 Con.Open();
                 CmdObj.ExecuteNonQuery();
                 Con.Close();
                 lblMessage.Text
= "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功   

             }
         }
        
catch (Exception ex)
         {
             lblMessage.Text
= ex.Message.ToString();
         }

     }
}

读取:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Drawing;

public partial class ImgRead : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
     {
         ImgDataRead();
     }
    
private void ImgDataRead()
     {
        
int ImgID = Convert.ToInt32(Request.QueryString["id"]);

         SqlConnection Con
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

         String SqlCmd
= "SELECT * FROM ImageStore WHERE ID = @ImageID";
         SqlCommand CmdObj
= new SqlCommand(SqlCmd, Con);
         CmdObj.Parameters.Add(
"@ImageID", SqlDbType.Int).Value = ImgID;
         Con.Open();
         SqlDataReader SqlReader
= CmdObj.ExecuteReader();
         SqlReader.Read();
         Response.ContentType
= (string)SqlReader["ImageContentType"];//设定输出文件类型       
        
//输出图象文件二进制数制
        
         Response.OutputStream.Write((
byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));
         Response.BufferOutput
= true;
         Con.Close();   

     }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap