从sql server检索文件 [英] retrieve files from sql server

查看:111
本文介绍了从sql server检索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点点障碍



我将文档(doc,xls,pdf,.....)保存到sql server数据库中。我有sql中的表,列ID,文档,documentName



这一切都正常,但我需要先将文档检索回listbox或listview或其他东西,(我不知道)也许dgv,然后通过选择该列表中的一些文件将其保存在硬盘上当然,但首先要做的是第一件事



I have a little bit of a snag

I am saving documents (doc,xls, pdf,.....) into sql server database. I have table in sql, with columns Id, document, documentName

and it all works fine, but I need to retrieve documents back first to listbox or listview or something else,(i don't know) maybe dgv, and then by choosing some file from that list to save it on harddisk of course, but first thing first

cs.Open();

SqlCommand cmdDocs = new SqlCommand("SELECT Document, documentName FROM Documents WHERE userID = @userID", cs);
SqlDataAdapter daDocs = new SqlDataAdapter(cmdDocs);
cmdDocs.Parameters.Add("@userID", SqlDbType.NVarChar).Value = DataDetails.id.Text;

DataTable dtDocs = new DataTable();
DataSet dsDocs = new DataSet();

daDocs.Fill(dtDocs);
daDocs.Fill(dsDocs);
//DataSet dsDocs = new DataSet();

object oDocs = cmdDocs.ExecuteScalar();

if (oDocs != DBNull.Value)
{

   Byte[] dataDocs = new Byte[0];

   foreach (DataRow dir in dtDocs.Rows)
   {
      dataDocs = (Byte[])(dsDocs.Tables[0].Rows[0]["Document"]);
      string NameOfImage = (String)dsDocs.Tables[0].Rows[0]["documentName"];

      MemoryStream mem = new MemoryStream(dataDocs);

      //here I have to put something to fill listview or listbox or dgv but I dont know how or what, to get those documents
                        
   }
}
else
{

}

cs.Close();

推荐答案

请参考此代码/ .......



Please Refer this code/.......

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ImageID"] != null)
        {
            string strQuery = "select ID, DocumentName, Document from" +
                " tblFiles where ID=@id";
            String strConnString = System.Configuration.ConfigurationManager
                .ConnectionStrings["conString"].ConnectionString;
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@id", SqlDbType.Int).Value
                = Convert.ToInt32(Request.QueryString["FileID"]);
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
            if (dt != null)
            {
                Byte[] bytes = (Byte[])dt.Rows[0]["Document"];
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = dt.Rows[0]["ContentType"].ToString();
                Response.AddHeader("content-disposition", "attachment;filename="
                    + dt.Rows[0]["DocumentName"].ToString());
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
        }
    }


这篇关于从sql server检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆