首页 > 代码库 > 手机大数据DBHelper

手机大数据DBHelper

using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.Common;using System.Configuration;using System.Transactions;namespace BrightData.CellData.CityPlanning.DBUtility{    class DBHelper : IDB    {        String connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;        String providerName = ConfigurationManager.ConnectionStrings["connStr"].ProviderName;        IDbCommand IDB.CreateCommand()        {            return CreateDBCommand();        }        private IDbCommand CreateDBCommand()        {            return CreateDBCommand(null, null);        }        private IDbCommand CreateDBCommand(String[] names, Object[] values)        {            try            {                DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);                IDbConnection iconn = factory.CreateConnection();                iconn.ConnectionString = connString;                IDbCommand icmd = iconn.CreateCommand();                if (names != null && values != null)                {                    for (int i = 0; i < names.Length; i++)                    {                        IDbDataParameter iParameter = icmd.CreateParameter();                        iParameter.ParameterName = ":" + names[i];                        iParameter.Value = values[i];                        icmd.Parameters.Add(iParameter);                    }                }                icmd.Connection.Open();                return icmd;            }            catch (Exception ex)            {                System.IO.File.AppendAllText(@"D:\5.txt", ex.Message);                System.Diagnostics.Trace.WriteLine(ex.Message);                return null;            }        }        /// <summary>        /// 添加,删除,更新        /// </summary>        /// <param name="sql"></param>        /// <returns></returns>        int IDB.ExecuteNonQuery(string sql)        {            IDbCommand icmd = CreateDBCommand();            if (icmd != null)            {                try                {                    //using (TransactionScope sop = new TransactionScope())                    //{                    //    sop.Complete();                    //}                    icmd.CommandText = sql;                    return icmd.ExecuteNonQuery();                }                catch (Exception ex)                {                    System.Diagnostics.Trace.WriteLine(ex.Message);                    return -99;                }                finally                {                    icmd.Connection.Close();                }            }            return -99;        }        IDataReader IDB.ExecuteReader(string sql)        {            Console.WriteLine(sql);            IDbCommand icmd = CreateDBCommand();            if (icmd != null)            {                try                {                    icmd.CommandText = sql;                    return icmd.ExecuteReader(CommandBehavior.CloseConnection);                }                catch (Exception ex)                {                    System.IO.File.AppendAllText(@"D:\1.txt", ex.Message);                    System.Diagnostics.Trace.WriteLine(ex.Message);                    icmd.Connection.Close();                    return null;                }            }            return null;        }        object IDB.ExecuteScalar(string sql)        {            IDbCommand icmd = CreateDBCommand();            if (icmd != null)            {                try                {                    icmd.CommandText = sql;                    return icmd.ExecuteScalar();                }                catch (Exception ex)                {                    System.Diagnostics.Trace.WriteLine(ex.Message);                }                finally                {                    icmd.Connection.Close();                }            }            return null;        }        int IDB.ExecuteNonQuery(string sql, string[] names, object[] values)        {            IDbCommand icmd = CreateDBCommand(names, values);            if (icmd != null)            {                try                {                    icmd.CommandText = sql;                    return icmd.ExecuteNonQuery();                }                catch (Exception ex)                {                    System.Diagnostics.Trace.WriteLine(ex.Message);                    return -99;                }                finally                {                    icmd.Connection.Close();                }            }            return -99;        }        IDataReader IDB.ExecuteReader(string sql, string[] names, object[] values)        {            IDbCommand icmd = CreateDBCommand(names, values);            if (icmd != null)            {                try                {                    icmd.CommandText = sql;                    return icmd.ExecuteReader(CommandBehavior.CloseConnection);                }                catch (Exception ex)                {                    System.Diagnostics.Trace.WriteLine(ex.Message);                    icmd.Connection.Close();                    return null;                }            }            return null;        }                void IDB.setConnString(string conn)        {            connString = conn;        }        void IDB.setConnDrive(string Drive)        {            providerName = Drive;        }    }}

 IDB

using System;using System.Collections.Generic;using System.Text;using System.Data;namespace BrightData.CellData.CityPlanning.DBUtility{    public interface IDB    {        int ExecuteNonQuery(String sql);        int ExecuteNonQuery(String sql, String[] names, Object[] values);        IDataReader ExecuteReader(String sql);        IDataReader ExecuteReader(String sql, String[] names, Object[] values);        object ExecuteScalar(String sql);        IDbCommand CreateCommand();        void setConnString(String conn);        void setConnDrive(String Drive);    }}
IDB Code

 

手机大数据DBHelper