Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用.NET提取FoxPro备注字段中的数据?_C#_Oledb_Visual Foxpro_Foxpro - Fatal编程技术网

C# 如何使用.NET提取FoxPro备注字段中的数据?

C# 如何使用.NET提取FoxPro备注字段中的数据?,c#,oledb,visual-foxpro,foxpro,C#,Oledb,Visual Foxpro,Foxpro,我正在编写一个C#程序,将FoxPro数据库转换为XML,除备注字段为空外,其他一切都正常工作。有没有什么东西是我在转换那个位时遗漏的 我使用的是C#.NET3.5SP1,VisualFoxpro9SP1OLEDB驱动程序。连接字符串正常,因为所有其他数据都被正确提取 当我将FoxPro数据库转换为SQL Server时,备注字段也为空,因此我无法转换两次。我对C#或FoxPro或SQL Server不是很熟悉,因此我无法在这方面给你太多建议 但是,如果找不到合适的驱动程序,可以考虑自己解析原始

我正在编写一个C#程序,将FoxPro数据库转换为XML,除备注字段为空外,其他一切都正常工作。有没有什么东西是我在转换那个位时遗漏的

我使用的是C#.NET3.5SP1,VisualFoxpro9SP1OLEDB驱动程序。连接字符串正常,因为所有其他数据都被正确提取


当我将FoxPro数据库转换为SQL Server时,备注字段也为空,因此我无法转换两次。

我对C#或FoxPro或SQL Server不是很熟悉,因此我无法在这方面给你太多建议

但是,如果找不到合适的驱动程序,可以考虑自己解析原始数据和备忘录文件。另一个问题涉及到这一点:

信不信由你,如果你决定编写自己的C#解析器,这些文件格式很容易解析。这些规范可从Microsoft获得:


我使用ODBC链接VFP 8表,备注字段工作正常。我不知道OLEDB是否不同


这里可能没有Visual FoxPro表格。许多VFP系统使用与其替换的FoxPro 2或dBase应用程序相同的表。您可以查看文件头或尝试其他ODBC驱动程序,看看它们是否工作。

最后不得不自己做一些工作,但它可能会在将来帮助其他人:

        public static object GetDbaseOrFoxproRawValue(string DBPath, string TableName, string ColumnName, 
        string CompareColumnName, string CompareValue, bool CompareColumnIsAutoKey)
    {
        using (BinaryReader read = new BinaryReader(File.Open(
            Path.Combine(DBPath, TableName + ".dbf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
        {
            // Is it a type of file that I can handle?
            if (new byte[] { 0x02, 0x03, 0x30, 0x43, 0x63, 0x83, 0x8b,
                             0xcb, 0xf5, 0xfb }.Contains(read.ReadByte()))
            {
                // Skip date.
                read.BaseStream.Seek(3, SeekOrigin.Current);

                // Read useful datas...
                uint RecordCount = read.ReadUInt32();
                ushort FirstRecord = read.ReadUInt16();
                ushort RecordLength = read.ReadUInt16();
                int FieldCount = FirstRecord - 296 / 32;

                // Make sure things aren't stupid.
                ColumnName = ColumnName.ToLower();
                CompareColumnName = CompareColumnName.ToLower();

                // Find target column (field)
                string temp;
                UInt32 CompareFieldOffset = uint.MaxValue, FieldOffset = uint.MaxValue;
                byte CompareFieldLength = 0, FieldLength = 0;
                char FieldType = ' ';
                for (int i = 0; i < FieldCount; i++)
                {
                    read.BaseStream.Seek(32 + (i * 32), SeekOrigin.Begin);
                    temp = Encoding.ASCII.GetString(read.ReadBytes(11)).Replace("\0", "").ToLower();
                    if (temp == CompareColumnName)
                    {
                        read.ReadChar();
                        CompareFieldOffset = read.ReadUInt32();
                        CompareFieldLength = read.ReadByte();
                    }
                    if (temp == ColumnName)
                    {
                        FieldType = read.ReadChar();
                        FieldOffset = read.ReadUInt32();
                        FieldLength = read.ReadByte();
                    }

                    if (CompareFieldOffset != uint.MaxValue && FieldOffset != uint.MaxValue)
                        break;
                }

                // Make sure we can continue.
                if (CompareFieldOffset == uint.MaxValue || 
                    FieldOffset == uint.MaxValue) return null;

                // Iterate through each record to find the one we want.
                for (int index = 0; index < RecordCount; index++)
                {
                    read.BaseStream.Seek(FirstRecord + (index * RecordLength) + CompareFieldOffset, SeekOrigin.Begin);
                    temp = Encoding.Default.GetString(read.ReadBytes(CompareFieldLength)).Replace("\0", "");
                    if (temp == CompareValue)
                    {
                        read.BaseStream.Seek(FirstRecord + (index * RecordLength) + FieldOffset, SeekOrigin.Begin);
                        switch (FieldType)
                        {
                            case 'M':
                            case 'I': return read.ReadUInt32();
                            case 'C':
                            default: return Encoding.Default.GetString(read.ReadBytes(FieldLength)).Replace("\0", "");
                        }
                    }
                }
            }
            else
            {
                return null;
            }
        }

        return null;
    }
public静态对象GetDbaseOrFoxproRawValue(字符串DBPath、字符串TableName、字符串ColumnName、,
字符串CompareColumnName、字符串CompareValue、bool CompareColumnIsAutoKey)
{
使用(BinaryReader read=新的BinaryReader(File.Open(
Combine(DBPath,TableName+“.dbf”)、FileMode.Open、FileAccess.Read、FileShare.ReadWrite)
{
//它是我可以处理的文件类型吗?
如果(新字节[]{0x02,0x03,0x30,0x43,0x63,0x83,0x8b,
0xcb,0xf5,0xfb}.Contains(read.ReadByte())
{
//跳过日期。
read.BaseStream.Seek(3,SeekOrigin.Current);
//阅读有用的数据。。。
uint RecordCount=read.ReadUInt32();
ushort FirstRecord=read.ReadUInt16();
ushort-RecordLength=read.ReadUInt16();
int FieldCount=FirstRecord-296/32;
//确保事情不愚蠢。
ColumnName=ColumnName.ToLower();
CompareColumnName=CompareColumnName.ToLower();
//查找目标列(字段)
字符串温度;
UInt32 CompareFieldOffset=uint.MaxValue,FieldOffset=uint.MaxValue;
字节比较字段长度=0,字段长度=0;
char FieldType='';
对于(int i=0;i

只需从中获取结果并将其用作备忘录文件的索引(使用MSDN文档,该代码非常简单)。

谈论如何使事情变得棘手-有几种方法可以在.NET中打开FoxPro表-通过Access链接很简单…@Matt:也许你应该将其作为答案发布?仍然没有运气。使用此连接字符串(string.Format()'d当然):“Driver={{Microsoft Visual FoxPro Driver}};SourceType=DBF;SourceDB={0};Exclusive=No;Collate=Machine;NULL=No;DELETED=No;BACKGROUNDFETCH=No;”尝试正常查询并作为左侧查询(列_name,8000)。复制并粘贴此代码时,收到一个错误“无法访问IList.Contains的显式实现”。在
Contains
之前添加
.ToList()
修复了此问题。关于从文档的何处开始使用此索引从备注字段获取数据,有什么建议吗?@reggaeguitar我的摇滚明星朋友,你是真正的节约者!谢谢