dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersJayant Tripathy
Kavery
Mani
Danasegarane.A
Padma
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET WebForms

How to read files from directory and insert into database as bytes?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 45


In this article I am going to explain about read all files from directory and insert into database table as a byte format.



 


Description


In this project I have convert all files as bytes and insert in to sql server table. This technique is useful for you in your project work.

Create table like below

create table fupload(ImgName varchar(250),Img image)

Cde Behind

Here i read all files from the folder / filter files based on the condition and insert into database.


using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
sqlcon.Open();
byte[] cnt = null;
//Source directory to read files
DirectoryInfo di = new DirectoryInfo("D:\\DNS\\samp");
//if you want only jpg files then filter it
FileInfo[] ArrFi = di.GetFiles("*.jpg");
foreach (FileInfo fi in ArrFi)
{
cnt = FileToByteArray(fi.FullName);
SqlCommand sqlcmd = new SqlCommand("insert into fupload(ImgName,Img) values (@Im, @Img)", sqlcon);
sqlcmd.Parameters.Add("@Im", fi.Name);
sqlcmd.Parameters.Add("@Img", cnt);
sqlcmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{

}
finally
{
sqlcon.Close();
}
}
public byte[] FileToByteArray(string fname)
{
byte[] cnt = null;
try
{
System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
long tb = new System.IO.FileInfo(fname).Length;
cnt = br.ReadBytes((Int32)tb);
fs.Close();
fs.Dispose();
br.Close();
}
catch (Exception ex)
{
}
return cnt;
}
}

Source code:

Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this article is help you to know about read files from folder and insert into database table as bytes.

Attachments
  • InsertFile_DB (44059-04630-InsertFile-DB.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to read files from directory and insert into database as bytes?"

    No responses found. Be the first to respond...

    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to Convert bytes to image and save in specified path?
    Previous Resource: How to use Details view control in ASP.NET?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Store files in database  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.