Search code examples
c#visual-studio-2010oledbdbf

How to read values from datarows. OleDB


I am reading dbf file through oledb. I am getting fieldsname and their types but issue is that i am not getting any value in rows.

My code is as follows:

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;

when i view at datagridview, then i am getting just fieldnames but i am not getting any value.

I have tried many dbf files, but the result is the same.

What i am doing wrong here? I am very confused as i am simply opening connection and trying to get all data.


Solution

  • something like this, it should work

            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;
                    }
                }
            }
    

    EDIT

    I tried it by myself

    1 I created simple WinForms application

    2 Downloaded dbf from this https://www.ibiblio.org/laslave/downloads/

    3 Then added component DataGridView to Form

    4 Then added this code

    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 And all works fine enter image description here