close

前言:

當資料庫有多個欄位的時候,一個一個KEY抓資料消耗太多時間!

原本想用DataView的某方法去抓,發現根本沒有相應的解決方案

還是回歸原本的OleDbDataReader來做了

程式碼:

   public string Get(int id)
        {
            string header = "";
            String strQuery;
            String strConn;          
            strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data") + @"\Database1.accdb";
            strQuery = "SELECT * FROM WRDS3";
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbCommand comm = new OleDbCommand(strQuery, conn);
            conn.Open();
            OleDbDataReader DR = comm.ExecuteReader();
            //依照選擇的資料表名稱,combo box填入相對應的欄位名稱
            for (int i = 0; i < DR.FieldCount; i++) {
                header += (i== DR.FieldCount-1)? DR.GetName(i) : DR.GetName(i) + ",";
            }
            conn.Close();
            string[] colum = header.Split(',');
            string SQLstr2 ="SELECT * FROM WRDS3";
            ads2 = new AccessDataSource();
            ads2.DataFile = databaseName;
            ads2.SelectCommand = SQLstr2;
            DataView dv = (DataView)ads2.Select(DataSourceSelectArguments.Empty);
            string data = "";
            if (dv.Count != 0)
            { 
                for (int i = 0; i < 10; i++)
                {
                    for (int col=0;col< colum.Length;col++) {
                        data+= dv[i][colum[col]].ToString()+",";
                    }
                    data += "\r\n";
                 }
            }
            ads2.Dispose();          
            return data;
        }

arrow
arrow
    全站熱搜

    程式小試身手 發表在 痞客邦 留言(0) 人氣()