C# sw.Dispose(); } fs.Close(); fs.Dispose(); } } } }
检查您用于实际执行C# sw.Dispose(); } fs.Close(); fs.Dispose(); } } } },c#,sql,ms-access,csv,jet,C#,Sql,Ms Access,Csv,Jet,检查您用于实际执行SELECT*INTO…查询的代码,看看您是否能找到您的代码与我的代码之间的显著差异。最后,我找到了问题所在。 首先,您应该注意要导入的文件的字符集。如果将schema.ini文件设置为ANSI,则必须确保该文件为ANSI。 然后,我还遇到了一些关于“DecimalSymbol=,”选项的问题。移除后,导入工作正常。无论如何,使用我的区域设置(Italia(Italiano))应该是正确的,因为十进制符号是,' 这是创建schema.ini文件的最后一个方法 <conne
SELECT*INTO…
查询的代码,看看您是否能找到您的代码与我的代码之间的显著差异。最后,我找到了问题所在。
首先,您应该注意要导入的文件的字符集。如果将schema.ini
文件设置为ANSI,则必须确保该文件为ANSI。
然后,我还遇到了一些关于“DecimalSymbol=,”
选项的问题。移除后,导入工作正常。无论如何,使用我的区域设置(Italia(Italiano))应该是正确的,因为十进制符号是,'
这是创建schema.ini
文件的最后一个方法
<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>
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("Format=Delimited(;)");
sw.WriteLine("DateTimeFormat=yyyy-MM-dd");
sw.WriteLine("CharacterSet=ANSI");
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();
}
}
感谢您的测试!至少我知道它是有效的。。。以某种方式我将
AccessDb.Query()
方法定义添加到原始帖子中。看看…@RainbowCoder好吧,我注意到的一件事是,你的代码使用的是OleDbCommand
对象,而我的代码使用的是OdbcCommand
对象。MSDN页面“”肯定是MSDN库的ODBC部分的一部分。好的。。。但是,既然OLEDB是一个围绕ODBC的微软“包装器”,这是一个旧的API,为什么它不能工作呢?另请参见:OLEDB应该比ODBC更有效。无论如何,我不能重构AccessDb类,因为我还使用它访问非关系数据库,如.XLS和.XML文件。你能试一下Oledb驱动程序和我的.MDB文件连接字符串吗?@RainbowCoder一件可能会有所不同的事情是Windows区域设置。我的是“英语(美国)”。如果您的是不同的,那么您可以尝试在Windows的区域设置控制面板中将其临时设置为“English(United States)”,看看这是否有什么不同。(不应该,但是…)您添加的代码很有用,但是我们仍然不知道您的connString
是什么样子。添加了连接字符串!
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);
}
}
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();
}
}
<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>
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("Format=Delimited(;)");
sw.WriteLine("DateTimeFormat=yyyy-MM-dd");
sw.WriteLine("CharacterSet=ANSI");
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();
}
}