码迷,mamicode.com
首页 > 移动开发 > 详细

手机大数据DBHelper

时间:2014-12-09 17:14:46      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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

bubuko.com,布布扣
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

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/xiaoshi212/p/4153461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!