这是我的 .txt 文件的连接字符串和一些代码
public class FileTransfers
{
public void fileFromDrive(string filename)
{
FileInfo file = new FileInfo(filename);
string fileConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
file.DirectoryName +
"; Extended Properties='text;HDR=YES;FMT=Delimited(,)';";
using (OleDbConnection con = new OleDbConnection(fileConnectionString))
{
using (OleDbCommand cmd = new OleDbCommand(
string.Format("SELECT * FROM [{0}]", file.Name), con))
{
con.Open();
using (OleDbDataAdapter adp = new OleDbDataAdapter(cmd))
{
DataTable tbl = new DataTable("Attendance");
adp.Fill(tbl);
}
}
}
}
}
但问题是当我调试 tbl
中的记录时,它只显示一列中的数据,但在我的 .txt 文件中有 7 个多列和数百行。
我试过 FMT=Delimited(,), FMT=TabDelimited,FMT=FiXed
但没有得到多列。我知道每个条目的末尾都需要一个 (,),但我不能手动这样做。
最佳答案
正如 Jan Schreuder 在他的文章 Using OleDb to Import Text Files 中提到的那样,您需要考虑执行此过程的一些细节。 ,
The Jet engine makes assumptions about the content of the file. This can result in incorrect imports. For example, it might think a column contains date values. But in fact, your file should treat the columns as a string. In these cases, you should create a Schema.Ini file that describes the type of value for each column. The class creates a Schema.Ini file before it opens the delimited file, but only to specify what the delimiter is. You may want to change this to use pre-defined INI files that describe your input file.
因此,继续按照规定创建 schema.ini
文件,您的问题将全部消失。它的内容应该是这样的,
[FileName.csv]
ColNameHeader=True
Format=CSVDelimited
有关如何操作的更多详细信息,请参阅以下 MSDN 指南,
关于c# - .txt 文件格式的 Microsoft Jet OLEDB 4.0 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28061210/