Update a MS Word file to SQL Server being in a datagridview from C#

50few1ms  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(105)

I would like to modify a file in the bd SQL Server

I am designing a C# application that stores word files in a SQL Server database but I like to open the files from my datagridview and save changes coming from MS Word.

amrnrhlw

amrnrhlw1#

This is less an answer than it is an extended comment. First, we don't "do it for [you]... if possible reproduce the code etc.". We help you by answering questions about coding that you ask, preferably by you showing us your code and asking questions about that.

But, here's an attempt to answer what you are looking for. I'm assuming this is Windows Forms (there's a winforms tag you can use on your question, by the way). My assumption is based on your reference to DataGridView .

By the way, a SQL Server database doesn't store files. It can store blobs of stuff that would normally be in a file, but not files. In what you seem to be describing, it's likely going to be storing OpenXML data that would normally be in a .docx file.

I'm guessing that you are trying to build a Windows Forms app that has a DataGridView (DGV). Each row of the grid will represent one of your database-resident "files". It will have some metadata (like the "filename") and a control (likely a button) that will act to open the "file" in Word.

To do this, you will need to copy the blob of file data out of your database row and create an actual (temporary) file somewhere, and then start Word, pointing it to your newly created file.

The .NET Framework provides you with two tools you can use at this point.

  1. When you start up another process, you get a reference to the process, and you can set things up so that you can be notified with the process ends. This way, you can tell when your user exits Word. Now, Microsoft tends to do special things with their applications, so this may require some finesse.
  2. There's something in the Framework known as a FileSystemWatcher . You can point it at a file and you will be notified whenever that file is updated or deleted.

What you want to do with this information is really up to you. After this, it is, as @siggemannen said in the comments, just a matter of figuring out what the parts of this are, and then writing code to implement those parts. Remember, if you have issues with your code, come back here, show us your code, and get answers.

Update:

You're begging me to write your software for you. That's not how this site works (and, I work for a living and don't have time to do your work). But, this should help.

  1. Create a table in SQL Server that has one column to store the contents of your Word "document". That column should be capable of storing a Blob ( https://learn.microsoft.com/en-us/sql/relational-databases/blob/binary-large-object-blob-data-sql-server?view=sql-server-ver16 ). Have at least one other column for metadata (a filename, perhaps).
  2. Populate that table with at least two rows (two file).
  3. Write a Windows Forms app with a grid. Select the metadata from your table and show it in some sort of grid or list. The UI should include a button for each row.
  • When someone clicks the rows button:

  • Select the blob data for that row

  • Write it out to a temporary file (use the TEMP environment variable to find a good place - use a name that's not to obvious, but that ends in .docx )

  • Use a FileSystemWatcher to monitor that file for changes

  • Use System.Diagnostics.Process (using the Shell) to start that document up, opening it in Word

  • Using the Process object that you got when you started up your document, and hook up an Exited event handler.

  • Decide what you want to do with the events from the FileSystemWatcher and the Exited event. You could update the data in the database on every change, or simply monitor the file for changes and update it when Word exits. You likely want to delete the temporary file while the Exited event happens.

There you go.

相关问题