⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbbasesql.cs

📁 一个数据库操作类
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;

namespace XBase
{
	#region Base Class for SQL-Server
	/// <summary>
	/// Descript:DataBase class for SQL-Server
	/// Author:XuminRong
	/// Create date:2004/11/12
	/// </summary>
	public class DBBaseSql
	{
		#region Variable
		/// <summary>
		/// Connect DataBase's string
		/// </summary>
		private string m_strConn;
		/// <summary>
		/// Sql string
		/// </summary>
		protected string m_strSQL;
		/// <summary>
		/// Error information
		/// </summary>
		protected Exception m_Err;

		#region SqlClient
		/// <summary>
		/// SqlConnection object
		/// </summary>
		protected SqlConnection m_SqlConnection;
		/// <summary>
		/// SqlCommand object
		/// </summary>
		protected SqlCommand m_SqlCommand;    
		/// <summary>
		/// SqlDataReader object
		/// </summary>
		protected SqlDataReader m_SqlDataReader;  
		/// <summary>
		/// SqlDataAdapter object
		/// </summary>
		protected SqlDataAdapter m_DataAdapter;
		#endregion
		#endregion

		#region struct function
		/// <summary>
		/// Title: DBBaseSql function
		/// Description:DBBaseSql struct function
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="conn_">string type;Connect DataBase's string;</param>
		public DBBaseSql(string strConn_)
		{
			m_strConn = strConn_;
			m_Err = null;
			return;
		}
		#endregion

		#region Connect
		/// <summary>
		/// Title: Connect function
		/// Description:Connect SQL-Server DataBase
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <returns>bool type;If connect success return true else return false.</returns>
		public bool Connect()
		{
			m_SqlConnection = new SqlConnection();
			m_SqlConnection.ConnectionString = m_strConn;
			try
			{
				//Open SqlConnection
				m_SqlConnection.Open();
				return true;
			}
			catch(Exception err_)
			{
				//Throw error
				m_Err =err_;
				return false;
			}
		}
		/// <summary>
		/// Title: Close function
		/// Description:Close SqlConnection
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		public void Close()
		{
			m_SqlConnection.Close();
			return;
		}
		/// <summary>
		/// Title: CloseState function
		/// Description:Get SqlConnection's closeState
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <returns>if open return true else return false.</returns>
		public bool CloseState()
		{
			if (m_SqlConnection.State.ToString().ToLower() == "Open".ToLower())
			{
				return true;
			}
			return false;
		}
		/// <summary>
		/// Title: ConnectState Property
		/// Description:Get SqlConnection's closeState
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		private string ConnectState
		{
			get
			{
				return m_SqlConnection.State.ToString();
			}
		}
		/// <summary>
		/// Title: ConnectString property
		/// Description:Get SqlConnection's string
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		public string ConnectString
		{
			get
			{
				return m_strConn;
			}
		}
		#endregion

		#region Execute by sql
		/// <summary>
		/// Title: ExcuteNonQuery function
		/// Description:ExcuteNonQuery by sql 
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="SqlComm_">string type;SQL CommandText</param>
		/// <returns>int type;Changed rows;</returns>
		public int ExcuteNonQuery(string strSqlComm_)
		{
			//Clear null
			m_Err = null;
			m_strSQL = strSqlComm_;
			try
			{
				//Execute  strSqlComm_
				m_SqlCommand =  new SqlCommand(strSqlComm_, m_SqlConnection);
				return m_SqlCommand.ExecuteNonQuery();
			}
			catch(Exception err_)
			{
				//Throw error
				m_Err = err_;
				return -1;
			}
		}
		/// <summary>
		/// Title: ExcuteScalar function
		/// Description:ExcuteScalar by sql 
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="SqlComm_">string type;SQL CommandText</param>
		/// <returns>object type;Excute value;</returns>
		public object ExcuteScalar(string strSqlComm_)
		{
			//Clear null
			m_Err = null;
			m_strSQL = strSqlComm_;
			try
			{
				//Execute  strSqlComm_
				m_SqlCommand =  new SqlCommand(strSqlComm_, m_SqlConnection);
				return m_SqlCommand.ExecuteScalar();
			}
			catch(Exception err_)
			{
				//Throw error
				m_Err = err_;
				return null;
			}
		} 
		/// <summary>
		/// Title: GetDataSet function
		/// Description:Filled DataSet by sql
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="SqlComm_">string type;SQL CommandText</param>
		/// <param name="TableName_">string type;DataSet's Name</param>
		/// <param name="dsDataSet_">DataSet type;will filled's DataSet</param>
		public void GetDataSet(string strSqlComm_,string strTableName_,ref DataSet dsDataSet_)
		{
			//Clear m_Err
			m_Err = null;
			m_strSQL = strSqlComm_;
			try
			{
				//Execute  strSqlComm_
				m_DataAdapter = new SqlDataAdapter(strSqlComm_,m_SqlConnection);
				m_DataAdapter.Fill(dsDataSet_,strTableName_);
				return;
			}
			catch(Exception err_)
			{
				//Throw error
				m_Err = err_;
				return;
			}
		}
		#endregion

		#region DataReader
		/// <summary>
		/// Title: ExcuteDBReader function
		/// Description:ExcuteDBReader by sql;Get Object for  DataReader
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="SqlComm_">string type;SQL CommandText</param>
		public void ExcuteDBReader(string strSqlComm_)
		{
			//Clear null
			m_Err = null;
			m_strSQL = strSqlComm_;
			try
			{
				//Execute  strSqlComm_
				m_SqlCommand =  new SqlCommand(strSqlComm_, m_SqlConnection);
				m_SqlDataReader = m_SqlCommand.ExecuteReader();
				return;
			}
			catch(Exception err_)
			{
				//Throw error
				m_Err = err_ ;
				return;
			}
		}
		/// <summary>
		/// Title: GetReadItem function
		/// Description:Get DataReader member by index in current row
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="Ordinal_">int type;DataReader member's index</param>
		/// <returns>object type;DataReader member</returns>
		public object GetReadItem(int intOrdinal_)
		{
			return m_SqlDataReader.GetValue(intOrdinal_);
		}
		/// <summary>
		/// Title: GetReadItem function
		/// Description:Get DataReader member by name in current row
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <param name="ItemName_">string type;DataReader member's name</param>
		/// <returns>object type;DataReader member</returns>
		public object GetReadItem(string strItemName_)
		{
			return m_SqlDataReader[strItemName_];
		}
		/// <summary>
		/// Title: ReadResult function
		/// Description:Read  DataReader's next row recorder
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		/// <returns>bool trye;next row is null return false else return true</returns>
		public bool ReadResult()
		{
			return m_SqlDataReader.Read();
		}
		/// <summary>
		/// Title: CloseReader function
		/// Description:Close DataReader
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		public void CloseReader()
		{
			m_SqlDataReader.Close();
		}
		#endregion
		
		#region Property
		/// <summary>
		/// Title: Err property
		/// Description:Get m_Err object
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		public object Err
		{
			get
			{
				return m_Err;
			}
		}
		/// <summary>
		/// Title: SQL property
		/// Description:Get sql string
		/// Author:MR.XU
		/// Cteate Date:2004/11/12
		/// </summary>
		public string SQL
		{
			get
			{
				if (m_strSQL == null)
				{
					m_strSQL = "";
				}
				return m_strSQL;
			}
		}
		#endregion
	}
	#endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -