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

  • 相关阅读:
    Matlab 图像处理中出现纯黑或纯白是怎么回事?
    网页中的公式在翻译时如何被保留下来?
    请勿 fAKe 评论
    C++ STL 使用注意事项整理
    APIO 2020 题解
    谈谈对题解本质的理解
    点双 & 边双连通图计数的细节比较
    联合省选 2021 A 卷 题解
    指数生成函数(EGF)计数小记
    UOJ-37 清华集训2014 主旋律
  • 原文地址:https://www.cnblogs.com/Hawk-Hong/p/15439850.html
Copyright © 2011-2022 走看看