Monday, May 31, 2010

Make connection with .dbf file and read and write with C# .

There are two way to make connection with .dbf file :

1. With ODBC Connection.

           string filepath = "c:\\nn\\";
           
           OdbcConnection CC = new OdbcConnection("Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=" + filepath + ";Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;");
            CC.Open();
           
            OdbcCommand cmd = new OdbcCommand("Select * From " + filepath + "smsout.dbf" , CC);
            OdbcDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                DataTable dt = new DataTable();
                dt.Load(dr);
            }
            CC.Close();

2. With OLEDB Connection.

            OleDbConnection ccc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath.Substring(0, filepath.LastIndexOf("\\")) + ";Extended Properties=dBASE IV;");
            ccc.Open();
            OleDbCommand cmd1 = new OleDbCommand("Select * From " + filepath + "smsout.dbf", ccc);
            OleDbDataReader dr1 = cmd1.ExecuteReader();
            if (dr1.HasRows)
            {
                DataTable dt1 = new DataTable();
                dt1.Load(dr1);
            }
            ccc.Close();

No comments:

Post a Comment