有關C#上傳文件到MSSQL數據存儲問題各位,用C#代碼怎樣寫把用戶端指定的文件(C/S結構)保存到數據庫Binary字段中,反過來,又怎樣從數據庫下載到用戶端,形成對應的文件。
注:如果文件較大,程式要告訴用戶上傳或下載文件的進度。

解决方案 »

  1.   

    上传StringBuilder strbSqlFile = new StringBuilder();
    if (FileUpload1.HasFile)
                    {
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = db.conn;
                        cmd.CommandTimeout = 300;                    strbSqlFile.Append("INSERT INTO TableName VALUES (");                    HttpPostedFile upFile = FileUpload1.PostedFile;
                        int upFileLength = upFile.ContentLength;
                        byte[] FileArray = new Byte[upFileLength];
                        Stream UpFileStream = upFile.InputStream;
                        UpFileStream.Read(FileArray, 0, upFileLength);
                        strbSqlFile.Append("    File=@File) ");
                        cmd.Parameters.Add("@File", SqlDbType.VarBinary);
                        cmd.Parameters["@File"].Value = FileArray;                    strbSqlFile.Append("WHERE PTNO='" + strPTNO + "' AND Seq='" + (maxSeq + 1) 
                        cmd.CommandText = strbSqlFile.ToString();
                        cmd.ExecuteNonQuery();
                    }
    下载获取的数据放在dataset中
    byte[] byteData = (byte[])ds.Tables[0].Rows[0][0];        Response.Clear();
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift-JIS");
            Response.ContentType = "application/msword; charset=Shift-JIS";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlPathEncode(strFileName) );
            Response.AddHeader("Content-Length ", byteData.Length.ToString());
            Response.Flush();
            Response.BinaryWrite(byteData);        Response.End();
        }
      

  2.   

    很简单的,参考:
    http://topic.csdn.net/t/20051228/15/4486561.html
      

  3.   

    您提示的這種方法是可以上傳和下載文件數據庫,但我怎樣告訴用戶已經完成了多少呢?
    (注:我是用C#寫C/S結構的程式)
      

  4.   

    http://www.cnblogs.com/gudieaofei/articles/1265372.html
      

  5.   

    誰能告訴我WinFrom中怎樣獲取客戶端保存文件到MS SQL數據庫中的方法,並能告知用戶保存的進度......
      

  6.   

    我是用C# Winform寫上傳文件到數據庫中,謝謝指點