ASP.NET虛擬主機在創(chuàng)建數(shù)據(jù)源時的隱患淺析
ASP.NET虛擬主機在創(chuàng)建數(shù)據(jù)源時的隱患是什么呢?首先我們來看創(chuàng)建數(shù)據(jù)源的代碼(代碼在listdrivers.aspx.cs文件中):
- //通過此方法返回一個集合形式的數(shù)據(jù)視圖DataView
- ICollection CreateDataSource() {
- //定義內(nèi)存中的數(shù)據(jù)表DataTable
- DataTable dt = new DataTable();
- //定義DataTable中的一行數(shù)據(jù)DataRow
- DataRow dr;
- /*向DataTable中增加一個列,格式:DataColumn("Column", type)
- Column為數(shù)據(jù)列的名字,type為數(shù)據(jù)列的數(shù)據(jù)類型*/
- dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
- dt.Columns.Add(new DataColumn("drivers", typeof(string)));
- dt.Columns.Add(new DataColumn("detail", typeof(string)));
- //使用for循環(huán)將邏輯驅(qū)動器的名稱以行的形式添加到數(shù)據(jù)表DataTable中
- for (int i = 0; i < nNumOfDrives; i++) {
- //定義新行
- dr = dt.NewRow();
- //對行中每列進行賦值,注意要與上邊定義的DataTable的行相對應(yīng)
- dr[0] = i; //循環(huán)生成的序號
- dr[1] = achDrives[i].ToString(); //邏輯驅(qū)動器的名稱
- dr[2] = "查看詳情";
- //向DataTable中添加行
- dt.Rows.Add(dr);
- }
- //根據(jù)得到的DataTable生成自定義視圖DataView
- DataView dv = new DataView(dt);
- //返回得到的視圖DataView
- return dv;
- }
我們通過這個方法得到了一個包含所有我們需要的數(shù)據(jù)的數(shù)據(jù)視圖DataView,我們只需要在此aspx頁的Page_Load方法中將此數(shù)據(jù)視圖綁定到DataGrid上就可以了。
ASP.NET虛擬主機在創(chuàng)建數(shù)據(jù)源時的數(shù)據(jù)綁定代碼(代碼在listdrivers.aspx.cs文件中):
- /* 設(shè)置DataGrid的數(shù)據(jù)源DataSource為我們從CreateDataSource()方法得到的數(shù)據(jù)視圖DataView */
- DriversGrid.DataSource = CreateDataSource();
- //將此DataGrid進行數(shù)據(jù)綁定
- DriversGrid.DataBind();
通過上邊介紹的幾種主要方法我們就實現(xiàn)了獲取系統(tǒng)信息和顯示所有邏輯驅(qū)動器名稱的功能,并且可以通過相應(yīng)的鏈接進入下一個顯示目錄和文件名的程序listdir.aspx顯示該邏輯驅(qū)動器下的所有目錄和文件。
ASP.NET虛擬主機在創(chuàng)建數(shù)據(jù)源時的隱患就向你介紹到這里,希望對你有所幫助。
【編輯推薦】