主要记录工作当中遇到的一些问题和总结的一些经验
客户端请求-->web服务接口-->sql 语句执行(存储在数据库中)-->web服务(客户端通过调用web服务接口)-->返回DataTable或Dataset(sql server)--> 统一的DataTable或Dataset转换成对象-->提交给客户端(xml、json等等其他的)
1、首先通过sql语句返回结果,返回的结果一般都以Dataset的形式和DataTable的形式返回。
2、统一的DataTable或Dataset转换成对象
1 #region 写对象信息 2 3 /// <summary> 4 /// 5 /// </summary> 6 /// <typeparam name="T"></typeparam> 7 /// <param name="tableName"></param> 8 /// <returns></returns> 9 public T WriteTObjectInfo<T>(string tableName, DataRow dr, string[] exceptArray) 10 { 11 try 12 { 13 if (this.Status == 0) 14 { 15 throw new Exception(this.Msg); 16 } 17 18 T item = Activator.CreateInstance<T>(); 19 20 List<Parameter> listParameter = GetProperties<T>(item, exceptArray); 21 22 foreach (Parameter p in listParameter) 23 { 24 foreach (DataColumn dc in this.dsResult.Tables[tableName].Columns) 25 { 26 if (dc.ColumnName == p.Name) 27 { 28 Type type = item.GetType(); 29 30 MethodInfo method = type.GetMethod("SetAttributeValue"); 31 32 method.Invoke(item, new object[] { p.Name, dr[p.Name].ToString().Trim() }); 33 } 34 } 35 } 36 37 return item; 38 } 39 catch (Exception ex) 40 { 41 throw new Exception("写" + Activator.CreateInstance<T>().ToString() + "信息发生错误,错误原因:" + ex.Message); 42 } 43 } 44 45 /// <summary> 46 /// 47 /// </summary> 48 /// <typeparam name="T"></typeparam> 49 /// <param name="tableName"></param> 50 /// <returns></returns> 51 public T WriteTObjectInfo<T>(string tableName) 52 { 53 try 54 { 55 if (this.Status == 0) 56 { 57 throw new Exception(this.Msg); 58 } 59 60 T item = Activator.CreateInstance<T>(); 61 62 if (this.dsResult.Tables.Contains(tableName)) 63 { 64 DataRow dr = this.dsResult.Tables[tableName].Rows[0]; 65 66 List<Parameter> listParameter = GetProperties<T>(item); 67 68 foreach (Parameter p in listParameter) 69 { 70 foreach (DataColumn dc in this.dsResult.Tables[tableName].Columns) 71 { 72 if (dc.ColumnName == p.Name) 73 { 74 Type type = item.GetType(); 75 76 MethodInfo method = type.GetMethod("SetAttributeValue"); 77 78 method.Invoke(item, new object[] { p.Name, dr[p.Name].ToString().Trim() }); 79 } 80 } 81 } 82 } 83 84 return item; 85 } 86 catch (Exception ex) 87 { 88 throw new Exception("写" + Activator.CreateInstance<T>() + "信息发生错误,错误原因:" + ex.Message); 89 } 90 } 91 92 /// <summary> 93 /// 94 /// </summary> 95 /// <typeparam name="T"></typeparam> 96 /// <param name="tableName"></param> 97 /// <returns></returns> 98 public T WriteTObjectInfo<T>(string tableName, string[] exceptArray) 99 { 100 try 101 { 102 if (this.Status == 0) 103 { 104 throw new Exception(this.Msg); 105 } 106 107 T item = Activator.CreateInstance<T>(); 108 109 if (this.dsResult.Tables.Contains(tableName)) 110 { 111 DataRow dr = this.dsResult.Tables[tableName].Rows[0]; 112 113 List<Parameter> listParameter = GetProperties<T>(item, exceptArray); 114 115 foreach (Parameter p in listParameter) 116 { 117 foreach (DataColumn dc in this.dsResult.Tables[tableName].Columns) 118 { 119 if (dc.ColumnName == p.Name) 120 { 121 Type type = item.GetType(); 122 123 MethodInfo method = type.GetMethod("SetAttributeValue"); 124 125 method.Invoke(item, new object[] { p.Name, dr[p.Name].ToString().Trim() }); 126 } 127 } 128 } 129 } 130 131 return item; 132 } 133 catch (Exception ex) 134 { 135 throw new Exception("写" + Activator.CreateInstance<T>() + "信息发生错误,错误原因:" + ex.Message); 136 } 137 } 138 139 /// <summary> 140 /// 141 /// </summary> 142 /// <typeparam name="T"></typeparam> 143 /// <param name="tableName"></param> 144 /// <returns></returns> 145 public List<T> WriteTObjectInfoList<T>(string tableName, string[] exceptArray) 146 { 147 try 148 { 149 if (this.Status == 0) 150 { 151 throw new Exception(this.Msg); 152 } 153 154 List<T> list = new List<T>(); 155 156 if (this.dsResult.Tables.Contains(tableName)) 157 { 158 foreach (DataRow dr in this.dsResult.Tables[tableName].Rows) 159 { 160 T item = WriteTObjectInfo<T>(tableName, dr, exceptArray); 161 162 list.Add(item); 163 } 164 } 165 166 return list; 167 } 168 catch (Exception ex) 169 { 170 throw new Exception("写" + Activator.CreateInstance<T>().ToString() + "信息发生错误,错误原因:" + ex.Message); 171 } 172 } 173 174 /// <summary> 175 /// 176 /// </summary> 177 /// <typeparam name="T"></typeparam> 178 /// <param name="tableName"></param> 179 /// <returns></returns> 180 public List<T> WriteTObjectInfoList<T>(string tableName) 181 { 182 return WriteTObjectInfoList<T>(tableName, new string[] { }); 183 } 184 185 #endregion