惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
小众软件
小众软件
D
Docker
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
博客园 - 叶小钗
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
IT之家
IT之家
博客园 - 司徒正美
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog

博客园 - David.C

ASP.net 验证码(C#) 这是我最近从网上摘抄的.net笔试题 office应用小技巧 正确显示出Word中的空格 仓库管理系统解决方案 餐饮业点菜系统解决方案 餐饮服务案例分析(预订) Asp.Net安全验证小结 - David.C - 博客园 客户关系管理的三大纪律 软件工程---网站项目管理 电子政务与办公自动化初探 详解加密技术概念、加密方法以及应用 痛苦的选择:不再只专注于技术(转载--http://zhenyulu.cnblogs.com/archive/2004/10/17/53443.aspx) 在C#中调用Microsoft.VisualBasic命名空间下的类型验证函数 用C#实现基于用C#实现基于TCP协议的网络通讯 MSDN 文章收藏 数据访问:使用 ADO.NET 的最佳实践(ADO.NET 技术文档) ASP.NET编程中的十大技巧 用ASP.NET上传图片并生成可带版权信息的缩略图 VS.NET 2003 控件命名规范
Asp.Net在SqlServer中的图片存取技术
David.C · 2005-10-30 · via 博客园 - David.C

在使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来

一,上传并存入SqlServer
 数据库结构
  create table test
  {
     id identity(1,1),
     FImage image
  }
  相关的存储过程
  Create proc UpdateImage
  (
     @UpdateImage Image
  )
  As
  Insert Into test(FImage) values(@UpdateImage)
  GO

在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>

然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
        //获得图象并把图象转换为byte[]
        HttpPostedFile upPhoto=UpPhoto.PostedFile;
        int upPhotoLength=upPhoto.ContentLength;
        byte[] PhotoArray=new Byte[upPhotoLength];
        Stream PhotoStream=upPhoto.InputStream;
        PhotoStream.Read(PhotoArray,0,upPhotoLength);

        //连接数据库
        SqlConnection conn=new SqlConnection();
        conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

        SqlCommand cmd=new SqlCommand("UpdateImage",conn);
        cmd.CommandType=CommandType.StoredProcedure;

        cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
        cmd.Parameters["@UpdateImage"].Value=PhotoArray;

        //如果你希望不使用存储过程来添加图片把上面四句代码改为:
        //string strSql="Insert into test(FImage) values(@FImage)";
        //SqlCommand cmd=new SqlCommand(strSql,conn);
        //cmd.Parameters.Add("@FImage",SqlDbType.Image);
        //cmd.Parameters["@FImage"].Value=PhotoArray;

 conn.Open();
 cmd.ExecuteNonQuery();
 conn.Close();
}

二,从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>

ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
     if(!Page.IsPostBack)
     {
                SqlConnection conn=new SqlConnection()
                conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
               
                string strSql="select * from test where id=2";//这里假设获取id为2的图片
                SqlCommand cmd=new SqlCommand()
                reader.Read();
                Response.ContentType="application/octet-stream";
                Response.BinaryWrite((Byte[])reader["FImage"]);
                Response.End();
                reader.Close();
     }
}

在winform中将图片存入sqlserver,并从sqlserver中读取并显示在picturebox中