zoukankan      html  css  js  c++  java
  • Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control


          原文:   Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control

    主要是介绍在VS2005中如何将图片直接存入到数据库,当然利用了FileUpload这个控件

    下面我把主要代码贴上来(已经测试过了,没什么问题):

    Save:

    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    if (FileUpload1.HasFile)
            

                
    using(BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream))
                
    {
                    
    byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);

                    
    using (SqlConnection conn = new SqlConnection("server=.;database=IBatisNet;uid=sa;pwd="))
                    
    {
                        
    using (SqlCommand command = conn.CreateCommand())
                        
    {
                            command.CommandText 
    = @"INSERT INTO photo (photo) VALUES (@photo)";
                            command.Parameters.AddWithValue(
    "@photo", image);
                            conn.Open();
                            command.ExecuteNonQuery();
                        }

                    }

                }

            }

        }

    Display:

    protected void Button2_Click(object sender, EventArgs e)
        
    {
            Response.Clear();
            Response.ContentType 
    = "image/jpeg";

            
    using (SqlConnection conn = new SqlConnection("server=.;database=IBatisNet;uid=sa;pwd="))
            
    {
                
    using (SqlCommand command = conn.CreateCommand())
                
    {
                    command.CommandText 
    = "select top 1 photo from photo";
                    conn.Open();
                    
    byte[] imageData = (byte[])command.ExecuteScalar();

                    Response.BinaryWrite(imageData);
                    
                }


            }

        }
  • 相关阅读:
    [MSSQL]也说SQL中显示星期几函数
    ECMAScript旮里旮旯儿一(galigalaoer)
    [MSQL]RANK函数
    敏捷背后的理论
    敏捷软件开发 Agile software Development
    第三章 WebGL资源 WebGL Resources
    第一章 WebGL简介 Introduction
    [MSSQL]PIVOT函数
    《Javascript高级程序设计》读书笔记 Number对象
    visual studio 2010 冷门技巧分享
  • 原文地址:https://www.cnblogs.com/pw/p/460879.html
Copyright © 2011-2022 走看看