zoukankan      html  css  js  c++  java
  • C#通过SQL读写文件


    int pdfMax = 0;
    try
    {
    pdfMax = Convert.ToInt32(ConfigurationManager.AppSettings["PDFMax"]);
    }
    catch
    {
    pdfMax = 5;
    }
    this.openFileDialog1.Filter = "PDF檔案|*.PDF";
    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    this.StrFile = this.openFileDialog1.FileName;
    this.txtFile.Text = this.StrFile.Substring(this.StrFile.LastIndexOf('\') + 1);
    FileInfo fi = new FileInfo(this.StrFile);
    FileStream fs=fi.OpenRead();
    long fileLen = fs.Length;
    if (fileLen > pdfMax * 1024 * 1024)
    {
    MessageBox.Show("上傳文件限制最大" + pdfMax + "MB");
    return;
    }
    byte[] bytes = new byte[fileLen];
    fs.Read(bytes, 0, (int)fileLen);
    DataSet ds = new DataSet();
    string sql = "insert into tPic values(1,@Pic)";
    using (SqlConnection cn = new SqlConnection("Data Source=dev755;Initial Catalog=Test;User ID=sa;Password=liteon1234"))
    {
    using (SqlCommand cm = new SqlCommand(sql))
    {
    if (cn.State != ConnectionState.Open)
    cn.Open();
    cm.Connection = cn;
    SqlParameter par = new SqlParameter("@Pic", SqlDbType.Image);
    par.Value = bytes;
    cm.Parameters.Add(par);
    cm.ExecuteNonQuery();
    cm.CommandText = "Select*From tPic";
    cm.Parameters.Clear();
    using (SqlDataAdapter sa = new SqlDataAdapter(cm))
    sa.Fill(ds);
    }
    }

    if (ds == null) return ;
    if (ds.Tables.Count == 0) return ;
    byte[] outs= (byte[])ds.Tables[0].Rows[0]["Pic"];
    FileStream fs1 = new FileStream(@"D:網絡訪問異常處理3.pdf", FileMode.OpenOrCreate,FileAccess.ReadWrite);
    fs1.Write(outs, 0, outs.Length);
    fs1.Close();
    WebBrowser wb = new WebBrowser();
    wb.Navigate(@"D:網絡訪問異常處理3.pdf", true);
    DirectoryInfo di = new DirectoryInfo(@"D:");
    FileInfo[] fis=di.GetFiles();
    for(int i = 0; i < fis.Length; i++)
    {
    if (fis[i].CreationTime.ToString("yyyy-MM-dd").CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
    File.Delete(fis[i].FullName);
    }

  • 相关阅读:
    实例!使用Idea创建SSM框架的Maven项目
    springboot开发中的领域模型pojo
    JDK源码阅读:Object类阅读笔记
    DevSecOps: JIRA、Confluence工具、JIRA插件-Xray、eazybi、FishEye、Crucible、jenkins
    MySQL监控及优化
    二叉树 红黑树 B树 B+树 理解
    mysql高性能分页语句_如何优化Mysql千万级快速分页
    使用.Net MinIO SDK 踩的坑
    Windows下Minio介绍、安装及使用、密码修改
    使用docker mediawiki,搭建网页wiki
  • 原文地址:https://www.cnblogs.com/Hawk-Hong/p/15439850.html
Copyright © 2011-2022 走看看