如何通过asp.net中的fileupload控件以varbinary格式插入图像 [英] how to insert image in varbinary format through fileupload control in asp.net

查看:49
本文介绍了如何通过asp.net中的fileupload控件以varbinary格式插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过asp.net中的 fileupload 控件以 varbinary 格式插入 image

解决方案
轻松!这是我使用的代码(比你可能需要的更多):

  ///   <  摘要 >  
/// 将上传保存到数据库中。
/// < / summary >
/// < param < span class =code-summarycomment> name =fl > 包含控件下载。< / param >
/// < 返回 > 状态为字符串< / returns >
private string SaveUpload(FileUpload fl)
{
if (fl。 HasFile)
{
尝试
{
int version = 0 ;
string filename = Path.GetFileName(fl.FileName);
byte [] filedata = fl.FileBytes;
string strCon = ConnectionStrings.Download;
使用(SqlConnection con = new SqlConnection(strCon))
{
con.Open();
// 检查版本 - 如果文件中已有文件,则增加版本号。
使用(SqlCommand ver = new SqlCommand( SELECT MAX(版本)FROM dlContent WHERE fileName = @ FN,con))
{
ver.Parameters。 AddWithValue( @ FN,filename);
object o = ver.ExecuteScalar();
if (o!= null && o!= System.DBNull.Value )
{
// 已存在。
version =( int )o + 1 ;
}
}
// 将文件粘贴到数据库中。
使用(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,版本);
ins.Parameters.AddWithValue( @ UD,DateTime.Now);
ins.ExecuteNonQuery();
}
}
return string .Format( {0}已上传,版本= {1},文件名,版本);
}
catch (例外情况)
{
return 无法上传文件。出现以下错误: + ex.Message;
}
}
return 请选择一个文件。;
}


how to insert image in varbinary format through fileupload control in asp.net

解决方案

Easy! This is teh code I use (which does a bit more than you probably need):

/// <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.";
    }


这篇关于如何通过asp.net中的fileupload控件以varbinary格式插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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