当前位置: 代码迷 >> ASP.NET >> 范例dbhelper类要放在哪里写
  详细解决方案

范例dbhelper类要放在哪里写

热度:2663   发布时间:2013-02-25 00:00:00.0
实例dbhelper类要放在哪里写
新手,接触三层,从别人那里拷了一个dbhelper现成的类,使用的时候,是不是这样写  
Database db=new Database createtable("sqlconn")可是这个要放在三层里面的哪里?是放在dal里面的任何一个 类里都要写个这个?把dbherler的代码贴出来

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace DAL
{
  /// <summary>
  /// 用来创建DataBase对象的静态类
  /// </summary>
  public static class DBHelper
  {
  public static DataBase CreateData(string DataNameInConfigfile)
  {
  string pn = ConfigurationManager.ConnectionStrings[DataNameInConfigfile].ProviderName; //读取配置文件
  string cs = ConfigurationManager.ConnectionStrings[DataNameInConfigfile].ConnectionString;

  //下面判断数据库类型并创建相应的对象

  if (pn.ToUpper().Contains("OLEDB"))
  {
  OleDbDataAdapter oledbda = new OleDbDataAdapter();
  oledbda.SelectCommand = new OleDbCommand();
  oledbda.SelectCommand.Connection = new OleDbConnection(cs);
  return new DataBase(oledbda);
  }
  if (pn.ToUpper().Contains("SQL"))
  {
  SqlDataAdapter sqlda = new SqlDataAdapter();
  sqlda.SelectCommand = new SqlCommand();
  sqlda.SelectCommand.Connection = new SqlConnection(cs);
  return new DataBase(sqlda);
  }
  if (pn.ToUpper().Contains("ODBC"))
  {
  OdbcDataAdapter odbcda = new OdbcDataAdapter();
  odbcda.SelectCommand = new OdbcCommand();
  odbcda.SelectCommand.Connection = new OdbcConnection(cs);
  return new DataBase(odbcda);
  }
  return null;
  }

  public static DataBase CreateData(string ConnectionString, string ProviderName)
  {
  //以下判断数据库类型并创建相应的对象
  if (ProviderName.ToUpper().Contains("OLEDB"))
  {
  OleDbDataAdapter oledbda = new OleDbDataAdapter();
  oledbda.SelectCommand = new OleDbCommand();
  oledbda.SelectCommand.Connection = new OleDbConnection(ConnectionString);
  return new DataBase(oledbda);
  }
  if (ProviderName.ToUpper().Contains("SQL"))
  {
  SqlDataAdapter sqlda = new SqlDataAdapter();
  sqlda.SelectCommand = new SqlCommand();
  sqlda.SelectCommand.Connection = new SqlConnection(ConnectionString);
  return new DataBase(sqlda);
  }
  if (ProviderName.ToUpper().Contains("ODBC"))
  {
  OdbcDataAdapter odbcda = new OdbcDataAdapter();
  odbcda.SelectCommand = new OdbcCommand();
  odbcda.SelectCommand.Connection = new OdbcConnection(ConnectionString);
  return new DataBase(odbcda);
  }
  return null;
  }
  }



  /// <summary>
  /// 执行主要操作的类
  /// </summary>
  public class DataBase
  {
  private DbDataAdapter mDataAdapter; //指向传入的DbDataAdapter的引用