embeddedXLSX fileinto exe C#

我在VS17中编写代码,并且代码使用存储在xlsx文件中的数据库(到目前为止,通过读取文件的path并使用OLE.DB读取它)使用该数据库:

 string DataBase_File = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), String.Format("{0}", db_name)); string constr = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=NO""", DataBase_File); using (OleDbConnection conn = new OleDbConnection(constr)) { conn.Open(); OleDbCommand command = new OleDbCommand(string.Format("Select * from [{0}]", SheetName), conn); OleDbDataReader reader = command.ExecuteReader(); 

当我想要将它编译成.exe文件时,我按照以下方式将文件添加到资源中:

 var fileString = namespace.Properties.Resources.DataBase; 

但是,我得到的结果是fileString是{bytes[28432]}

我怎样才能使它成为一个path或文件,我实际上可以使用它的单元格中的值作为数据库?

谢谢

用线

 var fileString = namespace.Properties.Resources.DataBase; 

你得到的是字节数组( byte[] ),而你试图把这个字节数组作为你的excel文件的path。 不幸的是,这是行不通的。 当指定fileString作为文件名时,你得到该byte[] .ToString() ,因此{bytes[28432]}

唯一实现你的任务是把那个字节写入(临时)文件,从中获取数据,并删除临时文件。 要做到这一点,你可以做到以下几点:

 //temp file path string tempDbPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); //temp db file name string tempDbFile = "tempdb.xlsx"; //path + filename string tempDBLocation = Path.Combine(tempDbPath, tempDbFile); //get byte array from application's resources byte[] resourceBytes = Properties.Resources.DataBase; //write all bytes to specified location File.WriteAllBytes(tempDBLocation, resourceBytes); //connect and select data as usual string constr = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=NO""", tempDBLocation); using (OleDbConnection conn = new OleDbConnection(constr)) { conn.Open(); OleDbCommand command = new OleDbCommand(string.Format("Select * from [{0}]", "Sheet1$"), conn); OleDbDataReader reader = command.ExecuteReader(); } //delete temp file File.Delete(tempDBLocation);