public List <SimpDataEntery> ExecuteSimpData(DbCommand cmd) { MyTransaction _tran = MyTransaction.BeginTransaction(cmd); List <SimpDataEntery> _simpDbEnterys = new List <SimpDataEntery>(); SimpDataColInf[] _simpCols; List <object[]> _simpRows; string _fieldType = string.Empty; try { using (DbDataReader _reader = cmd.ExecuteReader()) { do { _simpCols = new SimpDataColInf[_reader.FieldCount]; for (int _i = 0, _iCnt = _reader.FieldCount; _i < _iCnt; _i++) { _simpCols[_i].name = _reader.GetName(_i); _simpCols[_i].type = (DotNetType)Enum.Parse(typeof(DotNetType), _reader.GetFieldType(_i).Name); } _simpRows = new List <object[]>(); while (_reader.Read()) { object[] _objs = new object[_reader.FieldCount]; for (int _i = 0, _iCnt = _reader.FieldCount; _i < _iCnt; _i++) { if ((_objs[_i] = _reader.GetValue(_i)).Equals(DBNull.Value)) { _objs[_i] = null; } if (_objs[_i] is DateTime) { _objs[_i] = ((DateTime)_objs[_i]).ToString(); } if (_objs[_i] is Boolean) { _objs[_i] = _objs[_i].Equals(true) ? 1 : 0; } } //_reader.GetValues(_objs); _simpRows.Add(_objs); } _simpDbEnterys.Add(new SimpDataEntery() { Cols = _simpCols, Rows = _simpRows, TVal = System.DateTime.Now.Ticks }); } while (_reader.NextResult()); _reader.Close(); _tran.Commit(); } } catch (Exception e) { _tran.Rollback(); _simpDbEnterys.Add(CreateErrorSimpDataEntery(e.ToString())); } return(_simpDbEnterys); }
//返回SQL影响的行数 public object ExecuteScalar(DbCommand cmd) { MyTransaction _tran = MyTransaction.BeginTransaction(cmd); object ret = null; try { ret = cmd.ExecuteScalar(); _tran.Commit(); } catch (Exception e) { _tran.Rollback(); return(e.ToString()); } return(ret); }
//返回DataSet对象 public DataSet ExecuteDataSet(DbCommand cmd) { DbProviderFactory dbfactory = DbProviderFactories.GetFactory(SimpDataDBHelper.dbProviderName); DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter(); DataSet ds = new DataSet(); MyTransaction _tran = MyTransaction.BeginTransaction(cmd); dbDataAdapter.SelectCommand = cmd; try { dbDataAdapter.Fill(ds); _tran.Commit(); } catch (Exception e) { _tran.Rollback(); ds.Tables.Add(CreateErrorTable(e.ToString())); } return(ds); }
internal string ExecuteJsonScalar(DbCommand cmd) { var dbfactory = DbProviderFactories.GetFactory(dbProviderName); var dbDataAdapter = dbfactory.CreateDataAdapter(); var ds = new DataSet(); var tran = MyTransaction.BeginTransaction(cmd); if (dbDataAdapter == null) { return("{\"ERROR\":\"\"}"); } dbDataAdapter.SelectCommand = cmd; try { dbDataAdapter.Fill(ds); tran.Commit(); } catch (Exception e) { tran.Rollback(); ds.Tables.Add(CreateErrorTable(e.ToString())); } return(JsonHelper.DataSetToJson(ds)); }