Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i upload files(any format) into my database(sql server 2005) from my local disc(hard disk) by using c# windows application.

I am using visual studio 2008, c# windows application.

please send me the code to upload a bulk of files from my local hd and save it into sql server using procedures.
Posted

Here is the code to insert binary data into DB :

C#
SqlConnection cn=new SqlConnection("put_conn_str");
String query = @"INSERT INTO ImagesStore (OriginalPath,ImageDarta) Values (@path,@data)";
SqlCommand cmd=new SqlCommand(query,cn);

//read file
byte []barray=System.IO.File.ReadAllBytes(dlg.FileName);

//extract the filename only from the selected path
string filename=System.IO.Path.GetFileName(dlg.FileName);

// create sql parameters

cmd.Parameters.Add("@path",SqlDbType.VarChar,100).Value=filename;
cmd.Parameters.Add("@data",SqlDbType.Image,barray.Length).Value=barray;

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();


Extracted from here :
http://www.daniweb.com/software-development/csharp/threads/276483[^]

Of course "path" is varchar(100) and "data" is image data type.
Also you may use varbinary for storing binary data.


"please send me the code to upload a bulk of files"
If you repeat this operation for multiple files you will insert multiple files into the DB.

Hope it helps.
 
Share this answer
 
Comments
Abhinav S 29-Jan-12 3:02am    
Nice 5.
Amir Mahfoozi 29-Jan-12 3:05am    
Thanks Abhinav.
Bulk copy - see an implementation of the SqlBulkCopy command here[^].
Try this[^] too.
Have a look at this[^] as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900