Goran Bibic

Goran Bibic

  • 480
  • 2.9k
  • 200.8k

Insert PDF in database c#

Aug 6 2018 1:40 AM
Is this good way?
 
  1. private void Button2_Click(object sender, EventArgs e)   //Save pdf in database  
  2.         {  
  3.             byte[] filedata = null;  
  4.             MemoryStream ms = new MemoryStream();  
  5.             filedata = ms.GetBuffer();  
  6.             axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());  
  7.   
  8.   
  9.   
  10.                     using (SqlConnection openCon = new SqlConnection(cs))  
  11.                     {  
  12.   
  13.                         string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @maxNo=@maxNo+1; INSERT into dbo.documents (number,file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";  
  14.   
  15.                         using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
  16.                         {  
  17.                             querySaveStaff.Connection = openCon;  
  18.   
  19.                             querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;  
  20.   
  21.                             querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;  
  22.   
  23.                             openCon.Open();  
  24.                             querySaveStaff.ExecuteNonQuery();  
  25.                             openCon.Close();                              
  26.   
  27.   
  28.                         }  
  29.   
  30.                     }  
  31.   

 

Answers (10)