一些Excel文件没有从共享路径移动到SQL Server
本文关键字:移动 路径 SQL Server 共享 Excel 文件 一些 | 更新日期: 2023-09-27 17:53:16
我们有一个应用程序,其中Excel文件中的数据(存在于共享路径中)移动到数据库。如果出现任何错误,则通过将错误写入日志文件将文件移动到错误文件夹。它使用windows服务进行操作。
有时文件没有任何错误仍然移动到错误文件夹写日志External table is not in the expected format.
,但同一文件再次上传一次或多次,它移动到数据库没有任何错误。
XP Server中存在windows服务、DB和共享路径。这些年来应用程序运行得很好。但是最近,几乎每个文件都出现了上述问题。
我们还安装了Microsoft 2003、2007、2012 office组件和access引擎。但是这个问题仍然存在。
我指的是下面的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()
{
}
}
}
我做了一些关于OLEDB com和更新版本的Excel的搜索。似乎很多人都有与它们兼容的问题。
不幸的是,看起来微软并没有对此给予任何关注。微软在许多年前就宣布放弃OLEDB功能,并且他们已经停止在Office产品和SQL服务器中添加任何类型的内部支持。事实上,MSAccess Web应用程序和Web数据库的官方关闭日期是2018年4月。在这种情况下,服务器的更新,windows的客户端版本,或者Excel的客户端版本可能会触发这个问题,而且看起来不会有修复。我自己已经开始使用第三方软件包(有免费的)来处理与办公产品的交互,因为我已经厌倦了绞尽脑汁去创建解决方案。老实说,我不知道为什么Access仍然存在,如果他们剥夺了以编程方式连接到Access数据库的能力。
我知道这并不能解决你的问题,但与其试图解决无法解决的问题,不如面对现实,继续前进。
看这个问题,这似乎是读取excel文件的问题,而不是SQL表。尝试更改Excel连接字符串。
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='"Excel 12.0;HDR=Yes;IMEX=1'";";
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和扩展属性=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;";
或
从下面的链接更改连接字符串:http://www.connectionstrings.com/excel - 2007