如何在数据库中存储文件? [英] how to store files in database?

查看:881
本文介绍了如何在数据库中存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据访问中的代码



  public   class  DataAccess 
{
FilesEntity files = new FilesEntity();
public void SaveFileName(FilesEntity filentity)
{
files = filentity;
string cs = ConfigurationManager.ConnectionStrings [ DBCS]的ConnectionString。
使用(SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand( spnotaryfiles,con);
cmd.Parameters.AddWithValue( @ Filename,files.filename);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
}
}
}







控制器代码< br $>






 [HttpPost] 
public ActionResult索引(HttpPostedFileBase文件)
{
if (file!= null && file.ContentLength > 0
{
尝试
{
var fileName = Path .GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath( 〜/ Images /),fileName);
file.SaveAs(path);

// da.SaveFileName(entity);
}
catch (例外情况)
{
throw ex;
}

}

return View();
}









视图中的COde



 @ using(Html.BeginForm(Index,File,FormMethod.Post,new {enctype =multipart / form-data }))
{

< input type = 文件 名称 = file / < span class =code-keyword>> < br / >
< 输入 type = 提交 名称 = 提交 id = 提交 value = 上传 / >

}









我在这里能够将文件存储在我给出的文件夹中,但如何将文件名存储在数据库中。

需要帮助

提前感谢

解决方

这个存储过程是什么:spnotaryfiles



看起来像在SQL数据库中?,你的代码调用它并给它参数..



如果你在其他地方删掉了代码,你将不得不在数据库中重新创建存储过程来完成你想要的工作...



https://msdn.microsoft.com/en-gb/ library / ms345415.aspx [ ^

Code in Data Access

public class DataAccess
    {
        FilesEntity files = new FilesEntity();
        public void SaveFileName(FilesEntity filentity)
        {
            files = filentity;
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spnotaryfiles", con);
                cmd.Parameters.AddWithValue("@Filename", files.filename);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }




Code in Controller



[HttpPost]
   public ActionResult Index(HttpPostedFileBase file)
   {
       if(file != null && file.ContentLength > 0)
       {
           try
           {
               var fileName = Path.GetFileName(file.FileName);
               var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
               file.SaveAs(path);

               //da.SaveFileName(entity);
           }
           catch (Exception ex)
           {
               throw ex;
           }

       }

       return View();
   }





COde in Views

@using (Html.BeginForm("Index", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="file" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
  
}





Here I am able to store the files in the folder i have given but how to store the file name in database.
Need Help
thanks in advance

解决方案

what does this stored procedure : spnotaryfiles

look like in the SQL database?, your code is calling that and giving it parameters..

if you've ripped the code off elsewhere you will have to recreate the stored procedure in your database to do the job you want...

https://msdn.microsoft.com/en-gb/library/ms345415.aspx[^]


这篇关于如何在数据库中存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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