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

📄 dbconn.cs

📁 这个是当年毕业设计的参考资料
💻 CS
字号:
using System;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using Microsoft.Win32;
using System.Xml;
using System.Windows.Forms;
using System.Drawing;
namespace DBConn
{		
	/// <summary>
	/// DB 的摘要说明。
	/// class DB is like a virtual Database,
	/// All operations on Database are done by this class.
	/// </summary>
	internal class ParameterList
	{
		private ArrayList al;		

		public ParameterList()
		{
			al = new ArrayList();
		}

		public ParameterList(int capacity)
		{			
			al = new ArrayList(capacity);
		}

		public void add(string paramName,SqlDbType type, int size, object paramValue)
		{			
			SqlParameter odp = new SqlParameter(paramName, type, size);
			odp.Value = paramValue;
			odp.Direction = ParameterDirection.Input;
			al.Add(odp);
		}

		public void add(string paramName, SqlDbType type, int size, object paramValue, ParameterDirection pd)
		{			
			SqlParameter odp = new SqlParameter(paramName, type, size);
			odp.Value = paramValue;
			odp.Direction = pd;
			al.Add(odp);

		}
		
		public SqlParameter getParameter(int index)
		{
			return (SqlParameter)al[index];
		}
		public object getParamValue(int index)
		{
			return this.getParameter(index).Value;
		}
		public int Length
		{
			get
			{
				return al.Count;
			}
		}


	}
	internal class DB
	{
		private static string connectStringForReading = null;
		private static string connectStringForWriting = null;

		private  DB()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		/// <summary>
		/// 检查连接字符串是否有效,如果无效,就试图从注册表读取数据。
		/// 如果无法读取,就报错。
		/// </summary>
		/// <returns></returns>
		private static bool checkForConnectString()
		{
			if (connectStringForReading == null || connectStringForWriting == null)
			{
				//				RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\SKXT", false);
				//				//string  dbcon = "server=syit9;database=SKDB;user id=sa;password=;";
				//				connectStringForWriting = (string)rk.GetValue("value");	
				//				//connectStringForWriting = dbcon;

				//============================================================================================
				string XdPath = Application.StartupPath;//获取路径
				XmlNodeReader reader = null ;

				//string connectStringForWriting="";
				try
				{
					string s = "" ;
					XmlDocument doc = new XmlDocument ( ) ;
					// 装入指定的XML文档
					doc.Load ( ""+XdPath+"\\INI.xml" ) ; 
					// 设定XmlNodeReader对象来打开XML文件
					reader = new XmlNodeReader ( doc ) ;
					// 读取XML文件中的数据,并显示出来
					//string ss=reader.ReadString();
				
					while ( reader.Read ( ) ) 
					{
						//判断当前读取得节点类型
						switch ( reader.NodeType )
						{
							case XmlNodeType.Element :
								s = reader.Name ;
								break ;
							case XmlNodeType.Text :
								if ( s.Equals ( "ServerIni" ) )
									connectStringForWriting=reader.Value;//								
								break ;
						} 
					}
				}
				finally
				{
					//清除打开的数据流
					if ( reader != null )
						reader.Close ( ) ;
				}
				//============================================================================================

				connectStringForReading = connectStringForWriting;
			}

			if (connectStringForReading == null || connectStringForWriting == null)
				return false;
			else
				return true;
				
		}

		/// <summary>
		/// 获得对数据库的连接
		/// </summary>
		/// <param name="isReadOnly">是否以只读方式连接数据库</param>
		/// <returns>数据库连接</returns>
		internal static SqlConnection getSqlConnection(bool isReadOnly)
		{
			if(!DB.checkForConnectString())
				return null;

			if(isReadOnly)
				return new SqlConnection(DB.connectStringForReading);
			else
				return new SqlConnection(DB.connectStringForWriting);

		}
		//for getting data set;
		internal static DataSet getDataSet(SqlConnection conn, string searchString, bool isStoredProcedure)
		{
			if(!DB.checkForConnectString())
				return null;

			DataSet ds = new DataSet();
			SqlDataAdapter adapter = new SqlDataAdapter(searchString, conn);
			if (isStoredProcedure)
				adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
			adapter.Fill(ds);
			adapter.Dispose();

			return ds;
		}

		//for getting scalar data
		internal static object getScalarData(SqlConnection conn, string searchString, bool isStoredProcedure)
		{
			object returnData = new object();

			if(!DB.checkForConnectString())
				return null;

			SqlCommand command = new SqlCommand(searchString, conn);

			if (isStoredProcedure)
				command.CommandType = CommandType.StoredProcedure;
		
			return command.ExecuteScalar();
		}

		//for insert delete update operations
		internal static int updateRows(SqlConnection conn, string updateString, bool isStoredProcedure)
		{
			if(!DB.checkForConnectString())
				return -1;

			SqlCommand command = new SqlCommand(updateString, conn);
			if (isStoredProcedure)
				command.CommandType = CommandType.StoredProcedure;
			return command.ExecuteNonQuery();
		}


		internal static DataSet getDataSet(SqlConnection conn, string searchString, ParameterList parameters, bool isStoredProcedure)
		{
			if(!DB.checkForConnectString())
				return null;

			DataSet ds = new DataSet();
			SqlDataAdapter adapter = new SqlDataAdapter(searchString, conn);
			if (isStoredProcedure)
				adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

			for (int i = 0; i < parameters.Length; ++i)
			{

				
				adapter.SelectCommand.Parameters.Add(parameters.getParameter(i));

			}

			adapter.Fill(ds);
			adapter.Dispose();

			return ds;
		}
		internal static object getScalarData(SqlConnection conn, string searchString, ParameterList parameters, bool isStoredProcedure)
		{

			if(!DB.checkForConnectString())
				return null;

			SqlCommand command = new SqlCommand(searchString, conn);

			for (int i = 0; i < parameters.Length; ++i)
			{
				command.Parameters.Add(parameters.getParameter(i));
			}

			if(isStoredProcedure)
				command.CommandType = CommandType.StoredProcedure;

			return command.ExecuteScalar();
		}
		//===============================================================
		//专门为 storyproc而做。(不需要返回的)
		internal static SqlDataReader getDataSet(SqlConnection conn, bool isStoredProcedure,string searchString, ParameterList parameters)
		{
			if(!DB.checkForConnectString())
				return null;
			
			SqlCommand command = new SqlCommand(searchString, conn);
			
			if (isStoredProcedure)
				command.CommandType = CommandType.StoredProcedure;
			for (int i = 0; i < parameters.Length; ++i)
			{
				
				command.Parameters.Add(parameters.getParameter(i));
			}
			SqlDataReader ds=command.ExecuteReader();
			return ds;
        
		}
		//=========================================================================
		//专门为 storyproc而做。(需要返回的)
		internal static SqlDataReader getDataSet(SqlConnection conn, SqlCommand command,string searchString, ParameterList parameters)
		{
			if(!DB.checkForConnectString())
				return null;
			
			command.CommandType = CommandType.StoredProcedure;
			for (int i = 0; i < parameters.Length; ++i)
			{
				
				command.Parameters.Add(parameters.getParameter(i));
			}
			SqlDataReader ds=command.ExecuteReader();
			return ds;
        
		}
		//==========================================================================

		//for insert delete update operations
		internal static int  updateRows(SqlConnection conn, string updateString, ParameterList parameters, bool isStoredProcedure)
		{
			if(!DB.checkForConnectString())
				return -1;

			SqlCommand command = new SqlCommand(updateString, conn);

			if (isStoredProcedure)
				command.CommandType = CommandType.StoredProcedure;
			for (int i = 0; i < parameters.Length; ++i)
			{
				
				command.Parameters.Add(parameters.getParameter(i));
			}

			return command.ExecuteNonQuery();

		}




	}
}

⌨️ 快捷键说明

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