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、正则判别使用
private void txtNumber_TextChanged(object sender, TextChangedEventArgs e) { e.Handled = System.Text.RegularExpressions.Regex.IsMatch(txtNumber.Text, @"^-?[0-9]d*$"); if (txtNumber.Text != "admin") { if (e.Handled == false && txtNumber.Text != "") { CustomMessageBox.CustomMessageBox.ShowBox("请输入整数![0~9]", "提示"); txtNumber.Text = ""; Keyboard.Focus(txtNumber); return; } } } public static bool checkString(string source) { Regex regExp = new Regex("[~!@#$%^&*()=+[\]{}''";:/?.,><`|!·¥…—()\-、;:。,》《]"); return !regExp.IsMatch(source); }