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
-
-
- using system.IO;
- using MySql.Data.MysqlClient;
- using Microsoft.Win32
-
-
-
-
- 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);";
-
-
- private void Browse(object sender, RoutedEventArgs e)
- {
-
- OpenFileDialog opendlg = new OpenFileDialog();
- opendlg.Filter = "PDF Files |*.pdf||*.pdf";
- if (opendlg.ShowDialog() == true) {
- labelURL.Content = opendlg.FileName;
- }
- }
-
- private void Update(object sender, RoutedEventArgs e) {
- try {
-
- if (labelURL.Content.ToString() == "URL")
- {
- MessageBox.Show("Browse Your Selected PDF Files for Updating..", "", MessageBoxButton.OK, MessageBoxImage.Warning);
- } else {
-
- string Query = "update pdffiles set pdfsize=@pdfsize where pdfname='" + txtFileName.Text + "' ";
- string URLFileName = labelURL.Content.ToString();
- byte[] GetPDFFileSize;
- FileStream stream = new FileStream(URLFileName, FileMode.Open, FileAccess.ReadWrite);
- BinaryReader breader = new BinaryReader(stream);
- GetPDFFileSize = new byte[stream.Length];
- GetPDFFileSize = breader.ReadBytes((int) stream.Length);
-
- stream.Close();
-
- MySqlConnection conn = new MySqlConnection(la.LocalHost);
- MySqlCommand cmd = new MySqlCommand(Query, conn);
- conn.Open();
- var param2 = new MySqlParameter(@ "pdfsize", MySqlDbType.LongBlob, GetPDFFileSize.Length);
- param2.Value = GetPDFFileSize;
- cmd.Parameters.Add(param2);
- int InsertFiles = cmd.ExecuteNonQuery();
- if (InsertFiles > 0) {
-
- MessageBox.Show("Information Updated Successfully!", "", MessageBoxButton.OK, MessageBoxImage.Information);
- labelURL.Content = "URL";
- }
- conn.Close();
- }
- } catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- }
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.