• 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
     {
     }
    }
  • 相关阅读:
    [c++ 11x rvalue reference]
    Exception Cost
    How to set NoStepInto for VS debugging
    STL算法find_if和find
    [转载]The Biggest Changes in C++11 (and Why You Should Care)
    QT信号和槽
    读《构建之法》前三章有感
    复利计算器(3)——数据库
    《构建之法》——第四章
    操作系统命令行解释
  • 原文地址:https://www.cnblogs.com/pyt5208/p/447077.html
Copyright © 2020-2023  润新知