初学ADO.NET时写的数据库访问类[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

初学ADO.NET时写的数据库访问类

/*************************************************

OleDb

**************************************************/

using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace Db
{
 /// <summary>
 ///
 /// </summary>
 public class OleDb
 {
  public OleDb()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   m_oleDbCnn = null;
   //
  }

  /// <summary>
  /// Connecion连接 字符
  /// </summary>
  private string m_strCnn;// = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  /// <summary>
  /// OleDbConnection
  /// </summary>
  private OleDbConnection m_oleDbCnn =  null;//new OleDbConnection(m_strCnn);

  /// <summary>
  /// 打开Connection
  /// </summary>
  private void OpenCnn()
  {
   if (m_oleDbCnn.State==ConnectionState.Open)
   {
    m_oleDbCnn.Close();
   }
   m_oleDbCnn.Open();
  }

  /// <summary>
  /// //执行delete,update,insert等操作
  /// </summary>
  public int ExeNoQuery(string strSql)
  {
   int nRet = -1;
   m_strError = "";
   OleDbCommand oleDbCmd = new OleDbCommand(strSql,m_oleDbCnn);
   try
   {
    OpenCnn();
    nRet = oleDbCmd.ExecuteNonQuery();
   }
   catch (OleDbException ex)
   {
    nRet = -1;
    m_strError = ex.ToString();
   }
   finally
   {
    if (m_oleDbCnn.State==ConnectionState.Open)
    {
     oleDbCmd.Dispose();
     m_oleDbCnn.Close();
    }
   }
   return nRet;
  }

  /// <summary>
  /// //执行delete,update,insert等操作
  /// </summary>
  public int ExeNoQuery(OleDbCommand oleDbCmd)
  {
   int nRet = -1;
   m_strError = "";
   oleDbCmd.Connection = m_oleDbCnn;
   try
   {
    nRet = oleDbCmd.ExecuteNonQuery();
   }
   catch (OleDbException ex)
   {
    nRet = -1;
    m_strError = ex.ToString();
   }
   finally
   {
    if (m_oleDbCnn.State==ConnectionState.Open)
    {
     m_oleDbCnn.Close();
    }
   }
   return nRet;
  }

  /// <summary>
  /// 判断是否存在
  /// </summary>
  public int ExeIsExist(string strSql)
  {
   int nExist = -1;
   m_strError = "";
   OleDbCommand oleDbCmd = new OleDbCommand(strSql,m_oleDbCnn);
   try
   {
    OpenCnn();
    OleDbDataReader dr= oleDbCmd.ExecuteReader();
    if (dr.Read())
    {
     nExist = 1;
    }
    else
    {
     nExist = 0;
    }
   }
   catch (OleDbException ex)
   {
    nExist = -1;
    m_strError = ex.ToString();
   }
   finally
   {
    if (m_oleDbCnn.State==ConnectionState.Open)
    {
     oleDbCmd.Dispose();
     m_oleDbCnn.Close();
    }
   }
   return nExist;
  }

本文关键:初学ADO.NET时写的数据库访问类
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top