Update Blob PDF File From MySQL In C# Using WPF

First of all, I will not go from step to step from the beginning but will show only the code that will execute the functions for the update.
 
To begin, create a WPF application.
 
Add two buttons and name them Update and Browse for GUI labels to display and name btnUpdate and btnBrowse for the name for using in coding.
 
Add a Text Box and name it File Name for GUI and txtFileName for coding name. Add a label and name URL for GUI label display and labelURL for the coding name.
 
Button Update for executing update function.
 
Button Browse for searching the PDF or other files format.
 
Text Box for Inserting the name of the file browse into the MySQL database.
 
Label for displaying the URL location of the file for converting into binary.
 
Before starting your coding, you have to install MySQL .Net Connector in order to import or using the methods, properties and fields of the MySQL Codes in the program.
 
Nevertheless, make sure to add reference of the  MySql.Data.Client DLL files to your project
  1. //using this class library    
  2.   
  3. using system.IO;  
  4. using MySql.Data.MysqlClient;  
  5. using Microsoft.Win32  
  6. // using Microsoft.Win32 when coding using WPF    
  7.   
  8. //Code for database connection string    
  9.   
  10. protected string ConnectionString = "Datasource=(Your Host IP); Database=(Your Database Name/Scheme Name); Username=(Your Database Username. eg(root)); Password=(Your Database Password); Port=(If You used the port other then 3306);";  
  11. //Code for Browse    
  12.   
  13. private void Browse(object sender, RoutedEventArgs e) //For WPF and EventArgs e for Windows Form    
  14. {  
  15.   
  16.  OpenFileDialog opendlg = new OpenFileDialog();  
  17.  opendlg.Filter = "PDF Files |*.pdf||*.pdf";  
  18.  if (opendlg.ShowDialog() == true) {  
  19.   labelURL.Content = opendlg.FileName;  
  20.  }  
  21. }  
  22. //Code for Update    
  23. private void Update(object sender, RoutedEventArgs e) {  
  24.  try {  
  25.   //Checking if the label is empty    
  26.   if (labelURL.Content.ToString() == "URL"//For WPF and labelURL.Text for Windows Form    
  27.   {  
  28.    MessageBox.Show("Browse Your Selected PDF Files for Updating..""", MessageBoxButton.OK, MessageBoxImage.Warning);  
  29.   } else {  
  30.    //Streaming browse file and convert it into bytes    
  31.    string Query = "update pdffiles set pdfsize=@pdfsize where pdfname='" + txtFileName.Text + "' ";  
  32.    string URLFileName = labelURL.Content.ToString();  
  33.    byte[] GetPDFFileSize;  
  34.    FileStream stream = new FileStream(URLFileName, FileMode.Open, FileAccess.ReadWrite);  
  35.    BinaryReader breader = new BinaryReader(stream);  
  36.    GetPDFFileSize = new byte[stream.Length];  
  37.    GetPDFFileSize = breader.ReadBytes((int) stream.Length);  
  38.   
  39.    stream.Close();  
  40.    //Mysql Update Codes    
  41.    MySqlConnection conn = new MySqlConnection(la.LocalHost);  
  42.    MySqlCommand cmd = new MySqlCommand(Query, conn);  
  43.    conn.Open();  
  44.    var param2 = new MySqlParameter(@ "pdfsize", MySqlDbType.LongBlob, GetPDFFileSize.Length);  
  45.    param2.Value = GetPDFFileSize;  
  46.    cmd.Parameters.Add(param2);  
  47.    int InsertFiles = cmd.ExecuteNonQuery();  
  48.    if (InsertFiles > 0) {  
  49.     //Proceed ..     
  50.     MessageBox.Show("Information Updated Successfully!""", MessageBoxButton.OK, MessageBoxImage.Information);  
  51.     labelURL.Content = "URL";  
  52.    }  
  53.    conn.Close();  
  54.   }  
  55.  } catch (Exception ex) {  
  56.   MessageBox.Show(ex.Message);  
  57.  }  
  58. }  
Note
  • pdffiles is the table name in database
  • pdfsize is the file browse for update in LONGBLOB data type in the database
  • pdfname is the name given to the file update.