首页 > 代码库 > 手机大数据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); }}
手机大数据DBHelper
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。