Opening attachment stored in MS Access using C# - c#

I have a column in my table stored in MS Access as a Attachment data type. It stores various files such as docx, pdf's etc.
I am trying to display a file from the table using a documentViewer (obtained from XtremeDocumentStudio .NET).
I also have a combo box on my form with a list of employee ID's. When a particular ID is selected from the combo box, I want the associated attachment with that employee to be displayed in the documentViewer.
I am using this query:
SELECT EmployeeAttachment FROM Employee WHERE EmployeeID = 2
I have been stuck on this problem for a while and am not sure on how to implement it. Any help or ideas on how I would do this would be greatly appreciated.

In order to get a faithful copy of the file from the Attachment field in the Access database you need to use the .SaveToFile method of an ACE DAO Field2 object. For details, see the related question:
Extracting files from an Attachment field in an Access database
Once you have extracted the file to disk (e.g., to System.IO.Path.GetTempPath) then you can tell the viewer control where to find it.

Related

Write hidden information (like software name & version) in Excel file using OpenXML

My c# software exports to Excel files using OpenXML. I am reading and editing these files in the software even after user edits them in Excel. I want to add the software name and version somewhere, so, when the software opens the file, recognize it. Is there any property in the Workbook as document information, comments, or only visible to OpenXML not Excel?
For now the best workaround is to save this information in an excel sheet and hide the sheet. The point is this information should not be accidentally deleted by the user.
I'd use a Custom XML Part.
The content of a Custom XML Part is only accessible through the object model or by unzipping the file "package". So the information can't be altered accidentally or even viewed without extra effort. And if you're processing the file via Open XML, accessing a Custom XML Part is reasonably straight-forward.
(The user can see Document Properties (suggested in a comment) via the UI, in the File/Info tab.)
Set a custom property for excel document via Openxml
Find method SetCustomProperty() at the bottom of page. This function is written for Word document so change open-file line to below one for Excel document
using (var document = SpreadsheetDocument.Open(fileName, true))
And you are good to add any property to your file.
How to Hide properties
The properties will be visible in Excel through File-> Info -> Properties -> Advanced Properties window. Users will be able to delete them there. If this is not desired, a property won't be visible in excel if instead of this unique id
newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";
another one is used:
newProp.FormatId = Guid.NewGuid().ToString("B");
Note: for saving string use VTLPWSTR type. Do not use type VTBString along with the unique ID given above as Excel automatically deletes your property when you edit it in there (just by experience, I don't know why!).
How to read the properties?
You saved your file. Then, open it again and loop over all properties
foreach (CustomDocumentProperty property in document.CustomFilePropertiesPart.Properties)
{
if (property.Name.Value == nameof(Product) &&
property.VTBString.Text == Product)
return true;
}
where Product is string property holds the name of software, and VTBString is used to save value of Product. As many properties as desired can be saved and read with this method.

Reading data from excel and send to SQL Server using Asp.Net MVC 5 C# with LINQ if its possible

Sorry with my English
I have an input file where the user will upload an Excel File, 1st I need to scan the extension of the file (.xlsx or .xls), after reading data from it and save the data in SQL Server.
Abour scan a extension I have this:
var ext = Path.GetExtension(file.FileName);
var allowedExtensions = new[] { ".xlsx", ".xls" };
if(allowedExtensions.Contains(ext)){ //codigo }
Now, my biggest question is to read the file data and send it to SQL Server.
The table has these columns:
ID
Registro
Nome
Ativo
I'm a newbie in the area, if that's simple, sorry :)
so you can read this thread, here is solidly explained how you can read data from excel : http://csharp.net-informations.com/excel/csharp-read-excel.htm
and about storing this data in DB, you can crate model class with the properties you already showed to us and then fill this properties with the data from excel and than insert them in db.
I recommend to use linktoexcel package method for that process. Its is easy and little code to get data from excel file. Please check this link and try it.
Get all the values from excel file by using linqtoexcel
Good luck
You can also use "Aspose.Cells" from nuget https://www.nuget.org/packages/Aspose.Cells/
to extract the cell information and fill your own objects with values if your intention is not to render the .xls/.xlsx but to extract the information then add to the DB.

Saving each user's file along with corresponding database / table

CONTEXT: I am writing an application in Visual C# / .Net, where the user can:
Create a new document-file "Untitled.abc"
Add some data/content, e.g., images, text, etc.
Save the document-file as "MyDoc7.abc", where abc would be my app-specific extension.
QUESTION: Is the following method good to implement the above, or is there a simpler/cleaner approach?
I start with a SQL-Server database called MyDb.
Each time a user creates a new document-file, I programatically add a new DB table to MyDb
The newly created DB table stores the data/content from just that particular corresponding document-file created by the user.
When user re-opens the saved document-file in the future, I programatically read the data/content from the attached corresponding DB table.
UPDATE:
Solved. I will be using this Alternative approach:
I start with a SQL-Server database called MyDb with a table called MyTable, including a column called FileId.
Each time a user creates a new document-file, I assign the file a unique id, e.g., FileId = 5.
When the user adds a piece of data/content to the file, I store the piece of data to MyTable, but also store FileId = 5 for that piece of data.
In the future, when the user re-opens the file, I fetch all pieces of data from MyTable where FileId = 5.
That sounds like a particularly bad idea to do that.
You don't want to create tables on the fly
If all the data is in the database, what is the file for?
I would rather choose the approach that Office uses:
The file really is a zip archive which contains:
A folder for the assets (images and the like)
The actual file with the content and relative links to the assets.

How to read a text file into Queryable list of objects using c#?

I have a fax log file that which logs all fax jobs.
I need to read this file and construct a Queryable list of objects. The object attributes must be the same as headings in the log file, for example:
"JobID" "ParentJobID" "SubmissionTime" "Scheduled" "Status" "ErrorDesc" "ErrorCode" "StartTime" "EndTime" "Device" "DialedNumber" "CSID" "TSID"
and so on. In total there are about 50 different columns. It is tab delimited and values are stored inside the quotes. I want to be able to query this file depending on user selected options.
Can someone suggest me a way of doing this?
Thanks a lot!
Take a look at this article:
Link

URL to network file in SharePoint

I have a column in a SharePoint list that I wish to make a link to a file on the network. The file location is generated in code so I need to write a CAML script to update the column.
Could someone please give me an example of what the value stored in the database would be? In this example the file location is \server\folder\file.txt. I would like the textual name to be the same if possible.
For your link you should use a column of the type "Hyperlink or Picture". This column type can handle a link and a description for it. To set these both values for a this field you would use the following code.
SPFieldUrlValue urlField = new SPFieldUrlValue();
urlField.Description = #"\\server\folder\file.txt";
urlField.Url = #"\\server\folder\file.txt";
yourListItem["yourLinkColumnName"] = urlField;
yourListItem.Update();
SharePoint will automatically convert the link from "\server\folder\file.txt" to "file://server/folder/file.txt". But be aware that SharePoint won't handle the permissions a user needs to access the file. It's just a link.

Categories