从sql服务器保存和检索文件 [英] save and retrive files from sql server

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

问题描述

// Read the file and convert it to Byte Array
        string filePath = Server.MapPath("APP_DATA/TestDoc.doc");
        string filename = Path.GetFileName(filePath);

        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        br.Close();
        fs.Close();

        //insert the file into database
        string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-word";
        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
        InsertUpdateData(cmd);

protected void Retreive_Doc(object sender, EventArgs e)
    {
        string strQuery = "select Name, ContentType, Data from tblFiles where id=@id";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;
        DataTable dt = GetData(cmd);
        if (dt != null)
        {
            download(dt);
        }
    }


private void download (DataTable dt)
    {
        //String[] strings=( String[])dt.Rows[0]["Data"];
        Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
        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]["Name"].ToString());
        Response.BinaryWrite(bytes);
        Response.Flush(); 
        Response.End();
    }




我收到此错误

无法将类型为"System.String"的对象转换为类型为"System.Byte []".
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.InvalidCastException:无法将类型为"System.String"的对象强制转换为类型为"System.Byte []".

它将数据插入到数据库中并且不检索它




i get this error

Unable to cast object of type ''System.String'' to type ''System.Byte[]''.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type ''System.String'' to type ''System.Byte[]''.

it insert data to database and dosen''t retrive it

推荐答案

查找错误的行并尝试实现此操作:

http://sharpertutorials.com/string-to-byte-and-byte-to-string / [ ^ ]

http://www.authorcode.com /how-to-convert-a-string-into-byte-using-c-and-vb-net/ [
Find Line on which u get eror and try to implement this:

http://sharpertutorials.com/string-to-byte-and-byte-to-string/[^]

http://www.authorcode.com/how-to-convert-a-string-into-byte-using-c-and-vb-net/[^]


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

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