| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:MSSQLServerDBConnection.cs
- * 2.功能描述:SQL数据库不带有事物的操作类
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 张国印 2014/09/01 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace Dongke.IBOSS.PRD.Basics.DataAccess
- {
- /// <summary>
- /// SQL数据库不带有事物的操作类
- /// </summary>
- public class MSSQLServerDBConnection : IDBConnection
- {
- private static ArrayList m_Pool = new ArrayList();
- private SqlConnection m_SqlConnection;
- private ConnectionState m_ConState = ConnectionState.Closed;
- private string m_Constr;
- /// <summary>
- /// 忽略大小写
- /// </summary>
- public bool IgnoreCase
- {
- get;
- set;
- }
- /// <summary>
- /// SQL执行限定超时
- /// </summary>
- public bool IsCommandTimeout
- {
- get;
- set;
- }
- public MSSQLServerDBConnection(string m_strConn)
- {
- try
- {
- m_Constr = m_strConn;
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source;
- throw new Exception(strError, ex);
- }
- }
- #region IDBConnection 成员
- public int PoolSize
- {
- get
- {
- return m_Pool.Count;
- }
- }
- public ConnectionState ConnState
- {
- get
- {
- return m_ConState;
- }
- }
- public IDbConnection DbConnection
- {
- get
- {
- return m_SqlConnection;
- }
- set
- {
- m_SqlConnection = (value as SqlConnection);
- }
- }
- public string ConnStr
- {
- get { return m_Constr; }
- }
- public ConnectionState Open()
- {
- try
- {
- m_SqlConnection = new SqlConnection(m_Constr);
- m_SqlConnection.Open();
- m_ConState = m_SqlConnection.State;
- //m_Pool.Add(m_SqlConnection);
- }
- catch (Exception ex)
- {
- m_ConState = ConnectionState.Closed;
- string strError = ex.Message + ex.Source;
- throw new Exception(strError, ex);
- }
- return m_ConState;
- }
- public ConnectionState Close()
- {
- try
- {
- if (m_SqlConnection.State == ConnectionState.Open)
- {
- m_SqlConnection.Close();
- m_ConState = m_SqlConnection.State;
- }
- //if (m_Pool.Contains(m_SqlConnection))
- //{
- // m_Pool.Remove(m_SqlConnection);
- //}
- }
- catch (Exception ex)
- {
- m_ConState = ConnectionState.Closed;
- string strError = ex.Message + ex.Source;
- throw new Exception(strError, ex);
- }
- return m_ConState;
- }
- public DataSet GetSqlResultToDs(string p_strCommand, IDataParameter[] p_Parameter)
- {
- DataSet myDs = new DataSet();
- if (m_SqlConnection.State != ConnectionState.Open)
- Open();
- try
- {
- SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
- }
- dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- dataAdapter.Fill(myDs);
- }
- catch (Exception ex)
- {
- ex.Source = ex.Source + " 对应的SQL语句为:" + p_strCommand;
- string strError = ex.Message + ex.Source;
- throw new Exception(strError, ex);
- }
- return myDs;
- }
- public DataSet GetSqlResultToDs(string p_strCommand, string p_strName, IDataParameter[] p_Parameter)
- {
- DataSet myDs = new DataSet();
- if (m_SqlConnection.State != ConnectionState.Open)
- Open();
- try
- {
- SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
- }
- dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- dataAdapter.Fill(myDs, p_strName);
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
- throw new Exception(strError, ex);
- }
- return myDs;
- }
- public DataTable GetSqlResultToDt(string p_strCommand, IDataParameter[] p_Parameter)
- {
- //初始化结果集(DT)
- DataTable myDt = new DataTable();
- if (m_SqlConnection.State != ConnectionState.Open)
- Open();
- try
- {
- //dataAdapte实例化
- SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
- }
- dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- //填充数据集(DT)
- dataAdapter.Fill(myDt);
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
- throw new Exception(strError, ex);
- }
- //返回数据集
- return myDt;
- }
- public string GetSqlResultToStr(string p_strCommand, IDataParameter[] p_Parameter)
- {
- string strResult = "";
- object retValue = null;
- if (m_ConState != ConnectionState.Open)
- Open();
- try
- {
- SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- oraCmd.Parameters.AddRange(p_Parameter);
- }
- oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- retValue = oraCmd.ExecuteScalar();
- if (retValue != null)
- strResult = retValue.ToString();
- else
- strResult = String.Empty;
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
- throw new Exception(strError, ex);
- }
- return strResult;
- }
- public object GetSqlResultToObj(string p_strCommand, IDataParameter[] p_Parameter)
- {
- object retValue = null;
- if (m_ConState != ConnectionState.Open)
- Open();
- try
- {
- SqlCommand oraCmd = new SqlCommand(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- oraCmd.Parameters.AddRange(p_Parameter);
- }
- oraCmd.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- retValue = oraCmd.ExecuteScalar();
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
- throw new Exception(strError, ex);
- }
- return retValue;
- }
- public byte[] GetSqlResultToBt(string p_strCommand, IDataParameter[] p_Parameter)
- {
- int bufferSize = 100;
- byte[] bytData = new byte[bufferSize];
- byte[] bytResult = null;
- int retval;
- int startIndex = 0;
- if (m_SqlConnection.State != ConnectionState.Open)
- Open();
- try
- {
- //创建内存流,用于将byte[]数组写入到流中
- MemoryStream ms = new MemoryStream();
- SqlCommand sqlCommand = new SqlCommand(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- sqlCommand.Parameters.AddRange(p_Parameter);
- }
- SqlDataReader myReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
- while (myReader.Read())
- {
- //读取blob列数据到bytData中
- retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
- //将bytData写入到流中
- ms.Write(bytData, 0, retval);
- while (retval == bufferSize)
- {
- // 重新设置读取bytData数据后缓冲读取位置的开始索引
- startIndex += bufferSize;
- retval = Convert.ToInt32(myReader.GetBytes(0, startIndex, bytData, 0, bufferSize));
- ms.Write(bytData, 0, retval);
- }
- }
- bytResult = ms.ToArray();
- ms.Close();
- myReader.Close();
- }
- catch (Exception ex)
- {
- string strError = ex.Message + ex.Source + " 对应的SQL语句为:" + p_strCommand;
- throw new Exception(strError, ex);
- }
- return bytResult;
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="p_strCommand">存储过程名称</param>
- /// <param name="p_Parameter">IDataParameter类型 参数集合</param>
- /// <returns></returns>
- public DataSet ExecStoredProcedure(string p_strCommand, IDataParameter[] p_Parameter)
- {
- DataSet myDs = new DataSet();
- if (m_SqlConnection.State != ConnectionState.Open)
- Open();
- try
- {
- SqlDataAdapter dataAdapter = new SqlDataAdapter(p_strCommand, m_SqlConnection);
- if (p_Parameter != null)
- {
- dataAdapter.SelectCommand.Parameters.AddRange(p_Parameter);
- }
- dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
- dataAdapter.SelectCommand.CommandTimeout = 120; //2012-2-21 张国印 增加 用于查询超时
- dataAdapter.Fill(myDs);
- }
- catch (Exception ex)
- {
- ex.Source = ex.Source + " 对应的SQL语句为:" + p_strCommand;
- string strError = ex.Message + ex.Source;
- throw new Exception(strError, ex);
- }
- return myDs;
- }
- #endregion
- }
- }
|