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

📄 dbservice_2.cs

📁 酒店管理,主要从事酒店管理都可以.这可是北大青鸟的程序
💻 CS
字号:
using System;
using System.Data;
using System.Data.Odbc;

namespace HotelApp
{
	/// <summary>
	/// DBService_2 的摘要说明。
	/// </summary>
	public class DBService_2
	{
		private string strCon;
		private OdbcConnection con;
		//private OdbcCommand comm;
		public OdbcCommand comm;
		private OdbcDataAdapter sda;

		public string conStr
		{
			set
			{
				this.strCon=value;
			}
			get
			{
				return this.conStr;
			}
		}

		public DBService_2()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			setConString();
			con=new OdbcConnection(this.strCon);
			comm=new OdbcCommand();
			sda=new OdbcDataAdapter();
		}

		public OdbcConnection getCon()
		{
			con=new OdbcConnection(this.strCon);
			//con.Open();
			return this.con;
		}

		private void setConString()
		{
			this.conStr= "DRIVER=MySQL ODBC 3.51 Driver;UID=root;STMT=;OPTION=;PORT=3306;PASSWORD=;SERVER=localhost;DATABASE=hoteldb;DESC=";
		}

		//执行查询
		public DataSet executeBySQL(string sql)
		{
			DataSet ds=new DataSet();
			comm.CommandText=sql;
			comm.Connection=con;
			sda.SelectCommand=comm;
			
			try
			{
				sda.Fill(ds);
			}
			catch(Exception ex)
			{
				Console.Write(ex.Message);
				throw ex;
			}
			return ds;
		}
		//分页查询
		public DataSet executeBySQL(string sql,int startRecord,int maxSize,string TableName)
		{
			DataSet ds=new DataSet();
			comm.CommandText=sql;
			comm.Connection=con;
			sda.SelectCommand=comm;
			try
			{
				sda.Fill(ds,"total");
				sda.Fill(ds,(startRecord-1)*maxSize,maxSize,TableName);
			}
			catch(Exception ex)
			{
				Console.Write(ex.Message);
				throw ex;
			}
			return ds;
		}
		//增、删、改
		public int ExecuteNonQuery(string sql)
		{
			int i=0;
			comm.CommandText=sql;
			comm.Connection=con;
			con.Open();
			try
			{
				i=comm.ExecuteNonQuery();
			}
			catch(Exception ex)
			{
				Console.Write(ex.Message);
				throw ex;
			}
			finally
			{
				if(con!=null)
				{
					con.Close();
				}
			}
			return i;
		}
		public void setCommandText(string sql)
		{
			this.comm.CommandText=sql;
		}

		//增、删、改---不带参数
		public int ExecuteNonQuery()
		{
			int i=0;		
			comm.Connection=con;
			con.Open();
			try
			{
				i=comm.ExecuteNonQuery();
			}
			catch(Exception ex)
			{
				Console.Write(ex.Message);
				throw ex;
                
			}
			finally
			{
				if(con!=null)
				{
					con.Close();
				}
			}
			return i;
		}


	}
}

⌨️ 快捷键说明

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