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

📄 baseadlc.cs

📁 一个好的系统 对初学者很有用啊 来看看啊
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using ExamCenter.BLCTier;
namespace ExamCenter.ADLCTier
{

	/// <summary>
	/// Model 的摘要说明。
	/// -----------功能-----------
	/// 改类为静态类
	/// 此类含有3个方法:为抽象的数据访问类		
	/// 其它数据库连接、操作一般情况下应给依此为基类
	/// -------------输入输出参数说明---------
	/// 接收参数 
	/// SQL 语句
	/// 返回值
	/// 1 int
	/// 2 DataSet
	/// 3 bool
	/// ------------应用数据库表信息----------
	/// 根据sql语句连接可操作表
	/// 为不同表操作的基类
	/// ----------------编码信息---------
	/// 程序员   : 任仲考 李志胜 整理
	/// 创建时间 : 2003.08.19 

	public abstract class BaseADLC
	{
		//"Data source=rzk;initial catalog=examcenter;user id=sa;password=;"		
		
		public static string strConn="";
		public static SqlConnection conn; 
		protected static void Open() 
		{
			// 任:20修改数据库打开方式
//			string serverip=Encryption.Decrypt(ConfigurationSettings.AppSettings["serverip"],"11111111");
//			string databasename=Encryption.Decrypt(ConfigurationSettings.AppSettings["databasename"],"11111111");
//			string uid=Encryption.Decrypt(ConfigurationSettings.AppSettings["uid"],"11111111");
//			string pwd=Encryption.Decrypt(ConfigurationSettings.AppSettings["pwd"],"11111111");
//			strConn="Data source="+serverip+";"+"initial catalog="+databasename+";"+"user id="+uid+";"+"password="+pwd+";";			

		}
		protected static void Close() 
		{
			
			if (conn != null)
				conn.Close();
			
			
		}
		protected static int ExecuteSql(string strSql)
		{
			
			// 方法  :public static int ExecuteSql(string strSql)
			// 参数  :strSql为SQL语句
			// 功能  :执行增加,修改,删除操作
			// 返回值:整型
			Open() ;
			SqlConnection conn=new SqlConnection(strConn);
			SqlCommand cmd=new SqlCommand(strSql,conn);
			try
			{
				conn.Open();
				int row=cmd.ExecuteNonQuery();
				return row;
			}
			catch(System.Data.SqlClient.SqlException er)
			{
				throw new Exception(er.Message);
			}
			finally
			{
				cmd.Dispose();
				conn.Close();
			
			}

		}

		protected static DataSet ExecuteSqlReturnDs(string strSql)
		{
			// 方法  :public static DataSet ExecuteSqlReturnDs(string strSql)
			// 参数  :strSql为SQL语句
			// 功能  :执行查询操作
			// 返回值:数据集 DataSet
			Open() ;
			SqlConnection conn=new SqlConnection(strConn);
			try
			{
				conn.Open();
				SqlDataAdapter sda=new SqlDataAdapter(strSql,strConn);
				DataSet ds=new DataSet();
				sda.Fill(ds);
				return ds;
			}
			catch(System.Data.SqlClient.SqlException er)
			{
				throw new Exception(er.Message);
			}
			finally
			{
				conn.Close();
			}


		}

		protected static bool ExecuteSqlQueryReturnBool(string strSql)
		{
			// 方法  :public static bool ExecuteSqlReturnBool(string strSql)
			// 参数  :strSql为SQL语句
			// 功能  :执行查询操作确定是否含有查询记录
			// 返回值:布尔型
			Open();
			SqlConnection conn=new SqlConnection(strConn);
			SqlCommand cmd=new SqlCommand(strSql,conn);
			try
			{
			    conn.Open();
				object obj=cmd.ExecuteScalar();
				if(!object.Equals(obj,null))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(System.Data.SqlClient.SqlException er)
			{
				throw new Exception(er.Message);
			}
			finally
			{
				cmd.Dispose();
				conn.Close();
		
			}
		}

		protected static void UpdateDataSource(DataSet ds,string strSql)
		{
			//方法  :protected static void UpdateDataSource(DataSet dsData,string strSql)
			//参数  : 1.dsData为要更改的数据集
			//         2.strSql为SQL语句
			//功能  :更新数据源
			//返回值:空值
			Open();
			SqlConnection conn = new SqlConnection(strConn);
			SqlDataAdapter sda;
			SqlCommandBuilder cmdBuilder;
			SqlCommand cmd;

			if (ds.Tables[0] == null)
				throw new NullReferenceException("There is no Table existing in the current DataSet.");

			try
			{
				conn.Open();

				cmd = new SqlCommand(strSql, conn);
				sda = new SqlDataAdapter(cmd);
				cmdBuilder = new SqlCommandBuilder(sda);
				DataTable dtDeleted = ds.Tables[0].GetChanges(DataRowState.Deleted);
				if (dtDeleted != null)
					sda.Update(dtDeleted);

				DataTable dtModified = ds.Tables[0].GetChanges(DataRowState.Modified);
				if (dtModified != null)
					sda.Update(dtModified);

				DataTable dtAdded = ds.Tables[0].GetChanges(DataRowState.Added);
				if (dtAdded != null)
					sda.Update(dtAdded);
			}
			catch (SqlException sqlExc)
			{
				MessageBox.Show(sqlExc.ToString());
				throw sqlExc;
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.ToString());
				throw exc;
			}
			finally
			{
				if (conn.State != ConnectionState.Closed)
					conn.Close();
			}
		}

	}

}

⌨️ 快捷键说明

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