/// <summary> /// Runs the query. Usually used for udpate commands /// </summary> internal void ExecuteNonQuery() { try { //verbose output //VerboseOutput(); Open(); // run the query cmd.ExecuteNonQuery(); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch (Exception) { throw; } finally { if (cmd.Transaction == null) { Close(); } } }
/// <summary> /// Fills a dataReader. Use this for max performance /// </summary> /// <returns>Returns an IDataReader</returns> private IDataReader FillIDataReader() { try { //verbose output //VerboseOutput(); this.Open(); dr = cmd.ExecuteReader(); return(dr); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch (Exception) { throw; } }
/// <summary> /// Same as above but doesn't take a datatable name /// Use this if there is more than one data table come back from query /// </summary> /// <returns>DataSet with results from query</returns> public DataSet Fill() { try { // set up the dataset ds = new DataSet(); // set up the adapter da = new SqlDataAdapter(); da.SelectCommand = cmd; //verbose output //VerboseOutput(); // run the fill query da.Fill(ds); return(ds); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch (Exception) { throw; } }
/// <summary> /// Populates a single object of the given Type, with propeties set based on how they match up to the fields returned in the first row of the recordset. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> internal T FillObject <T>() { try { SqlDataReader rd = FillSqlReader(); T t = default(T); if (rd.Read()) { t = ConvertReaderToObject <T>(rd);; } rd.Close(); return(t); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch (Exception) { throw; } finally { Close(); } }
/// <summary> /// Returns a list of objects of the given Type, with propeties set based on how they match up to the fields returned in the recordset. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> internal List <T> FillList <T>() { try { SqlDataReader rd = FillSqlReader(); List <T> lst = new List <T>(); while (rd.Read()) { lst.Add(ConvertReaderToObject <T>(rd)); } rd.Close(); return(lst); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch (Exception) { throw; } finally { Close(); } }
public IEnumerable <T> Query <T>(string query, object[] param = null, CommandType ct = CommandType.Text) where T : new() { IEnumerable <T> obj = null; CinchMapping mappings = Mapper.MapQuery(query, param); //set query from mapper cmd.CommandText = query; cmd.CommandType = ct; //do we have params? if (mappings.SqlParams != null && mappings.SqlParams.Count > 0) { this.AddParameters(mappings.SqlParams); } try { //execute cmd and buffer reader SqlDataReader rd = FillSqlReader(); while (rd.Read()) { obj = ConvertReaderToEnumerable <T>(rd); } rd.Close(); return(obj); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch { throw; } finally { Close(); } }
/// <summary> /// Execute the command and return an object /// </summary> /// <returns>An Object</returns> internal Object ExecuteScalar() { Object rv = new Object(); try { //verbose output //VerboseOutput(); Open(); // run the query and return the value rv = cmd.ExecuteScalar(); } catch (SqlException sqlEx) { if (sqlEx.Number == 50000) { DBException dbx = new DBException(sqlEx.Message, sqlEx); throw dbx; } else { throw sqlEx; } } catch { throw; } finally { if (cmd.Transaction == null) { Close(); } } return(rv); }