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

📄 dbbaseclass.cs

📁 这是一个编好的网上书店系统
💻 CS
字号:
/********************************************************************
	created:	2004/11/27
	created:	27:11:2004   15:37
	filename: 	BaseClass.cs
	file base:	BaseClass
	file ext:	cs
	author:		Robert
	
	purpose:	It's the baseclass of all this online book store. So I 
				named it as abstract class.
*********************************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace RobertSoft.BookStore.DBClass
{
	/// <summary>
	/// BaseClass 
	/// </summary>
	public abstract class DBBaseClass
	{
#region "Fields of BaseClass"

		/// <summary>
		/// SQL Server connection string
		/// </summary>
		protected static string strConn = ConfigurationSettings.AppSettings["strConnection"];

		/// <summary>
		/// SQL Command
		/// </summary>
		protected static string strCmd;

#endregion

#region "Properties of BaseClass"
		
		private int nID;
		private string strName;

		/// <summary>
		/// Property: ID
		/// </summary>
		public int ID
		{
			get
			{
				return nID;
			}
			set
			{
				nID = value;
			}
		}

		/// <summary>
		/// Property: Name
		/// </summary>
		public string Name
		{
			get
			{
				return strName;
			}
			set
			{
				strName = value;
			}
		}

#endregion

#region "Functions of BaseClass"

		/// <summary>
		/// Execute SQL Commands
		/// </summary>
		/// <param name = "strSQL"> string </param>
		/// <return> int </return>
		public static int ExecuteSQLCmd(string strSQL)
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			SqlCommand currentCmd = new SqlCommand(strSQL, currentConn);
			try
			{
				currentConn.Open();
				currentCmd.ExecuteNonQuery();
				return 0;
			}

			// In case of some exception
			
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			
			// We have to close the connection at last correctly.
			
			finally
			{
				currentCmd.Dispose();
				currentConn.Close();
			}
		}

		/// <summary>
		/// Excute SQL Commands to reader. Just for query use.
		/// </summary>
		/// <param name="strSQL">string</param>
		/// <returns>int</returns>
		protected static int ExecuteSQLCmdEx(string strSQL)
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			SqlCommand currentCmd = new SqlCommand(strSQL, currentConn);
			try
			{
				currentConn.Open();

				// Just for query, so we use SqlDataReader here.

				SqlDataReader currentReader = currentCmd.ExecuteReader();
				if(currentReader.Read())
				{
					return 0;
				}
				else
				{
					throw new Exception("Value Unavailable!");
				}
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			finally
			{
				currentCmd.Dispose();
				currentConn.Close();

			}
		}

		/// <summary>
		/// Get DataSet
		/// </summary>
		/// <param name="strSQL">string</param>
		/// <returns>DataSet</returns>
		public static DataSet ExecuteSQLForDS(string strSQL) 
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			try
			{
				currentConn.Open();
				SqlDataAdapter currentDA = new SqlDataAdapter(strSQL, currentConn);
				DataSet ds = new DataSet("ds");
				currentDA.Fill(ds);
				return ds;
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			finally
			{
				currentConn.Close();
			}
		}
		
		/// <summary>
		/// Just get a single value
		/// </summary>
		/// <param name="strSQL">string</param>
		/// <returns>int</returns>
		public static int ExecuteSQLForValue(string strSQL)
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			SqlCommand currentCmd = new SqlCommand(strSQL, currentConn);

			try
			{
				currentConn.Open();
				// Only return the first line of the results
				object ret = currentCmd.ExecuteScalar();
				if(Object.Equals(ret, null))
				{
					throw new Exception("Value unavailable!");
				}
				else 
				{
					return (int)ret;
				}
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			finally
			{
				currentCmd.Dispose();
				currentConn.Close();
			}
		}
		
		/// <summary>
		/// Return for an object
		/// </summary>
		/// <param name="strSQL">string</param>
		/// <returns>object</returns>
		public static Object ExecuteSQLForValueEx(string strSQL)
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			SqlCommand currentCmd = new SqlCommand(strSQL, currentConn);
			try
			{	
				currentConn.Open();
				object ret = currentCmd.ExecuteScalar();
				if(Object.Equals(ret, null))
				{
					throw new Exception("Value unavailable!");
				}
				else
				{
					return ret;
				}
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			finally
			{
				currentCmd.Dispose();
				currentConn.Close();
			}
		}

		/// <summary>
		/// Execute more than one sql commands
		/// </summary>
		/// <param name="strSQL">string</param>
		/// <returns>int</returns>
		protected static int ExecuteSQLs(string[] strSQLs)
		{
			SqlConnection currentConn = new SqlConnection(strConn);
			SqlCommand currentCmd = new SqlCommand();
			int i = strSQLs.Length;
			
			try
			{
				currentConn.Open();
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}

			SqlTransaction currentTran = currentConn.BeginTransaction();

			try
			{
				currentCmd.Connection = currentConn;
				currentCmd.Transaction = currentTran;
				foreach(string str in strSQLs)
				{
					currentCmd.CommandText = str;
					currentCmd.ExecuteNonQuery();
				}
				currentTran.Commit();
				return 0;
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				currentTran.Rollback();
				throw new Exception(e.Message);
			}
			finally
			{
				currentCmd.Dispose();
				currentConn.Close();
			}
		}
#endregion	
	}
}

⌨️ 快捷键说明

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