c#数据库,连接类库---DbHelp.cs.pdf
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
Command cmd = connDb.create_command(); 73. cmd.Connection = conn; 74. cmd.CommandText = sql; 75. cmd.CommandType = ct; 76. if (sp != null) 77. { 78. foreach (IDataParameter para in sp) 79. { 80. cmd.Parameters.Add(para); 81. } 82. } 83.conn.Open(); 84.int result = cmd.ExecuteNonQuery(); 85.conn.Close(); 86.return result; 87.} 88.#endregion 89. #region 查询90./// <summary>91./// 查询92./// </summary>93./// <param name="sql">SQL语句</param>94./// <returns>IDataReader</returns>95.public static IDataReader query(string sql) 96. { 97.return query(sql, CommandType.Text, null, defaultPrv); 98. } 99. 100./// <summary>101./// 查询102./// </summary>103./// <param name="sql">SQL语句</param>104./// <param name="sp">参数</param>105./// <returns>IDataReader</returns>106.public static IDataReader query(string sql, IDataParameter[] sp) 107. { 108return query(sql, CommandType.Text, sp, defaultPrv); 109. } 110. 111./// <summary>112./// 查询113./// </summary>114./// <param name="sql">SQL语句</param>115./// <param name="ct">命令类型</param>116./// <param name="sp">参数</param>117./// <returns>IDataReader</returns>118.public static IDataReader query(string sql, CommandType ct, IDataParameter[] sp) 119. { 120return query(sql, ct, sp, defaultPrv); 121. } 122. 123./// <summary>124./// 查询125./// </summary>126./// <param name="sql">SQL语句</param>127./// <param name="ct">命令类型</param>128./// <param name="sp">参数</param>129./// <param name="connDb">数据驱动</param>130./// <returns>IDataReader</returns>131.public static IDataReader query(string sql, CommandType ct, IDataParameter[] sp, IDbProvider connDb) 132. { 133. using (IDbConnection conn = connDb.create_connection(connDb.get_conn)) 134. { 135.IDbCommand cmd = connDb.create_command(); 136. cmd.Connection = conn; 137. cmd.CommandText = sql; 138. cmd.CommandType = ct; 139. if (sp != null) 140. { 141. foreach (IDataParameter para in sp) 142. { 143. cmd.Parameters.Add(para); 144. } 145. } 146. conn.Open(); 147. return cmd.ExecuteReader(); 148. } 149. } 150. #endregion 151. #region 获取单个对象152./// <summary>153./// 获取单个对象154./// </summary>155./// <typeparam name="T">泛型类型</typeparam>156./// <param name="sql">SQL语句</param>157./// <returns>T</returns>158.public static T GetSingle<T>(string sql) where T : new() 159. { 160return GetSingle<T>(sql, CommandType.Text, null, defaultPrv); 161. } 162. 163./// <summary>164./// 获取单个对象165./// </summary>166./// <typeparam name="T">泛型类型</typeparam>167./// <param name="sql">SQL语句</param>168./// <param name="sp">参数</param>169./// <returns>T</returns>170.public static T GetSingle<T>(string sql, IDataParameter[] sp) where T : new() 171. { 172return GetSingle<T>(sql, CommandType.Text, sp, defaultPrv); 173. } 174. 175./// <summary>176./// 获取单个对象177./// </summary>178./// <typeparam name="T">泛型类型</typeparam>179./// <param name="sql">SQL语句</param>180./// <param name="ct">命令类型</param>181./// <param name="sp">参数</param>182./// <returns>T</returns>183.public static T GetSingle<T>(string sql, CommandType ct, IDataParameter[] sp) where T : new() 184. { 185return GetSingle<T>(sql, ct, sp, defaultPrv); 186. } 187. 188./// <summary>189./// 获取单个对象190./// </summary>191./// <typeparam name="T">泛型类型</typeparam>192./// <param name="sql">SQL语句</param>193./// <param name="ct">命令类型</param>194./// <param name="sp">参数</param>195./// <param name="connDb">数据驱动</param>196./// <returns>T</returns>197.public static T GetSingle<T>(string sql, CommandType ct, IDataParameter[] sp, IDbProvider connDb) where T : new() 198. { 199. using (IDbConnection conn = connDb.create_connection(connDb.get_conn)) 200. { 201.IDataReader dr = query(sql, ct, sp, connDb); 202. if (dr.Read()) 203. { 204. T obj = new T(); 205. Type type = obj.GetType(); 206. for (int i = 0; i < dr.FieldCount; i++) 207. { 208. string fielName = dr.GetName(i); 209. PropertyInfo proInfo = type.GetProperty(fielName); 210. if (proInfo != null) 211. { 212. proInfo.SetValue(obj, dr[i], null); 213. } 214. } 215. return obj; 216. } 217. dr.Close(); 218. return default(T); 219. } 220. } 221. #endregion 222. #region 获取列表223./// <summary>224./// 获取列表225./// </summary>226./// <typeparam name="T">泛型类型</typeparam>227./// <param name="sql">SQL语句</param>228./// <returns>List<T></returns>229.public static List<T> GetList<T>(string sql) where T : new() 230. { 231return GetList<T>(sql, CommandType.Text, null, defaultPrv); 232. } 233. 234./// <summary>235./// 获取列表236./// </summary>237./// <typeparam name="T">泛型类型</typeparam>238./// <param name="sql">SQL语句</param>239./// <param name="sp">参数</param>240./// <returns>List<T></returns>241.public static List<T> GetList<T>(string sql, IDataParameter[] sp) where T : new() 242. { 243return GetList<T>(sql, CommandType.Text, sp, defaultPrv); 244. } 245. 246./// <summary>247./// 获取列表248./// </summary>249./// <typeparam name="T">泛型类型</typeparam>250./// <param name="sql">SQL语句</param>251./// <param name="ct">命令类型</param>252./// <param name="sp">参数</param
剩余14页未读,继续阅读
- 粉丝: 10
- 资源: 15万+
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助