Need a database to store Word documents, tell me what format the field is needed and how to work with it.
- oneAre you sure that you want to store files in the database? Other organization data is not considering? - ixSci
- It's unclear what kind of DBMS you have. SQL Server? which version? answers depend on it. - nzeemin
- oneblob or clob if you understand the essence of the question. - NMD
|
2 answers
class DocxEntity { public int Id { get; set; } public byte[] DocxFile { get; set; } } class MyContext : DbContext { public DbSet<DocxEntity> DocxFiles { get; set; } } class Program { static void Main(string[] args) { PutFileToDatabase("test.docx"); GetFileFromDatabase(1, "out.docx"); } static void PutFileToDatabase(string path) { using (var context = new MyContext()) { FileStream fs = new FileStream(path, FileMode.Open); DocxEntity e = new DocxEntity(); e.DocxFile = new byte[fs.Length]; fs.Read(e.DocxFile, 0, (int)fs.Length); fs.Close(); context.DocxFiles.Add(e); context.SaveChanges(); } } static void GetFileFromDatabase(int fileId, string path) { using (var context = new MyContext()) { DocxEntity e = context.DocxFiles.FirstOrDefault(f => f.Id == fileId); if (e != null) { FileStream fs = new FileStream(path, FileMode.CreateNew); fs.Write(e.DocxFile, 0, e.DocxFile.Length); fs.Close(); } } } } |
Use the byte[] type for this, which is automatically mapped to varbinary(max) in the database.
It also makes sense to include FILESTREAM for this column, but for this you have to "poke around" in the database directly.
|