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

📄 roomeditmodule.ascx.cs

📁 这里有好几个关于SQL Server 2000 开发的项目代码
💻 CS
字号:
namespace JdglWeb.Modules
{
	using System;
	using System.Data;
	using System.Data.SqlClient;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Configuration;

	/// <summary>
	///		RoomEditModule 的摘要说明。
	/// </summary>
	public class RoomEditModule : ModuleBase
	{
		protected System.Web.UI.WebControls.Label CreateLabel;
		protected System.Web.UI.WebControls.DropDownList RCategoryNameList;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.CustomValidator RCategoryCustomvalidator;
		protected System.Web.UI.WebControls.TextBox RPositionTextBox;
		protected System.Web.UI.WebControls.TextBox DescriptionTextBox;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
		protected System.Web.UI.WebControls.Button Submit;
		protected System.Web.UI.WebControls.HyperLink Return;
		protected System.Web.UI.WebControls.Label RoomIdLabel;
		protected System.Web.UI.WebControls.Label ShowMsg;

		private void Page_Load(object sender, System.EventArgs e)
		{
			
			if(!IsPostBack)
			{
				//绑定房间类型信息下拉列表框
				//从文件Web.config中读取连接字符串
				string sqldb= ConfigurationSettings.AppSettings["ConnectionString"];
				//连接JdglSys数据库
				SqlConnection Conn= new SqlConnection (sqldb);
				Conn.Open ();
				//定义sql语句
				string mysql="select RCategoryId,Name from RoomCategory ";
				SqlCommand command=new SqlCommand  (mysql,Conn);
				SqlDataReader dr=command.ExecuteReader ();
				while(dr.Read ())
				{
					ListItem li=new ListItem(dr["Name"].ToString(),dr["RCategoryId"].ToString());
					RCategoryNameList.Items.Add (li);
				}
				Conn.Close ();
			    
				//显示房间信息
				RoomIdLabel.Text=Request.QueryString ["RoomId"].ToString ();
				//连接JdglSys数据库
				SqlConnection Conn1 = new SqlConnection (sqldb);
				Conn1.Open ();
				//利用Command对象调用存储过程
				SqlCommand mycommand=new SqlCommand  ("sp_ShowRoomById",Conn1);
				//将命令类型转为存储类型
				mycommand.CommandType =CommandType.StoredProcedure ;
				mycommand.Parameters .Add ("@RoomId",SqlDbType.Int);
				mycommand.Parameters ["@RoomID"].Value = int.Parse(RoomIdLabel.Text);
				SqlDataReader dr1=mycommand.ExecuteReader ();
				if(dr1.Read ())
				{
					RCategoryNameList.Items.FindByText(dr1["Name"].ToString()).Selected=true;
					RPositionTextBox.Text =dr1["RPosition"].ToString();
					DescriptionTextBox.Text =dr1["Description"].ToString ();
				}
				//关闭Conn1
				Conn1.Close();
			}
		}

		//房间类型是否已选定
		public void NotNullValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			if(RCategoryNameList.SelectedIndex==0)
			{
				args.IsValid =false;//房间类型未选
			}
			else
			{
				args.IsValid =true;//房间类型已选
			}
		}
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		设计器支持所需的方法 - 不要使用代码编辑器
		///		修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.Submit.Click += new System.EventHandler(this.Submit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Submit_Click(object sender, System.EventArgs e)
		{
			if(Page.IsValid )
			{
				//从文件Web.config中读取连接字符串
				string sqldb = ConfigurationSettings.AppSettings["ConnectionString"];
				//连接JdglSys数据库
				SqlConnection Conn = new SqlConnection (sqldb);
				Conn.Open ();
				//定义sql语句
				String updatesql="update RoomsInfo set RCategoryId= @RCategoryId, RPosition=@RPosition,Description=@Description where RoomId = @RoomId";
				//利用Command对象调用updatesql
				SqlCommand mycommand=new SqlCommand (updatesql,Conn);
				//添加参数
				mycommand.Parameters .Add ("@RoomId",SqlDbType.Int);
				mycommand.Parameters .Add ("@RCategoryId",SqlDbType.Int);
				mycommand.Parameters .Add ("@RPosition",SqlDbType.NVarChar);
				mycommand.Parameters .Add ("@Description",SqlDbType.NVarChar);
				//给存储过程的参数赋值
				mycommand.Parameters ["@RoomId"].Value =RoomIdLabel.Text;
				mycommand.Parameters ["@RCategoryId"].Value =RCategoryNameList.SelectedIndex;
				mycommand.Parameters ["@RPosition"].Value =RPositionTextBox.Text.Trim();
				mycommand.Parameters ["@Description"].Value =DescriptionTextBox.Text.Trim();
				try
				{
					mycommand.ExecuteNonQuery();
					ShowMsg.Text="房间信息修改成功";
					ShowMsg.Style["color"]="green";}
				catch(SqlException error)
				{
					ShowMsg.Text="修改未成功,请稍后再试。原因:"+error.Message;
					ShowMsg.Style["color"]="red";
				}
				//关闭连接
				Conn.Close();
			}
		}
	}
}

⌨️ 快捷键说明

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