数据库连接示例

ContractedBlock.gif ExpandedBlockStart.gif Code
 public void CreateDataSource(DatasourceProperties dataSource, string policyInfo)
        {
            
try
            {
                
using (SqlConnection c = new SqlConnection(conn))
                {
                    
if (c.State != ConnectionState.Open)
                    {
                        c.Open();
                    }
                    
using (SqlTransaction tran = c.BeginTransaction())
                    {
                        
try
                        {
                            
int id = SqlHelper.ExecuteNonQueryTypedParamsWithReturnValue(tran, "CreateDatasource",
                                dataSource.Server,
                                dataSource.ConnectionType.ToString(),
                                dataSource.AuthorizationMode.ToString(),
                                dataSource.Catalog,
                                dataSource.Name,
                                dataSource.Description,
                                dataSource.User,
                                DataProtector.EncryptString(dataSource.Password),
                                dataSource.TimeOut,
                                dataSource.DataSourceInfo,
                                dataSource.GetPropertiesString(),
                                HttpContext.Current.User.Identity.Name
                                );
                            
switch (id)
                            {
                                
case -1:
                                    
throw new Exception(SR.Exception_UserNotExist);
                                
case -2:
                                    
throw new Exception(SR.Exception_DatasourceAlreadyExist);
                                
case -3:
                                    
throw new Exception(SR.Exception_CreateDatasourceError);
                                
default:
                                    
break;
                            }

                            
if (policyInfo.Length > 0)
                                SqlHelper.ExecuteNonQueryTypedParamsWithReturnValue(tran, 
"SetPolicy", id, policyInfo.Replace("-1", id.ToString()));
                            tran.Commit();
                        }
                        
catch (Exception error)
                        {
                            tran.Rollback();
                            
throw new Exception(error.Message);
                        }
                    }
                }
            }
            
catch (Exception err)
            {
                
throw err;
            }
        }

 


    public class PersistenceService
    {
        #region Singleton
        private static readonly PersistenceService _instance = new PersistenceService();
        public static PersistenceService Instance
        {
            get { return _instance; }
        }
        #endregion

        private ConnectionStringSettings _connectionStringSettings = null;
        private SqlConnection _connection = null;

        private PersistenceService()
        {
            this._connectionStringSettings = ConfigurationManager.ConnectionStrings["PersistenceConnectionString"];

            if (null == this._connectionStringSettings)
            {
                this._connectionStringSettings = WebConfigurationManager.ConnectionStrings["PersistenceConnectionString"];
            }

            if (null == this._connectionStringSettings)
            {
                throw new SystemException("Can not locate the application configuration file.");
            }

            if (this.Connection == null)
            {
                throw new SystemException("Can not create database connection.");
            }
        }

        public SqlConnection Connection
        {
            get
            {
                if (this._connection == null)
                {
                    try
                    {
                        this._connection = new SqlConnection(this._connectionStringSettings.ConnectionString);
                    }
                    catch
                    {
                        this._connection = null;
                    }
                }
                return this._connection;
            }
        }
        private OleDbConnection _oleDbConn;
        private string _oleDbConnStr;
        public string OleDbConnStr
        {
            get { return _oleDbConnStr; }
            set { _oleDbConnStr = value; }
        }
        public OleDbConnection OleDbConn
        {
            get
            {
                if (this._oleDbConn == null)
                {
                    try
                    {
                        this._oleDbConn = new OleDbConnection(this._oleDbConnStr);
                    }
                    catch
                    {
                        this._oleDbConn = null;
                    }
                }
                return this._oleDbConn;
            }
        }

        public void OpenOleDbConnection()
        {
            if (this.OleDbConn == null)
            {
                throw new SystemException("不能创建数据库连接.");
            }
            CloseOleDbConnection();
            try
            {
                this.OleDbConn.Open();
            }
            catch (Exception ex)
            {
                this._oleDbConn = null;
                throw new Exception(ex.Message);
            }
        }

        public void CloseOleDbConnection()
        {
            if (this.OleDbConn != null)
            {
                if (this.OleDbConn.State != ConnectionState.Closed)
                {
                    this.OleDbConn.Close();
                }
            }
        }
        public void OpenConnection()
        {
            if (this.Connection == null)
            {
                throw new SystemException("不能创建数据库连接.");
            }

            CloseConnection();

            try
            {
                this.Connection.Open();
            }
            catch (Exception ex)
            {
                this._connection = null;
                throw new SystemException(ex.Message);
            }
        }

        public void CloseConnection()
        {
            if (this.Connection != null)
            {
                if (this.Connection.State != ConnectionState.Closed)
                {
                    this.Connection.Close();
                }
            }
        }
        public bool ExecuteOleDbSql(string sqlQuery)
        {
            using (OleDbCommand command = this.OleDbConn.CreateCommand())
            {
                OpenOleDbConnection();
                OleDbTransaction tran = this.OleDbConn.BeginTransaction();
                command.Connection = this.OleDbConn;
                command.CommandType = CommandType.Text;
                command.CommandText = sqlQuery;
                command.Transaction = tran;
                try
                {
                    command.ExecuteNonQuery();
                    tran.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    try
                    {
                        tran.Rollback();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    throw new Exception(e.Message);
                }
                finally
                {
                    this.CloseOleDbConnection();
                }
            }

        }
        public bool ExecuteSql(string sqlQuery)
        {
            using (SqlCommand command = this.Connection.CreateCommand())
            {
                OpenConnection();

                SqlTransaction tran = this.Connection.BeginTransaction();

                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = sqlQuery;
                command.Transaction = tran;

                try
                {
                    command.ExecuteNonQuery();
                    tran.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    try
                    {
                        tran.Rollback();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    throw new Exception(e.Message);
                }
                finally
                {
                    this.CloseConnection();
                }
            }
        }
        public DataTable ExecuteOleDbQuery(string query)
        {
            DataTable dtItem = null;
            using (OleDbCommand command = OleDbConn.CreateCommand())
            {
                command.Connection = this.OleDbConn;
                command.CommandType = CommandType.Text;
                command.CommandText = query;

                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = command;
                dtItem = new DataTable();
                try
                {
                    this.OpenOleDbConnection();
                    adapter.Fill(dtItem);
                }
                catch (Exception ex)
                {
                    dtItem = null;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    this.CloseOleDbConnection();
                }
            }
            return dtItem;
        }
        public DataTable ExecuteQuery(string query)
        {
           
            DataTable dtItem = null;
            using (SqlCommand command = this.Connection.CreateCommand())
            {
                command.Connection = this.Connection;
                command.CommandType = CommandType.Text;
                command.CommandText = query;

                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = command;

                dtItem = new DataTable();

                try
                {
                    this.OpenConnection();

                    adapter.Fill(dtItem);
                }
                catch(Exception ex)
                {
                    dtItem = null;
                    throw new Exception(ex.Message);
                }
                finally
                {
                    this.CloseConnection();
                }
            }
            return dtItem;
        }
    }

 

 

转载于:https://www.cnblogs.com/xl696128/archive/2009/03/19/1416810.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值