ASP.NET-利用Oledb讀取DBF資料庫(Oledb load .dbf file)

使用Oledb讀取DBF資料庫可參考下列兩個methods,實際呼叫參考最下面的code,把讀出檔案匯入datatable     

        using System.Data.OleDb;

        // 利用Oledb讀取DBF資料庫
        public static OleDbConnection OleDbDbfOpenConn(string DatabaseDirectory)
        {
            string cnstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DatabaseDirectory + "; Extended Properties=dBASE IV; User ID=Admin; Password=;");
            OleDbConnection icn = new OleDbConnection();
            icn.ConnectionString = cnstr;
            if (icn.State == ConnectionState.Open) icn.Close();
            icn.Open();
            return icn;
        }

        public static DataTable GetOleDbDbfDataTable(string DatabaseDirectory, string OleDbString)
        {
            DataTable myDataTable = new DataTable();
            OleDbConnection icn = OleDbDbfOpenConn(DatabaseDirectory);
            OleDbDataAdapter da = new OleDbDataAdapter(OleDbString, icn);
            DataSet ds = new DataSet();
            ds.Clear();
            da.Fill(ds);
            myDataTable = ds.Tables[0];
            if (icn.State == ConnectionState.Open) icn.Close();
            return myDataTable;
        }

        string databaseDirectory = @"D:\AAA\";
        string database = "test.DBF";
        string sql = "select * from " + database;
        DataTable dt_dbf = GetOleDbDbfDataTable(databaseDirectory, sql);

留言

這個網誌中的熱門文章

Python-相關係數矩陣實作(python-correlation matrix )

ASP.NET-後端將值傳給javascript

ASP.NET-FileUpload上傳後自動觸發button click(FileUpload upload auto trigger button click)