如何从数据行中读取值。数据库

How to read values from datarows. OleDB

我正在通过 oledb 读取 dbf 文件。我正在获取字段名及其类型,但问题是我没有在行中获取任何值。

我的代码如下:

string mySQL = "select * from line75.dbf";  // dbf table name
                OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);

                DA.Fill(YourResultSet);

                for (int i = 0; i < YourResultSet.Columns.Count; i++)
                {
                    FieldInfo field = new FieldInfo();
                    field.fieldname = YourResultSet.Columns[i].ColumnName;
                    field.fieldtype = YourResultSet.Columns[i].DataType.FullName;

                    tableFields.Add(field);
                }
                dataGridView1.DataSource = YourResultSet;

当我在 datagridview 查看时,我得到的只是 fieldnames 但我没有得到任何价值。

试了很多dbf文件,结果都是一样的

我做错了什么?我很困惑,因为我只是打开连接并尝试获取所有数据。

类似这样的东西,它应该可以工作

        using (OleDbConnection _connection = new OleDbConnection())
        {
            var ConnectionString = new StringBuilder("");
            ConnectionString.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;");
            ConnectionString.Append(@"Extended Properties=Paradox 5.x;");
            ConnectionString.Append(@"Data Source=D:\dbf;");
            _connection.ConnectionString = ConnectionString.ToString();
            _connection.Open();

            using (OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM line75.dbf;", _connection))
            {
                using (DataSet dsRetrievedData = new DataSet())
                {
                    da.Fill(dsRetrievedData);
                    dataGridView1.DataSource = dsRetrievedData;
                    dataGridView1.DataMember = dsRetrievedData.Tables[0].TableName;
                }
            }
        }

编辑

我自己试过了

1 我创建了简单的 WinForms 应用程序

2 从此 https://www.ibiblio.org/laslave/downloads/

下载 dbf

3 然后在Form中添加组件DataGridView

4 然后添加这段代码

using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            using (OleDbConnection _connection = new OleDbConnection())
            {
                var ConnectionString = new StringBuilder("");
                ConnectionString.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;");
                ConnectionString.Append(@"Extended Properties=dBASE IV;");
                ConnectionString.Append(@"Data Source=c:\temp;");
                _connection.ConnectionString = ConnectionString.ToString();
                _connection.Open();

                using (OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM SLAVE.DBF", _connection))
                {
                    using (DataSet dsRetrievedData = new DataSet())
                    {
                        da.Fill(dsRetrievedData);
                        dataGridView1.DataSource = dsRetrievedData;
                        dataGridView1.DataMember = dsRetrievedData.Tables[0].TableName;
                    }
                }
            }
        }
    }
}

5 一切正常