Exemplo n.º 1
0
        /// <summary>
        /// 执行SQL语句,获得查询结果表
        /// </summary>
        /// <param name="pDbConn"></param>
        /// <param name="pTrans"></param>
        /// <param name="sSql"></param>
        /// <returns></returns>
        public DataTable GetDataTable(string sSql, OracleParameter[] opc)
        {
            DataTable pDataTable = new DataTable();

            try
            {
                pCommand.CommandText = sSql;
                pCommand.Parameters.Clear();
                if (opc != null)
                {
                    for (int i = 0; i < opc.Length; i++)
                    {
                        pCommand.Parameters.Add(opc[i]);
                    }
                }
                OracleDataAdapter pDbAdapter = new OracleDataAdapter(pCommand);

                pDbAdapter.Fill(pDataTable);
                return(pDataTable);
            }
            catch (Exception ep)
            {
                LogBN.WriteLog(typeof(DbAPI), "GetDataTable 程序段的异常" + ep + sSql);
                //if (pTrans != null)
                //{
                //    pTrans.Rollback();
                //}
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 执行存储过程,获得查询结果表
        /// </summary>
        /// <param name="sSql">存储过程名称</param>
        /// <param name="opc">参数数组</param>
        /// <returns></returns>
        public DataTable GetDataTableFromProcedure(string sSql, OracleParameter[] opc)
        {
            var pDataTable = new DataTable();

            try
            {
                pCommand.CommandType = CommandType.StoredProcedure;
                pCommand.CommandText = sSql;
                pCommand.Parameters.Clear();
                if (opc != null)
                {
                    foreach (var t in opc)
                    {
                        pCommand.Parameters.Add(t);
                    }
                }
                var pDbAdapter = new OracleDataAdapter(pCommand);

                pDbAdapter.Fill(pDataTable);
                return(pDataTable);
            }
            catch (Exception ep)
            {
                LogBN.WriteLog(typeof(DbAPI), "GetDataTableFromProcedure 程序段的异常" + ep + sSql);
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="otList"></param>
        public void RunOracleTransaction(List <Entity.OracleTransaction> otList)
        {
            if (otList == null || otList.Count <= 0)
            {
                return;
            }
            using (OracleConnection connection = new OracleConnection(PrepareConnectionString("")))
            {
                connection.Open();

                OracleCommand command = connection.CreateCommand();

                var transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

                command.Transaction = transaction;

                try
                {
                    foreach (var ot in otList)
                    {
                        command.CommandText = ot.SqlString;
                        command.Parameters.Clear();
                        if (ot.Parameters == null || ot.Parameters.Count <= 0)
                        {
                            return;
                        }
                        foreach (var parameter in ot.Parameters)
                        {
                            command.Parameters.Add(new OracleParameter(parameter.Key, parameter.Value));
                        }
                        command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    LogBN.WriteLog(typeof(DbAPI), "执行事务RunOracleTransaction 程序段的异常" + ex.Message);
                    transaction.Rollback();
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// 带参数
 /// </summary>
 /// <param name="pDbConn"></param>
 /// <param name="sSql"></param>
 /// <param name="pParams"></param>
 /// <returns></returns>
 public object ExecuteOracleScalar(string sSql, OracleParameter[] opc)
 {
     try
     {
         pCommand.CommandText = sSql;
         pCommand.Parameters.Clear();
         if (opc != null)
         {
             for (int i = 0; i < opc.Length; i++)
             {
                 pCommand.Parameters.Add(opc[i]);
             }
         }
         return(pCommand.ExecuteOracleScalar());
     }
     catch (Exception ep)
     {
         LogBN.WriteLog(typeof(DbAPI), "ExecuteOracleScalar 程序段的异常" + ep + sSql);
         return(-1);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 执行存储过程
 /// </summary>
 /// <param name="sSql">存储过程名称</param>
 /// <param name="opc">参数数组</param>
 /// <returns></returns>
 public int ExecuteNonQueryByProcedure(string sSql, OracleParameter[] opc)
 {
     try
     {
         pCommand.CommandType = CommandType.StoredProcedure;
         pCommand.CommandText = sSql;
         pCommand.Parameters.Clear();
         if (opc == null)
         {
             return(pCommand.ExecuteNonQuery());
         }
         foreach (var t in opc)
         {
             pCommand.Parameters.Add(t);
         }
         return(pCommand.ExecuteNonQuery());
     }
     catch (Exception ep)
     {
         LogBN.WriteLog(typeof(DbAPI), "ExecuteNonQueryByProcedure 程序段的异常" + ep + sSql);
         return(-1);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// DataTable导出到Excel的MemoryStream
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        public static MemoryStream Export(DataTable dtSource, string strHeaderText)
        {
            try
            {
                var invisibleColumnCount = dtSource.Columns.Cast <DataColumn>().Count(item => item.Caption == "invisible");
                for (int i = 0; i < invisibleColumnCount; i++)
                {
                    dtSource.Columns.RemoveAt(0);
                }

                HSSFWorkbook workbook = new HSSFWorkbook();
                var          sheet    = workbook.CreateSheet();

                #region 右击文件 属性信息
                {
                    DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                    dsi.Company = "NPOI";
                    workbook.DocumentSummaryInformation = dsi;

                    SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                    si.Author                   = "Robin van Persie"; //填加xls文件作者信息
                    si.ApplicationName          = "for downloading."; //填加xls文件创建程序信息
                    si.LastAuthor               = "RvP";              //填加xls文件最后保存者信息
                    si.Comments                 = "RvP";              //填加xls文件作者信息
                    si.Title                    = "Title";            //填加xls文件标题信息
                    si.Subject                  = "Subject";          //填加文件主题信息
                    si.CreateDateTime           = DateTime.Now;
                    workbook.SummaryInformation = si;
                }
                #endregion

                var dateStyle = workbook.CreateCellStyle();
                var format    = workbook.CreateDataFormat();
                dateStyle.DataFormat = format.GetFormat("yyyy-MM-dd HH:mm:ss");

                //取得列宽
                int[] arrColWidth = new int[dtSource.Columns.Count];
                foreach (DataColumn item in dtSource.Columns)
                {
                    arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName).Length;
                }
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    for (int j = 0; j < dtSource.Columns.Count; j++)
                    {
                        int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
                        if (intTemp > arrColWidth[j])
                        {
                            arrColWidth[j] = intTemp;
                        }
                    }
                }
                int rowIndex = 0;
                foreach (DataRow row in dtSource.Rows)
                {
                    #region 新建表,填充表头,填充列头,样式
                    if (rowIndex == 65535 || rowIndex == 0)
                    {
                        if (rowIndex != 0)
                        {
                            sheet = workbook.CreateSheet();
                        }

                        #region 表头及样式
                        {
                            var headerRow = sheet.CreateRow(0);
                            headerRow.HeightInPoints = 25;
                            headerRow.CreateCell(0).SetCellValue(strHeaderText);

                            var headStyle = workbook.CreateCellStyle();
                            headStyle.Alignment = HorizontalAlignment.Center;
                            var font = workbook.CreateFont();
                            font.FontHeightInPoints = 20;
                            font.Boldweight         = 700;
                            headStyle.SetFont(font);
                            headerRow.GetCell(0).CellStyle = headStyle;
                            sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1));
                        }
                        #endregion


                        #region 列头及样式
                        {
                            var headerRow = sheet.CreateRow(1);
                            var headStyle = workbook.CreateCellStyle();
                            headStyle.Alignment = HorizontalAlignment.Center;
                            var font = workbook.CreateFont();
                            font.FontHeightInPoints = 10;
                            font.Boldweight         = 700;
                            headStyle.SetFont(font);
                            foreach (DataColumn column in dtSource.Columns)
                            {
                                headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                                headerRow.GetCell(column.Ordinal).CellStyle = headStyle;

                                //设置列宽
                                sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
                            }
                        }
                        #endregion

                        rowIndex = 2;
                    }
                    #endregion


                    #region 填充内容
                    var dataRow = sheet.CreateRow(rowIndex);
                    foreach (DataColumn column in dtSource.Columns)
                    {
                        var newCell = dataRow.CreateCell(column.Ordinal);

                        string drValue = row[column].ToString();

                        switch (column.DataType.ToString())
                        {
                        case "System.String":    //字符串类型
                            newCell.SetCellValue(drValue);
                            break;

                        case "System.DateTime":    //日期类型
                            DateTime dateV;
                            DateTime.TryParse(drValue, out dateV);
                            newCell.SetCellValue(dateV);

                            newCell.CellStyle = dateStyle;    //格式化显示
                            break;

                        case "System.Boolean":    //布尔型
                            bool boolV;
                            bool.TryParse(drValue, out boolV);
                            newCell.SetCellValue(boolV);
                            break;

                        case "System.Int16":    //整型
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Byte":
                            int intV;
                            int.TryParse(drValue, out intV);
                            newCell.SetCellValue(intV);
                            break;

                        case "System.Decimal":    //浮点型
                        case "System.Double":
                            double doubV;
                            double.TryParse(drValue, out doubV);
                            newCell.SetCellValue(doubV);
                            break;

                        case "System.DBNull":    //空值处理
                            newCell.SetCellValue("");
                            break;

                        default:
                            newCell.SetCellValue("");
                            break;
                        }
                    }
                    #endregion

                    rowIndex++;
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    ms.Flush();
                    ms.Position = 0;

                    workbook.Close();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
                    return(ms);
                }
            }
            catch (Exception ex)
            {
                LogBN.WriteLog(typeof(NpoiHelper), "导出Excel的方法MemoryStream Export 程序段的异常" + ex);
                return(null);
            }
        }