예제 #1
0
        public List <SimpDataEntery> ExecuteSimpData(DbCommand cmd)
        {
            var tran = MyTransaction.BeginTransaction(cmd);
            List <SimpDataEntery> simpDbEnterys = new List <SimpDataEntery>();
            string _fieldType = string.Empty;

            try
            {
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    do
                    {
                        var 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);
                        }
                        var 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(CultureInfo.InvariantCulture);
                                }
                                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);
        }
예제 #2
0
        //返回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);
        }
예제 #3
0
        //返回DataTable对象
        public DataTable ExecuteDataTable(DbCommand cmd)
        {
            DbProviderFactory dbfactory     = DbProviderFactories.GetFactory(DbProviderName);
            DbDataAdapter     dbDataAdapter = dbfactory.CreateDataAdapter();
            DataTable         dataTable     = null;
            MyTransaction     _tran         = MyTransaction.BeginTransaction(cmd);

            dbDataAdapter.SelectCommand = cmd;
            try
            {
                dataTable = new DataTable();
                dbDataAdapter.Fill(dataTable);
                _tran.Commit();
            }
            catch (Exception e)
            {
                _tran.Rollback();
                dataTable = CreateErrorTable(e.ToString());
            }
            return(dataTable);
        }
예제 #4
0
        //返回DataSet对象
        public DataSet ExecuteDataSet(DbCommand cmd)
        {
            DbProviderFactory dbfactory     = DbProviderFactories.GetFactory(DbProviderName);
            DbDataAdapter     dbDataAdapter = dbfactory.CreateDataAdapter();
            DataSet           ds            = new DataSet();
            MyTransaction     tran          = MyTransaction.BeginTransaction(cmd);

            if (dbDataAdapter != null)
            {
                dbDataAdapter.SelectCommand = cmd;
                try
                {
                    dbDataAdapter.Fill(ds);
                    tran.Commit();
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    ds.Tables.Add(CreateErrorTable(e.ToString()));
                }
            }
            return(ds);
        }
예제 #5
0
        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));
        }