常用的增删改查函数

  无论做什么系统,涉及到数据库肯定离不开增删改查,下面是自己写的有关数据库增删改查封装的函数,仅供参考。

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using OnLineExam.HelperClass;
using OnLineExam.CommonClass;

namespace OnLineExam.DataBaseClass

{
  // 数据库接口类
	public class DataBase
	{
		//私有变量,数据库连接
		protected SqlConnection Connection;
        protected string ConnectionString;

		//构造函数
		public DataBase()
		{
            ConnectionString = "Data Source=(local);DataBase=mrOnLineExam;User ID=sa;Password=123456;";   
        }
		//保护方法,打开数据库连接
		private void Open()
		{
		  //判断数据库连接是否存在
			if (Connection == null)
			{
			  //不存在,新建并打开
				Connection = new SqlConnection(ConnectionString);
				Connection.Open();
			}
			else
			{
			  //存在,判断是否处于关闭状态
			  if (Connection.State.Equals(ConnectionState.Closed))
				  Connection.Open();    //连接处于关闭状态,重新打开
			}
		}

		//公有方法,关闭数据库连接
		public void Close() 
		{
			if (Connection.State.Equals(ConnectionState.Open))
			{
                Connection.Close();     //连接处于打开状态,关闭连接
			}
		}

        /// <summary>
		/// 析构函数,释放非托管资源
		/// </summary>
		~DataBase()
		{
			try
			{
				if (Connection != null)
					Connection.Close();
			}
			catch{}
			try
			{
				Dispose();
			}
			catch{}
		}

		//公有方法,释放资源
		public void Dispose()
		{
			if (Connection != null)		// 确保连接被关闭
			{
				Connection.Dispose();
				Connection = null;
			}
		}		

		//公有方法,根据Sql语句,返回是否查询到记录
		public bool GetRecord(string XSqlString)
		{
            Open();
            SqlDataAdapter adapter = new SqlDataAdapter(XSqlString, Connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            Close();

            if (dataset.Tables[0].Rows.Count > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		//公有方法,返回Sql语句获得的数据值
		//SqlString的格式:select count(*) from XXX where ...
		//                 select max(XXX) from YYY where ...
		public int GetRecordCount(string XSqlString)
		{
            string SCount;

			Open();
			SqlCommand Cmd = new SqlCommand(XSqlString,Connection);
            SCount = Cmd.ExecuteScalar().ToString().Trim();
            if (SCount=="")
            SCount="0";
			Close();
			return Convert.ToInt32(SCount);
		}			

		//公有方法,根据XWhere更新数据表XTableName中的某些纪录
		//XTableName--表名
		//XHT--哈希表,键为字段名,值为字段值		
		public DataSet AdvancedSearch(string XTableName, Hashtable XHT)
		{
			int Count = 0;

			string Fields = "";
			foreach(DictionaryEntry Item in XHT)
			{
				if (Count != 0)
				{
					Fields += " and ";
				}
				Fields += Item.Key.ToString();
				Fields += " like '%";
				
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值