Home  >  Article  >  Database  >  DOTNET最近的一些技巧

DOTNET最近的一些技巧

WBOY
WBOYOriginal
2016-06-07 15:05:46931browse

有时我们需要从DataTable中抽取Distinct数据,以前总是以对DataTable进行foreach之类纯手工方式获

有时我们需要从DataTable中抽取Distinct数据,以前总是以对DataTable进行foreach之类纯手工方式获取。

近来发现DataView可以帮我们直接获取Distinct数据,汗一个!

DataTable dataTable;

DataView dataView = dataTable.DefaultView;

DataTable dataTableDistinct = dataView.ToTable(true,"FieldName1","FieldName2","...");//注:其中ToTable()的第一个参数为是否DISTINCT

 

 

DataTable分页

/// 


 2         /// 对DataTable进行分页,起始页为1
 3         /// 
 4         /// 
 5         /// 
 6         /// 
 7         /// 
 8        public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
 9         {
10             if (PageIndex == 0)
11                 return dt;
12             DataTable newdt = dt.Copy();
13             newdt.Clear();
14 
15             int rowbegin = (PageIndex - 1* PageSize;
16             int rowend = PageIndex * PageSize;
17 
18             if (rowbegin >= dt.Rows.Count)
19                 return newdt;
20 
21             if (rowend > dt.Rows.Count)
22                 rowend = dt.Rows.Count;
23             for (int i = rowbegin; i  rowend - 1; i++)
24             {
25                 DataRow newdr = newdt.NewRow();
26                 DataRow dr = dt.Rows[i];
27                 foreach (DataColumn column in dt.Columns)
28                 {
29                     newdr[column.ColumnName] = dr[column.ColumnName];
30                 }
31                 newdt.Rows.Add(newdr);
32             }
33 
34             return newdt;
35         }

 

 

 

 NET中Web Service和Web Form获得客户端IP的方法 收藏
在.NET中,WebService和WebForm页面,使用了不同的获得客户IP方法。
        注:以下方法在.NET2.0上测试通过。

Web Service使用(通过HttpContext对象调用Request对象):

HttpContext.Current.Request.UserHostAddress

HttpContext.Current.Request.ServerVariables.GetValues("REMOTE_ADDR")[0]

Web Form使用(可直接使用Request对象):

Request.ServerVariables.GetValues("REMOTE_ADDR")[0]

HttpContext.Current.Request.UserHostAddress

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn