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);
                    
                }


            }

        }
  • 相关阅读:
    牛客练习赛81 B. 小 Q 与彼岸花(DP/Trie/树状数组/FWT/好题)
    HDU6570 Wave(DP)
    HDU6567 Cotree(树的重心/树形DP)
    P1712 [NOI2016] 区间(双指针/线段树)
    P1063 [NOIP2006 提高组] 能量项链(区间DP)
    Jquery
    Jquery
    Jquery
    Jquery 学习
    JS学习
  • 原文地址:https://www.cnblogs.com/pw/p/460879.html
Copyright © 2011-2022 走看看