C# 某些Excel文件未从共享路径移动到SQL Server
我们有一个应用程序,其中Excel文件中的数据(存在于共享路径中)移动到数据库。如果发生任何错误,文件将通过将错误写入日志文件移动到错误文件夹。它使用windows服务执行操作 有时文件没有任何错误,但仍然通过写入日志移动到错误文件夹C# 某些Excel文件未从共享路径移动到SQL Server,c#,sql-server,excel,windows-services,C#,Sql Server,Excel,Windows Services,我们有一个应用程序,其中Excel文件中的数据(存在于共享路径中)移动到数据库。如果发生任何错误,文件将通过将错误写入日志文件移动到错误文件夹。它使用windows服务执行操作 有时文件没有任何错误,但仍然通过写入日志移动到错误文件夹外部表不是预期格式。但同一文件再次上载一次或多次,移动到数据库时没有任何错误 XP服务器中存在windows服务、数据库和共享路径。这些年来应用程序运行良好。但最近几天,几乎每个文件都出现了上述问题 我们还安装了Microsoft 2003、20072012 off
外部表不是预期格式。
但同一文件再次上载一次或多次,移动到数据库时没有任何错误
XP服务器中存在windows服务、数据库和共享路径。这些年来应用程序运行良好。但最近几天,几乎每个文件都出现了上述问题
我们还安装了Microsoft 2003、20072012 office组件和访问引擎。但问题仍然存在
我在下面提到Windows服务代码。请帮忙。提前谢谢
using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;
namespace Impexp_Service
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer T1 = new System.Timers.Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
///start
///
{
SqlConnection strconnection = new SqlConnection();
strconnection.ConnectionString = @"Data Source=XXXXXX;Initial Catalog=XXXX;User ID=XX;Password=XXXXXX;";
strconnection.Open();
// To get the all files placed at the shared path
DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\");
FileInfo[] files = directory.GetFiles("*.xlsx");
foreach (var f in files)
{
string path = f.FullName;
// TO establish connection to the excel sheet
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
DbDataReader dr = cmd.ExecuteReader();
// OleDbDataReader dReader;
// dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "imp_master_test";
sqlBulk.WriteToServer(dr);
excelConnection.Close();
File.Delete(path);
// To move error files to the error folder
/// end
T1.Interval = 20000;
T1.Enabled = true;
T1.Start();
T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
}
}
}
void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
T1.Enabled = false;
try
{
SqlConnection strconnection = new SqlConnection();
strconnection.ConnectionString = @"Data Source=10.91.XXXXXX;Initial Catalog=XXXXX;User ID=XXXXX;Password=XXXXX;";
strconnection.Open();
// To get the all files placed at the shared path
DirectoryInfo directory = new DirectoryInfo(@"D:\Impexp\Data\");
FileInfo[] files = directory.GetFiles("*.xlsx");
foreach (var f in files)
{
string path = f.FullName;
// TO establish connection to the excel sheet
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
try
{
excelConnection.Open();
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
DbDataReader dr = cmd.ExecuteReader();
// OleDbDataReader dReader;
// dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "imp_master_prod";
sqlBulk.WriteToServer(dr);
excelConnection.Close();
File.Delete(path);
}
// To move error files to the error folder
catch (Exception exp)
{
excelConnection.Close();
File.Move(path, Path.Combine(@"D:\Impexp\error\", f.Name));
string path1 = @"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine("File : " + path + " : " + exp.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
}
strconnection.Close();
// End of TRY 1
}
catch (UnauthorizedAccessException UAEx)
{
string path1 = @"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine(UAEx.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
catch (PathTooLongException PathEx)
{
string path1 = @"D:\Impexp\error\error.txt";
if (File.Exists(path1))
{
// Create a file to write to.
using (StreamWriter sw = File.AppendText(path1))
{
sw.WriteLine(PathEx.Message);
sw.Flush();
}
}
T1.Enabled = true;
T1.Start();
}
T1.Enabled = true;
T1.Start();
}
protected override void OnStop()
{
}
}
}
现在来看,这似乎是读取excel文件而不是SQL表的问题。尝试更改Excel连接字符串
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
到
另外,查看一下,根本原因可能是上载了较新版本的excel。您使用的excel 2007文件的连接字符串是否使用Microsoft.Jet.OLEDB.4.0和Extended Properties=excel 8.0 您可以将字符串更改为其他字符串,如下所示:
public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
或
从以下链接更改连接字符串:
我搜索了一些有关OLEDB COM和更新版本Excel的信息。似乎有很多人与他们存在兼容性问题 不幸的是,微软似乎没有对此给予任何关注。微软在很多年前就宣布了OLEDB功能的贬值,并且他们已经停止在其Office产品和SQL Server中添加任何类型的内部支持。事实上 MSAccess Web应用程序和Web数据库的官方关闭日期为2018年4月。在这种情况下,对服务器、windows客户端版本或Excel客户端版本的更新可能触发了此问题,而且看起来不会有修复。我自己已经开始使用第三方软件包(免费的)来处理与office产品的互操作,因为我已经厌倦了用头撞墙来创建解决方案。老实说,如果Access取消了以编程方式连接Access数据库的功能,我不知道为什么Access仍然存在
我知道这并不能解决你的问题,但面对事实,继续前进比试图解决无法解决的问题要好。假设“很多文件”都失败了,那么通过的文件有什么“不同”吗?(例如,表的数量,[报告]表中记录的数量,…)。最坏的情况是,仔细查看失败和正常文件:解压缩它们并比较所有(相关)文件。PS:根据我(有限)的经验,通过从excel加载数据可能会由于某些区域设置问题(在服务器和/或客户端计算机上)而失败。附言:我认为这不重要,因为它在过去一直有效;但是连接字符串不应该说
Excel12.0XML
@谢谢你的评论,先生。您能解释一下导致此问题的区域设置吗?我的连接字符串状态为::string excelConnectionString=@“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=“+path+”;Extended Properties=\”Excel 12.0;HDR=是;IMEX=1\“;”
excel12.0前后有一个\号;HDR=是;IMEX=1。这是造成问题的原因吗?理论上,excel以“本机格式”存储其信息,因此日期在内部存储为双精度格式。但是,有时用户会将粘贴内容从一个地方复制到另一个地方,最后将日期信息等粘贴到excel中作为字符串。即使单元格格式为“日期”,Excel也会接受此选项。在那里设置2000-04-18
看起来不错,但如果设置为excel,则没有任何意义,因为区域设置要求2000/04/18
(示例)。当您尝试使用OLEDB读取此内容时,您可能期望给定列的日期时间,但会显示字符串(包含无法识别的日期信息)。至于连接字符串:没有线索。我只是简单地将您指向ConnectionString网站,因为我注意到缺少Xml
部分。判断事情是否重要的最好方法是简单地尝试一下可能产生的影响。谢谢你花时间让我们知道你对这个问题的看法。非常感谢。
public static string path = @"C:\src\RedirectApplication\RedirectApplication\301s.xlsx";
public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";