DataTable GetPagedTable(DataTable dt, currentPageIndex, pageSize) { (currentPageIndex == ) dt; DataTable newdt = dt.Copy(); newdt.Clear(); rowbegin = (currentPageIndex - ) * pageSize; rowend = currentPageIndex * pageSize; (rowbegin >= dt.Rows.Count) newdt; (rowend > dt.Rows.Count) rowend = dt.Rows.Count; ( i = rowbegin; i <= rowend - ; i++) { DataRow newdr = newdt.NewRow(); DataRow dr = dt.Rows[i]; (DataColumn column dt.Columns) { newdr[column.ColumnName] = dr[column.ColumnName]; } newdt.Rows.Add(newdr); } newdt; } DataTable CompareTables(DataTable first, DataTable second) { first.TableName = ; second.TableName = ; DataTable table = DataTable(); { (DataSet ds = DataSet()) { ds.Tables.AddRange( DataTable[] { first.Copy(), second.Copy() }); DataColumn[] firstcolumns = DataColumn[ds.Tables[].Columns.Count]; ( i = ; i < firstcolumns.Length; i++) { firstcolumns[i] = ds.Tables[].Columns[i]; } DataColumn[] secondcolumns = DataColumn[ds.Tables[].Columns.Count]; ( i = ; i < secondcolumns.Length; i++) { secondcolumns[i] = ds.Tables[].Columns[i]; } DataRelation r = DataRelation(.Empty, firstcolumns, secondcolumns, ); ds.Relations.Add(r); ( i = ; i < first.Columns.Count; i++) { table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType); } table.BeginLoadData(); (DataRow parentrow ds.Tables[].Rows) { DataRow[] childrows = parentrow.GetChildRows(r); (childrows == || childrows.Length == ) table.LoadDataRow(parentrow.ItemArray, ); } table.EndLoadData(); } } (Exception ex) { ex; } table; }
USE pubs GO --使用帶有簡單 CASE 函數的 SELECT 語句 SELECT Category = CASE type WHEN 'popular_comp' THEN 'Popular Computing' WHEN 'mod_cook' THEN 'Modern Cooking' WHEN 'business' THEN 'Business' WHEN 'psychology' THEN 'Psychology' WHEN 'trad_cook' THEN 'Traditional Cooking' ELSE 'Not yet categorized' END, CAST(title AS varchar(25)) AS 'Shortened Title', price AS Price FROM titles WHERE price IS NOT NULL ORDER BY type, price COMPUTE AVG(price) BY type GO --使用帶有簡單 CASE 函數和 CASE 搜索函數的 SELECT 語句 SELECT 'Price Category' = CASE WHEN price IS NULL THEN 'Not yet priced' WHEN price < 10 THEN 'Very Reasonable Title' WHEN price >= 10 and price < 20 THEN 'Coffee Table Title' ELSE 'Expensive book!' END, CAST(title AS varchar(20)) AS 'Shortened Title' FROM titles ORDER BY price GO --使用帶有 SUBSTRING 和 SELECT 的 CASE 函數 SELECT SUBSTRING((RTRIM(a.au_fname) + ' '+ RTRIM(a.au_lname) + ' '), 1, 25) AS Name, a.au_id, ta.title_id, Type = CASE WHEN SUBSTRING(ta.title_id, 1, 2) = 'BU' THEN 'Business' WHEN SUBSTRING(ta.title_id, 1, 2) = 'MC' THEN 'Modern Cooking' WHEN SUBSTRING(ta.title_id, 1, 2) = 'PC' THEN 'Popular Computing' WHEN SUBSTRING(ta.title_id, 1, 2) = 'PS' THEN 'Psychology' WHEN SUBSTRING(ta.title_id, 1, 2) = 'TC' THEN 'Traditional Cooking' END FROM titleauthor ta JOIN authors a ON ta.au_id = a.au_id --