Articles in this section
Category / Section

How to store and load a `.EDP` file from a database in Windows Forms?

5 mins read

In the WinForms Diagram Control, you can store and load a .EDP file from a database. First, use a SaveFileDialog to select the location for saving the file. Next, serialize the symbol palette using a BinaryFormatter and save the content of the file. Then, insert this content into a SQL database using an INSERT command. To load the file, execute a SELECT command to retrieve the file’s content from the database, write the binary data to a file, and use the LoadPalette method to load the symbol palette from this file. This process allows you to manage symbol palette files directly from the database, ensuring efficient storage and retrieval. Below, we have provided a code example to demonstrate how to achieve this.

Code Snippet

Save File:

           SaveFileDialog savePaletteDialog = new SaveFileDialog();
           savePaletteDialog.Filter = "EDP Files|*.edp|All Files|*.*";
           savePaletteDialog.Title = "Save a EDP File";
           if (savePaletteDialog.ShowDialog(this) == DialogResult.OK)
           {
               SymbolPalette symbolPalette = paletteGroupBar1.CurrentSymbolPalette;
               string strSavePath = savePaletteDialog.FileName;
               if (symbolPalette != null)
               {
                   FileStream fStream = new FileStream(strSavePath, FileMode.OpenOrCreate, FileAccess.Write);
                   BinaryFormatter formatter = new BinaryFormatter();
                   formatter.Serialize(fStream, symbolPalette);
                   fStream.Close();

                   string filePath = strSavePath;
                   string fileContents = File.ReadAllText(filePath);

                   using (SqlConnection connection = new SqlConnection("your connection string")) // Using your proper connection string value to execute.
                   {
                       SqlCommand command = new SqlCommand("INSERT INTO FileTable (FileName, FileContents) VALUES (@FileName, @FileContents)” , connection);

                                                   command.Parameters.AddWithValue("@FileName", Path.GetFileName(filePath));

                       command.Parameters.AddWithValue("@FileContents", fileContents);
                       connection.Open();
                       command.ExecuteNonQuery();
                   }
               }
           }



Load File:

           string fileName = "example.edp"; // Replace with the file name you want to load
           string filePath = "D:\\example.edp"; // Replace with the file path you want to save the file to

           using (SqlConnection connection = new SqlConnection("your connection string")) // Using your proper connection string value to execute.
           {
               SqlCommand command = new SqlCommand("SELECT FileContents FROM FileTable WHERE FileName = @FileName", connection);
               command.Parameters.AddWithValue("@FileName", fileName);

               connection.Open();

               using (SqlDataReader reader = command.ExecuteReader())
               {
                   if (reader.Read())
                   {
                       byte[] fileContents = (byte[])reader["FileContents"];
                       File.WriteAllBytes(filePath, fileContents);
                   }
               }
           }

           this.paletteGroupBar1.LoadPalette("D:\\example.edp"); // Use the exact location of the retrieved file location to load the palette. 
           

Conclusion:

I hope you enjoyed learning about How to store and load a .EDP file from a database in Windows Forms

You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations. You can also explore our WinForms Diagram documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied