By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Store multiple files(pdf,.dwg) in database

P: 2
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
Share this Question
Share on Google+
1 Reply


P: 2
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

Post your reply

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