C#使用schema.ini导入CSV文件创建表但不加载数据
作者:互联网
我需要将一些CSV文件导入MDB数据库内的临时表中.
这些文件来自ASP.NET Web应用程序上的文件上传.
这是我的CSV文件:
"Operating Unit Organization Name";"Year";"Sales Rep Name";"Date";"Week";"Product Number";"Account Name";"Customer Number";"Corporate Brand";"Brand";"Ordered Quantity";"Amount"
"IT Operating Unit";2014;"Name-561004";2014-02-21;"2014 Week08";"123456+";"Buyer name";"456789";"Corp Brand";"Brand";4;147,52
"IT Operating Unit";2014;"Name-561004";2014-02-21;"2014 Week08";"123.012EXP";"Buyer name";"789123";"Corp Brand";"Brand";10;204,9
"IT Operating Unit";2014;"Name-561004";2014-02-17;"2014 Week08";"101S-3";"Buyer name";"234567";"Another Corp Brand";"Another Brand";30;237,5
这是生成schema.ini文件的方法.由于它必须包含CSV文件的名称,因此每次上载新CSV时,我都会生成一个新的架构文件,因为我需要将它们全部保存在具有特定名称的文件夹中.
private void CreateCsvSchemaFile()
{
using (FileStream fs = new FileStream(Path.GetDirectoryName(FilePath) + "\\schema.ini", FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("[" + Path.GetFileName(FilePath) + "]");
sw.WriteLine("ColNameHeader=True");
//sw.WriteLine("MaxScanRows=0");
sw.WriteLine("Format=Delimited(;)");
sw.WriteLine("DateTimeFormat=yyyy-MM-dd");
sw.WriteLine("CharacterSet=ANSI");
sw.WriteLine("DecimalSymbol=,");
sw.WriteLine("Col1=\"Operating Unit Organization Name\" Text Width 255");
sw.WriteLine("Col2=\"Year\" Long");
sw.WriteLine("Col3=\"Sales Rep Name\" Text Width 255");
sw.WriteLine("Col4=\"Date\" DateTime");
sw.WriteLine("Col5=\"Week\" Text Width 255");
sw.WriteLine("Col6=\"Product Number\" Text Width 255");
sw.WriteLine("Col7=\"Account Name\" Text Width 255");
sw.WriteLine("Col8=\"Customer Number\" Text Width 255");
sw.WriteLine("Col9=\"Corporate Brand\" Text Width 255");
sw.WriteLine("Col10=\"Brand\" Text Width 255");
sw.WriteLine("Col11=\"Ordered Quantity\" Long");
sw.WriteLine("Col12=\"Amount\" Currency");
sw.Close();
sw.Dispose();
}
fs.Close();
fs.Dispose();
}
}
总会正确生成schema.ini文件,实际上,数据库上的表是使用正确的字段名称和类型创建的.
这是执行CSV导入的方法.
private void ImportCsvIntoTemp()
{
try
{
CreateCsvSchemaFile();
string query = @"SELECT * INTO TEMP_CSV
FROM [Text;HDR=no;Database={0}].[{1}]";
query = String.Format(query, Path.GetDirectoryName(FilePath), Path.GetFileName(FilePath));
AccessDb.Query(AccessDbConnString, query);
}
catch (Exception ex)
{
string message = String.Format("CSV file import failed. Inner Exception: {0}", ex.Message);
throw new ImportFailedException(message);
}
}
表TEMP_CSV已正确创建,但没有数据加载到其中.
相反,如果我使用Access打开MDB,则会看到8个空行.我试图更改schema.ini文件上的某些参数,例如添加/删除MaxScnaRows = 0,CharacterSet等…,并且在查询中我也尝试更改FROM属性,但是我总是得到相同的结果.
-编辑添加了AccessDb.Query()方法和数据库连接字符串-
我没有包括Query()方法,因为AccessDb只是一个“数据库层”类,其中包含创建与db的连接并执行带/不带参数的查询/列表查询/标量的方法.我在其他页面和许多其他应用程序中都使用了此类.
无论如何,这是AccessDb.Query()方法.
public static void Query(string connString, string query)
{
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (OleDbException odbEx)
{
throw odbEx;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Dispose();
conn.Close();
}
}
这是我在Web.config文件中配置的数据库连接字符串.
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Progetti\Personale\Progetti\Infomed\Database\Infomed.mdb;User Id=admin;Password=;" />
</connectionStrings>
解决方法:
您发布的代码本质上是正确的.我将其复制并粘贴到新的C#项目中,并对其进行了足够的调整以使其可以运行.当我确实运行它时,它可以正常工作,创建新表并将所有三(3)行导入其中.
主要区别在于,我仅使用以下代码来执行SELECT * INTO …查询.代替…
AccessDb.Query(AccessDbConnString, query);
…我用了…
using (OleDbConnection con = new OleDbConnection())
{
con.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=C:\Users\Public\test\CsvImportTest\MyDb.mdb;";
con.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
con.Close();
}
完整的代码是:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace CsvImportTest
{
class Program
{
static string FilePath = @"C:\Users\Public\test\CsvImportTest\TestData.csv";
static void Main(string[] args)
{
ImportCsvIntoTemp();
Console.WriteLine("Done.");
}
private static void ImportCsvIntoTemp()
{
try
{
CreateCsvSchemaFile();
string query = @"SELECT * INTO TEMP_CSV
FROM [Text;HDR=no;Database={0}].[{1}]";
query = String.Format(query, Path.GetDirectoryName(FilePath), Path.GetFileName(FilePath));
//AccessDb.Query(AccessDbConnString, query);
using (OleDbConnection con = new OleDbConnection())
{
con.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=C:\Users\Public\test\CsvImportTest\MyDb.mdb;";
con.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
}
con.Close();
}
}
catch (Exception ex)
{
string message = String.Format("CSV file import failed. Inner Exception: {0}", ex.Message);
Console.WriteLine(message);
//throw new ImportFailedException(message);
}
}
private static void CreateCsvSchemaFile()
{
using (FileStream fs = new FileStream(Path.GetDirectoryName(FilePath) + "\\schema.ini", FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("[" + Path.GetFileName(FilePath) + "]");
sw.WriteLine("ColNameHeader=True");
//sw.WriteLine("MaxScanRows=0");
sw.WriteLine("Format=Delimited(;)");
sw.WriteLine("DateTimeFormat=yyyy-MM-dd");
sw.WriteLine("CharacterSet=ANSI");
sw.WriteLine("DecimalSymbol=,");
sw.WriteLine("Col1=\"Operating Unit Organization Name\" Text Width 255");
sw.WriteLine("Col2=\"Year\" Long");
sw.WriteLine("Col3=\"Sales Rep Name\" Text Width 255");
sw.WriteLine("Col4=\"Date\" DateTime");
sw.WriteLine("Col5=\"Week\" Text Width 255");
sw.WriteLine("Col6=\"Product Number\" Text Width 255");
sw.WriteLine("Col7=\"Account Name\" Text Width 255");
sw.WriteLine("Col8=\"Customer Number\" Text Width 255");
sw.WriteLine("Col9=\"Corporate Brand\" Text Width 255");
sw.WriteLine("Col10=\"Brand\" Text Width 255");
sw.WriteLine("Col11=\"Ordered Quantity\" Long");
sw.WriteLine("Col12=\"Amount\" Currency");
sw.Close();
sw.Dispose();
}
fs.Close();
fs.Dispose();
}
}
}
}
检查用于实际执行SELECT * INTO …查询的代码,看是否可以发现代码和我的代码之间有显着差异.
标签:jet,csv,sql,c,ms-access 来源: https://codeday.me/bug/20191029/1962823.html