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


            }

        }
  • 相关阅读:
    开始系统的研究区块链技术了
    基于Centos7的比特币源码编译
    WTForms
    flask-session
    抽屉之Tornado实战(5)--点赞与评论树
    零碎知识点
    flask信号
    MetaClass
    flask系列
    flask源码剖析--请求流程
  • 原文地址:https://www.cnblogs.com/pw/p/460879.html
Copyright © 2020-2023  润新知