在c#win中将文件保存在sql数据库中。形成 [英] Save file in sql database in c# win. form

查看:97
本文介绍了在c#win中将文件保存在sql数据库中。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#win中保存sql数据库中的文件。形成?它是以字节存储的吗?有人提供代码snipet,因为我通过谷歌搜索,并没有找到一个好的文章。



提前谢谢

How to Save file in sql database in c# win. form? Is it stored in bytes? Can somebody provide a code snipet as i searched through google and was not able to find a good article for this.

Thanks in advance

推荐答案

通常你使用字节数组,我在这里找到并举例: http:// www .akadia.com / services / dotnet_read_write_blob.html [ ^ ]
Generally you work with arrays of bytes, i found and example here: http://www.akadia.com/services/dotnet_read_write_blob.html[^]


http://stackoverflow.com/questions/2259037/upload-download-file-from-sql-server-2005-2008-from-winforms-c-sharp-app [ ^ ]



http://stackoverflow.com/questions/2445326/how-to-store-the-file-in-the -database [ ^ ]



这两个链接将会帮助你,,,访问它们





谢谢..:)
http://stackoverflow.com/questions/2259037/upload-download-file-from-sql-server-2005-2008-from-winforms-c-sharp-app[^]

http://stackoverflow.com/questions/2445326/how-to-store-the-file-in-the-database[^]

These two links will deffinately help you,,,visit them


thanks..:)


是...它存储在Bytes ....这里是示例代码...



Yes...It is stored in Bytes....here is the sample code...

protected void Upload(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}


这篇关于在c#win中将文件保存在sql数据库中。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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