首页 > 代码库 > 项目知识点总结
项目知识点总结
1、将对象modelList转换为DataTable表
private DataTable ListToDataTable(List<Tellyes.Model.QuestionOption> QuestionOptionList) { DataTable dt = new DataTable(); if (QuestionOptionList.Count > 0) { PropertyInfo[] propertys = QuestionOptionList[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { System.Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))//出现可能为空的情况 { colType = pi.PropertyType.GetGenericArguments()[0]; } dt.Columns.Add(pi.Name, colType); } for (int i = 0; i < QuestionOptionList.Count; i++) { ArrayList al = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(QuestionOptionList[i], null); al.Add(obj); } object[] array = al.ToArray(); dt.LoadDataRow(array, true); } } return dt; }
2、查询excel表格添加到dataset
public DataSet ExecleDs(string filenameurl) { string strConn = "Provider=Microsoft.ACE.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties=‘Excel 12.0; HDR=YES; IMEX=1‘"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataSet ds = new DataSet(); string strSql = string.Format("SELECT * FROM [{0}$]", "Sheet1"); OleDbDataAdapter odda = new OleDbDataAdapter(strSql, conn); odda.Fill(ds, "hou"); return ds; }
3、将一文件上传到服务器中:所有操作都在后台
string clientIP = "192.168.4.22"; string serverIP = "192.168.4.22"; string clientFilesName = "shangchuan"; string serverFilesName = @"QuestionUploadFile\" + userName; string fileName = "111.flv"; string clientPath = @"\\" + clientIP + "\\" + clientFilesName + "\\" + fileName; string serverPath = @"\\" + serverIP + "\\" + serverFilesName + "\\" + fileName; System.IO.File.Copy(clientPath, serverPath,true );
前提是:将文件shangchuan,QuestionUploadFile设置共享属性,同时在安全属性上是目标机器增加应有权限。
4、DataTable排序处理
实例:
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("id", typeof(int))); dt.Columns.Add(new DataColumn("name", typeof(string ))); dt.Columns.Add(new DataColumn("age", typeof(int))); for (int i = 0; i < 5; i++) { DataRow dr = dt.NewRow(); dr["id"] = i; dr["name"] = i + "jim"; dr["age"] = 10 - i; dt.Rows.Add(dr); } DataView dv = dt.DefaultView; dv.Sort = "age Asc"; DataTable dt2 = dv.ToTable();
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。