【问题标题】:DBF File name truncatedDBF 文件名被截断
【发布时间】:2017-04-23 00:54:04
【问题描述】:

我使用下面的代码创建了一个 dbf 文件并填充它,一切正常。问题是文件名被截断为最多 8 个字符。知道为什么或如何保持全名吗?

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties=dBase IV";
OleDbConnection connection = new OleDbConnection(connectionString);
        connection.Open();
cmd.CommandText = @"CREATE TABLE calendfull(
                                date1 datetime ,
                                day1 int ,
                                month1 int ,
                                year1 int ,
                                dow int ,
                                endmonth int  
                                )";

            cmd.ExecuteNonQuery();

  foreach (DataRow row in calend.Rows)
            {
                day = Convert.ToInt32(row["day"]);
                year = Convert.ToInt32(row["year"]);
                month = Convert.ToInt32(row["month"]);
                dow = Convert.ToInt32(row["dow"]);
                endmonth = Convert.ToInt32(row["endmonth"]);
                date1 = Convert.ToDateTime(row["date1"]);

                cmd.CommandText = @"insert into calendFull  values ('" + date1 + "'," + day + "," + month + "," + year + "," + dow + "," + endmonth + ")";
                cmd.ExecuteNonQuery();
            }

【问题讨论】:

  • 创建文件的代码在哪里?我在这里遗漏了什么吗?
  • 我更新了问题以包含整个代码,创建和填充

标签: c# oledb dbf


【解决方案1】:

DBF 文件名遵循 8.3 命名规范,因此名称不能超过 8 个字符。

但是等等,我看到一些名称大于 8 个字符的 dbf 文件。

是的,这是允许的,但是当您在命令提示符中查看时,它们的实际名称类似于文件名。例如largef~1

那么我该如何阅读它们?您应该使用 kernel32.dll

获取它们的简称

代码如下:

    public string GetShortFileName(string fileDirectory, string fileNameWithExtension)
    {
        StringBuilder temp = new StringBuilder(255);

        string path = System.IO.Path.Combine(fileDirectory, fileNameWithExtension);

        int n = GetShortPathName(path, temp, 255);

        if (n == 0)
            throw new NotImplementedException();

        string extension = System.IO.Path.GetExtension(path);

        return ((temp.ToString().Split('\\')).Last()).ToLower();//.Replace(extension, string.Empty);
    }

    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            public static extern int GetShortPathName(
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]    
        string path,
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]    
        StringBuilder shortPath,
        int shortPathLength);

【讨论】:

  • 使用kernel32.dll获取短名称?我与 dBASE 和 FoxPro 合作多年。文件名可以并且现在超过 8 个字符,并且可以在 Windows 中通过全名引用。这张图片还有更多内容 - 显然没有显示,因为 OP 提供的代码与文件或其名称无关。
  • 我多年来一直在使用dbf 文件。我已经看到了这个问题。你可以搜索一下(dbf文件名限制),看看我是不是真的。
  • @MichaelPerrenoud 看到这个(social.msdn.microsoft.com/Forums/en-US/visualfoxprogeneral/…)。似乎在使用 Microsoft.Jet.Oledb 时会发生
【解决方案2】:
        const string connectionString = @"Provider = vfpoledb; Data Source = {0}; Collating Sequence = general;";
        OleDbConnection conn = new OleDbConnection(string.Format(connectionString, dirName));
        conn.Open();
        OleDbCommand cmd = new OleDbCommand(string.Format("select * from {0}", fileName), conn)

更改 oledb 提供程序:

Use Microsoft OLE DB Provider for Visual FoxPro 9.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2016-05-24
    相关资源
    最近更新 更多