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


            }

        }
  • 相关阅读:
    kNN之手写数字识别
    KNN 约会网站配对
    mask_rcnn训练自己的数据集
    Unable to determine the device handle for GPU 0000:01:00.0: GPU is lost.问题排坑
    PAT Advanced 1050 String Subtraction (20) [Hash散列]
    PAT Advanced 1048 Find Coins (25) [Hash散列]
    PAT Advanced 1041 Be Unique (20) [Hash散列]
    PAT Basic 1083 是否存在相等的差 (20) [hash映射,map STL]
    PAT Basic 1047 编程团体赛(20) [Hash散列]
    PAT Basic 1043 输出PATest (20分)[Hash散列]
  • 原文地址:https://www.cnblogs.com/pw/p/460879.html
Copyright © 2011-2022 走看看