public DataSet ToDataSet(IDbCode code, CommandType type = CommandType.Text) { string name = DateTime.Now.ToString(); command.Add(name, new SqlCommand()); SqlCommand com = command[name]; com.Connection = conn; com.CommandText = code.ToString(); com.CommandType = type; setCommand(com, (List <SqlParameter>)code.Paras); DataSet dt = new DataSet(); try { using (SqlDataAdapter adapter = new SqlDataAdapter(com)) { adapter.Fill(dt); } } catch (Exception ex) { DoException(); throw ex; } finally { Dispose(name); } return(dt); }
public object ToResult(IDbCode code, CommandType type = CommandType.Text) { string name = DateTime.Now.ToString(); command.Add(name, new SqlCommand()); SqlCommand com = command[name]; com.Connection = conn; com.CommandText = code.ToString(); com.CommandType = type; setCommand(com, (List <SqlParameter>)code.Paras); object result = null; try { this.OpenConnection(); result = com.ExecuteScalar(); } catch (Exception ex) { DoException(); throw ex; } finally { this.Dispose(name); this.CloseConnection(); } return(result); }
public List <T> ToList <T>(IDbCode code, CommandType type = CommandType.Text) where T : class, new() { List <T> list = new List <T>(); string name = DateTime.Now.ToString(); command.Add(name, new SqlCommand()); SqlCommand com = command[name]; com.Connection = conn; com.CommandText = code.ToString(); com.CommandType = type; setCommand(com, (List <SqlParameter>)code.Paras); Type t = typeof(T); List <PropertyInfo> pros; if (pro.ContainsKey(t.Name)) { pros = pro[t.Name]; } else { pros = t.GetProperties().ToList(); pro.Add(t.Name, pros); } try { this.OpenConnection(); using (SqlDataReader reader = com.ExecuteReader()) { while (reader.Read()) { T model = new T(); pros.ForEach(o => { if (ReaderExists(reader, o.Name)) { o.SetValue(model, reader[o.Name], null); } }); list.Add(model); } } } catch (Exception ex) { throw ex; } finally { this.Dispose(name); this.CloseConnection(); } return(list); }
public int ExcuteResult(IDbCode code, CommandType type = CommandType.Text) { string name = DateTime.Now.ToString(); command.Add(name, new SqlCommand()); SqlCommand com = command[name]; com.Connection = conn; com.CommandText = code.ToString(); com.CommandType = type; setCommand(com, (List <SqlParameter>)code.Paras); int result = 0; try { this.OpenConnection(); if (tran != null) { com.Transaction = (SqlTransaction)tran; } result = com.ExecuteNonQuery(); } catch (Exception ex) { DoException(); throw ex; } finally { if (tran == null) { Dispose(name); } this.CloseConnection(); } return(result); }
public DbHelper ExcuteString(Func <IDbCode, IDbCode> Fun) { Code = Fun(this.instance.Code); return(this); }