使用C#将文件上传到数据库 [英] Uploading files to database in C#

查看:269
本文介绍了使用C#将文件上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net中建立一个网站.我已经进行了文件上传控制,可以上传doc,txt文件.现在,我想将该上载的文件保存在sql server数据库表中.怎么做??
我正在用C#编写代码.
请帮助!
谢谢u

I am making a website in asp.net.I''ve made a file upload control to upload doc,txt files. Now I want to save that uploaded file in sql server database table. How to do it???
I am writing the code in C#.
Plz help !
Thank u

推荐答案

在数据库中创建一个Image字段-由于其他数据类型的大小限制太小,因此必须是image字段. 同时,为文件名创建一个nvarchar(Max)字段.
处理上载控件的按钮单击事件.
在处理程序中,上载控件具有您感兴趣的两个属性:FileName(仅是名称,没有可用的路径信息)和FileBytes,其中包含实际的文件内容.
此方法的作用超出了您的需要,但可以为您带来想法:

Create an Image field in the database - it needs to be image as the size limits are too small on the other datatypes.
At the same time, create a nvarchar(Max) field for the filename.
Handle the button click event of the Upload control.
In the handler, the upload control has two properties you are interested in: FileName (which is just the name, no path information is ever available) and FileBytes which contains the actual file content.
This method does more than you need, but it gives you the idea:

/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }


这篇关于使用C#将文件上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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