473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Store multiple files(pdf,.dwg) in database

Hi,
I need to insert multiple files from front-end in the same datacolumn in DB using c#.
& also need to retrieve from DB to UI.
Please help me out.

My code is as follows...
Expand|Select|Wrap|Line Numbers
  1. string filePath = fDialog.FileName;
  2.             string[] fileNames = fDialog.FileNames;
  3.             string filename = System.IO.Path.GetFileName(filePath);
  4.             string ext = Path.GetExtension(filename);
  5.             byte[] FilesData=null;
  6.             List<byte> FileData = new List<byte>();
  7.             List<string> AllFiles = new List<string>();
  8.  
  9.             for (int i = 0; i < fileNames.Length; i++)
  10.             {
  11.                 AllFiles.Add(fileNames[i]);
  12.             }
  13.             foreach (string str in AllFiles)
  14.             {
  15.                 FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read);
  16.                 BinaryReader br = new BinaryReader(fs);
  17.                 FilesData = br.ReadBytes((int)fs.Length);
  18.  
  19.  
  20.                 br.Close();
  21.                 fs.Close();
  22.             }
  23.  
  24.  
  25.                 try
  26.                 {
  27.                     connection.Open();
  28.                     string query = "insert into TableName(IDVERSION,FILENAME,FILE) values(@Idversion,@FileName,@File);commit;";
  29.                     command = new DB2Command(query, connection);
  30.                     command.Parameters.Add("@Idversion", DB2Type.Integer).Value = 0012013;
  31.                     command.Parameters.Add("@FileName", DB2Type.VarChar).Value = AllFiles.ToString();
  32.                     command.Parameters.Add("@File", DB2Type.Binary).Value = FilesData;
  33.                     command.ExecuteNonQuery();
  34.                 }
  35.                 catch (Exception)
  36.                 { }
  37.                 finally
  38.                 {
  39.                     connection.Close();
  40.                 }
Mar 13 '13 #1
1 2188
Expand|Select|Wrap|Line Numbers
  1.  private void btnUpload_Click(object sender, EventArgs e)
  2.         {
  3.             string filePath = fDialog.FileName;
  4.             string[] fileNames = fDialog.FileNames;
  5.             string filename = System.IO.Path.GetFileName(filePath);
  6.             string ext = Path.GetExtension(filename);
  7.             byte[] FilesData=null;
  8.             int FileSize = 0;
  9.             int size=0;
  10.  
  11.             List<string> AllFiles = new List<string>();
  12.  
  13.             for (int i = 0; i < fileNames.Length; i++)
  14.             {
  15.                 if (i == fileNames.Length)
  16.                 {
  17.                     strin.Append(filename[i]);
  18.  
  19.                 }
  20.                 else
  21.                 {
  22.                     strin.Append(filename[i]);
  23.                     strin.Append(",");
  24.                 }
  25.                 //AllFiles.Add(fileNames[i]);
  26.  
  27.                 //foreach (string str in AllFiles)
  28.                 //{
  29.                 FileStream fs = new FileStream(fileNames[i], FileMode.Open, FileAccess.Read);
  30.                 BinaryReader br = new BinaryReader(fs);
  31.  
  32.                 //FilesData = br.ReadBytes((int)fs.Length);
  33.                 byte[] fileContent = br.ReadBytes((int)fs.Length);
  34.                 FileSize = fileContent.Length;
  35.                 size = size + FileSize;
  36.                 br.Close();
  37.                 fs.Close();
  38.                 //}
  39.             }
  40.  
  41.             byte[] sizebyte =BitConverter.GetBytes(size);
  42.  
  43.  
  44.  
  45.                 try
  46.                 {
  47.                     connection.Open();
  48.                     string query = "insert into tblFiles(IDVERSION,FILENAME,FILE) values(@Idversion,@FileName,@File);";
  49.                     command = new DB2Command(query, connection);
  50.                     command.Parameters.Add("@Idversion", DB2Type.Integer).Value =147822 ;
  51.                     command.Parameters.Add("@FileName", DB2Type.VarChar).Value = strin.ToString();
  52.                     command.Parameters.Add("@File", DB2Type.Binary).Value = sizebyte;
  53.                     command.ExecuteNonQuery();
  54.                 }
  55.                 catch (Exception)
  56.                 { }
  57.                 finally
  58.                 {
  59.                     connection.Close();
  60.                 }       
  61.  
  62.  
  63.  
  64.         }
  65.  
  66.  
Mar 15 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bola | last post by:
Is anyone know how I can store Word Docs and PDF's in database, and how I can make link for visitor to download these files
0
by: Roshan James | last post by:
Hi, I'm creating an application which requires to store multiple file attachments in SQL Server Database from a windows form. I am able to store the files and retreive the files from the...
1
by: grzybek | last post by:
Hi, I have question about techniques of using files in SQL Server in Web Application. Assuming that I send files from my Web App ( client ) to server and located these files on hard disk ( on...
2
by: Nicolas | last post by:
How to store multiple files in only one sql blod field in sql database Like I got a record for a house: ID Name addresss price attachments The field "attachments" may contains documents...
1
by: akashkhasgiwala | last post by:
i am developing i web application in which every user will have an Xml File containing mostly text and date/time stamps this application will use MySql for storing data so pl tell me if i can STORE...
4
by: lyealain | last post by:
how to store many files in one database fields?? i manage to store one file into database field.. but fail to store many files..... how should i do that>> one database field for many files name.....
2
by: poopsy | last post by:
hello all, i am developing a web application for my final year project where i will be able to download and upload files(more specifically, it will be papers and articles, mostly pdf files) from the...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
2
by: crazykreize | last post by:
I want to know how to store text files format (.txt) in the SQL Server 2005.
0
by: Deepa Avudiappa | last post by:
We have designed a basic text editor using GTK+ in Ubuntu. We are supposed to store text files within a database(postgreSQL) which can be retrieved, viewed on our editor, modified using the options...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.