pagebase.cs

来自「《C#数据库项目案例导航》一书的配套光盘」· CS 代码 · 共 98 行

CS
98
字号
using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.SessionState;

namespace KhfwWeb
{
	/// <summary>
	/// PageBase 的摘要说明。
	/// </summary>
	public class PageBase:System.Web.UI.Page
	{
		public PageBase()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		private static string UrlSuffix
		{
			get
			{
				return HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
			}
		}
		/// <value>
		///UrlBase is used to get the prefix for URLs
		/// </value>
		public static String UrlBase
		{
			get
			{  
				return @"http://" + UrlSuffix; 
			}
		}

		public static string UserType;
		
		public static bool CheckUser(string name,string pwd)
		{
			bool authenticated = false;
			//从文件Web.config中读取连接字符串
			string sqldb= ConfigurationSettings.AppSettings["ConnectionString"];
			//创建Command对象
			SqlCommand mycommand = new SqlCommand();
			//连接JdglSys数据库
			mycommand.Connection=new SqlConnection(sqldb);
			try
			{
				mycommand.Connection.Open();
				//调用存储过程sp_ValidateUser检验帐户的有效性
				mycommand.CommandText="sp_ValidateUser";
				mycommand.CommandType=CommandType.StoredProcedure;
			   
				SqlParameter Name=new SqlParameter("@name",SqlDbType.NVarChar,20);
				Name.Value=name.Trim();
				mycommand.Parameters.Add(Name);
				
				SqlParameter Password=new SqlParameter("@pwd",SqlDbType.NVarChar,15);
				Password.Value=pwd.Trim();
				mycommand.Parameters.Add(Password);
                
				SqlParameter IsValid=new SqlParameter("@IsValid",SqlDbType.Int);
				IsValid.Direction=ParameterDirection.Output;
				mycommand.Parameters.Add(IsValid);

				SqlParameter Type=new SqlParameter("@type",SqlDbType.Int);
				Type.Direction=ParameterDirection.Output;
				mycommand.Parameters.Add(Type);
           
				mycommand.ExecuteNonQuery();
				if(((int)IsValid.Value)==1)
				{
					//帐户有效
					authenticated=true;  
					//获取员工类型
					UserType=Type.Value.ToString();
				}
			}
			catch(Exception exc)
			{
				throw(exc);
			}
			finally
			{
				mycommand.Connection.Close();
			}
			//返回布尔值
			return authenticated;
		}

	}
}

⌨️ 快捷键说明

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