我工作的公司有一个新项目。其中两名员工将访问每个商店,以记录一些信息。这个项目很简单,我们只想将两个员工收集的数据插入到sql数据库中。
不幸的是,这两名员工将只有一台笔记本电脑,这是离线的。因此,我不能使用同步技术来插入数据。
我的解决方案是允许两个员工将数据写入excel表,然后我尝试找到一种方法将该excel表插入到sql数据库中。
我的问题
可以将excel表格插入到sql数据库中吗?
有没有更好的解决方案?
备注
数据库非常非常简单,它只是一个包含三列的表。ID、StoreName和StoreProductsNumber
发布于 2014-07-19 19:06:58
或者,您可以安排这样的查询在服务器上运行并自动同步:
INSERT INTO database..table(ID,StoreName,StoreProductsNumber)
SELECT
1 AS ID,
LTRIM(RTRIM([StoreName])),
LTRIM(RTRIM([StoreProductsNumber])),
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\folder\excelfile.xls','select * from [sheet1$]')发布于 2014-07-19 21:19:58
尝尝这个
public string GetDataTableOfExcel(string file_path)
{
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
string Import_FileName = Server.MapPath(file_path);
//Import_FileName = System.IO.Path.GetDirectoryName(file_path);
string fileExtension = Path.GetExtension(Import_FileName);
if (fileExtension == ".xlsx")
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'";
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "Select * from [Sheet1$]";
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
}
}
}
}现在,您的数据在DataTable中。您可以从datatable的数据创建插入查询。
file_path是excel文件的完整路径和目录名。
发布于 2014-07-19 18:57:07
通过SQL server的导入功能,你可以实现这一点。
http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
https://stackoverflow.com/questions/24839376
复制相似问题