zoukankan      html  css  js  c++  java
  • ASP.NET存取图片到数据库

    private void btnUpload_Click(object sender, System.EventArgs e)
    {
     //得到用户要上传的文件名
     string strFilePathName = loFile.PostedFile.FileName;
     string strFileName = Path.GetFileName(strFilePathName);
     int FileLength = loFile.PostedFile.ContentLength;
     if(FileLength<=0)
      return;
     try
     {//上传文件
      Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
      Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像
      //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
      StreamObject.Read(FileByteArray,0,FileLength);
      //建立SQL Server链接
      string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
      SqlConnection Con = new SqlConnection(strCon);
      String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
      SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
      CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;
      CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = loFile.PostedFile.ContentType; //记录文件类型
      //把其它单表数据记录上传
      CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = tbDescription.Text;
      //记录文件长度,读取时使用
      CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = FileLength;
      Con.Open();
      CmdObj.ExecuteNonQuery();
      Con.Close();
      //跳转页面
      Response.Redirect("ShowAll.aspx");
     }
     catch
     {
     }
    }
  • 相关阅读:
    Javascript在使用import 与export 区别及使用
    【repost】Python正则表达式
    js常见算法
    【repost】 JS变量重复声明以及忽略var 声明的问题及其背后的原理
    【repost】javascript callback
    【repost】js window对象属性和方法相关资料整理
    Donald Knuth
    前端知识体系
    【repost】让你一句话理解闭包(简单易懂)
    【repost】图解Javascript上下文与作用域
  • 原文地址:https://www.cnblogs.com/pyt5208/p/447077.html
Copyright © 2011-2022 走看看