c# - .txt 文件格式的 Microsoft Jet OLEDB 4.0 连接字符串

标签 c# sql-server-2008 file-io oledb oledbconnection

这是我的 .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 指南,

Schema.ini File (Text File Driver)

关于c# - .txt 文件格式的 Microsoft Jet OLEDB 4.0 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28061210/

相关文章:

c# - 执行多个线程

c# - ASP.NET MVC - 无法将 string[] 值传递给另一个 Controller 操作

sql - 处理 UNPIVOT 中的 NULL 值

python - 如何从python中的文件中读取多个词典?

类函数的 C++ 提取运算符

android - 写入 context.getFilesDir() 时为 "Permission denied"

c# - wcf 客户端在反序列化期间忽略派生类型

c# - 如何使用客户端对象模型授予用户对某些文件夹的权限?

sql-server - 获取 SQL-Server 还原作业的进度状态时出现问题

sql - 第二个最大值和最小值