📄 commonservice.cs
字号:
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 通过页面的URL获得主关键字
/// </summary>
/// <param name="thePage">调用此方法的Web页面对象</param>
/// <returns>主关键字数组</returns>
/// **************************************************************************
public static string[] GetCurrentPks(System.Web.UI.Page thePage)
{
//把页面的完整URL通过“&”进行分解
string[] original_pks = thePage.Request.RawUrl.Split('&');
string[] pks = new string[original_pks.Length - 2];
//将分解后的字符串进行处理,获得主关键字数组
for (int i=2; i<original_pks.Length; i++)
{
pks[i-2] = original_pks[i].Substring(original_pks[i].LastIndexOf("=") + 1);
}
return pks;
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 在弹出窗口中生成返回时父页面的完整URL
/// </summary>
/// <param name="thePage">弹出窗口的Web页面对象</param>
/// <returns>父页面的完整URL</returns>
/// **************************************************************************
public static string CreatReturnURL(System.Web.UI.Page thePage)
{
//获取父页面的当前页索引
string PageIndex = thePage.Request.QueryString["PageIndex"];
//获取父页面的URL
string ParentURL = thePage.Request.QueryString["ParentURL"];
//将父页面的URL进行还原
ParentURL = ParentURL.Replace("|","&");
//检查父页面的URL中是否含有页面信息,分别进行不同的生成处理
if(ParentURL.LastIndexOf("PageIndex") == -1)
{
string LinkChar;
if (ParentURL.IndexOf("?") != -1)
{
LinkChar = "&";
}
else
{
LinkChar = "?";
}
return(ParentURL + LinkChar + "PageIndex=" + PageIndex);
}
else
{
return(ParentURL.Substring(0,ParentURL.LastIndexOf("=") + 1) + PageIndex);
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 关闭窗口页面,返回父页面
/// </summary>
/// <param name="thePage">子页面的Page对象</param>
/// **************************************************************************
public static void Return(System.Web.UI.Page thePage)
{
string ReturnURL;
ReturnURL = CommonService.CreatReturnURL(thePage);
thePage.Response.Write("<script language=javascript>window.opener.location='" + ReturnURL + "';window.close();</script>");
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 返回特定的父页面
/// </summary>
/// <param name="thePage">子页面的Page对象</param>
/// <param name="ReturnURL">父页面的URL,如果参数为null,表示由系统来取得父页面的URL</param>
/// <param name="Flag">是否关闭子窗口的开关标记,true表示关闭子窗口,false表示不关闭子窗口</param>
/// **************************************************************************
public static void Return(System.Web.UI.Page thePage, string ReturnURL, bool Flag)
{
string script;
if(ReturnURL == null)
{
ReturnURL = CommonService.CreatReturnURL(thePage);
}
script = "<script language=javascript>window.opener.location='" + ReturnURL + "';";
if(Flag == true)
{
script = script + "window.close();";
}
script =script + "</script>";
thePage.Response.Write(script);
}
/// **************************************************************************
/// END
/// **************************************************************************
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
//以下是对下拉列表框的处理方法集
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根据查询语句对下拉列表框控件与数据记录集进行绑定<br/>
/// 注意事项:ValueField和TextField必须是SqlStatement查询结果集中的两个字段
/// </summary>
/// <param name="theDDL">DropDownList控件对象</param>
/// <param name="SqlStatement">检索数据的SQL语句</param>
/// <param name="ValueField">DropDownList的值域</param>
/// <param name="TextField">DropDownList的文本域</param>
/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
/// **************************************************************************
public static bool BindDropDownList( System.Web.UI.WebControls.DropDownList theDDL,
string SqlStatement,
string ValueField,
string TextField)
{
//声明并创建一个SqlConnection对象的实例
SqlConnection Connection = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
try
{
//声明并创建一个SqlCommand对象的实例
System.Data.SqlClient.SqlCommand SqlCmd = Connection.CreateCommand();
//给SqlCommand对象指定到某个SQL语句
SqlCmd.CommandText = SqlStatement;
//打开数据库连接对象
Connection.Open();
//声明并使用SqlCommand的ExecuteReader方法创建一个SqlDataReader对象的实例
System.Data.SqlClient.SqlDataReader SqlDR = SqlCmd.ExecuteReader();
//将查询结果集与下拉列表框控件进行绑定
theDDL.DataSource = SqlDR;
theDDL.DataValueField = ValueField;
theDDL.DataTextField = TextField;
theDDL.DataBind();
//关闭数据库连接对象
Connection.Close();
return true;
}
catch(Exception e)
{
if(Connection.State.ToString() == "Open") Connection.Close();
LogService.Write ("BindDropDownList(System.Web.UI.WebControls.DropDownList theDDL,string SqlStatement,string ValueField,string TextField)");
LogService.Write ("在根据查询语句对下拉列表框控件与数据记录集进行绑定时发生错误。");
LogService.Write (e.Message);
return false;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 对DropDownList控件中某个特定的值进行定位
/// </summary>
/// <param name="theDDL">下拉列表控件对象(传递值)</param>
/// <param name="selectvalue">要定位的项的Value值(传递值)</param>
/// <returns>布尔型返回值,执行成功返回true,执行失败返回false,并将错误信息写入错误日志</returns>
/// **************************************************************************
public static bool LocateDropDownList(System.Web.UI.WebControls.DropDownList theDDL, string SelectValue)
{
try
{
for(int i=0; i<theDDL.Items.Count; i++)
{
if (theDDL.Items[i].Value == SelectValue)
{
theDDL.SelectedIndex = i;
return true;
}
}
LogService.Write ("LocateDropDownList(" + theDDL.ID + ", " + SelectValue +")");
LogService.Write ("没有在DropDownList中查找到所要定位的特定值。");
return false;
}
catch(Exception e)
{
LogService.Write ("LocateDropDownList(System.Web.UI.WebControls.DropDownList theDDL, string SelectValue)");
LogService.Write ("在对DropDownList中的特定值进行定位时发生异常。");
LogService.Write (e.Message);
return false;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
//以下是数据库通用查询结果方法集
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根据SqlCommand对象查询执行数据库查询并返回结果
/// </summary>
/// <param name="sql">执行数据库查询的查询结果</param>
/// <param name="i">为1时表示执行ExecuteNonQuery(),为2时表示执行ExecuteReader(),为3时表示执行ExecuteScalar(),其它情况返回null,发生异常也返回null</param>
/// <returns>查询结果</returns>
/// **************************************************************************
public static object ExecQuery(SqlCommand SqlCmd, int i)
{
object result;
try
{
//打开数据库连接对象
SqlCmd.Connection.Open();
if(i == 1)
{
//执行数据库查询,成功后返回true
SqlCmd.ExecuteNonQuery();
result = true;
}
else if(i == 2)
{
//声明并使用SqlCommand的ExecuteReader方法创建一个SqlDataReader对象的实例,返回结果集
System.Data.SqlClient.SqlDataReader SqlDR = SqlCmd.ExecuteReader();
result = SqlDR;
}
else if(i == 3)
{
//执行数据库查询返回查询结果
result = SqlCmd.ExecuteScalar();
}
else
{
result = null;
}
SqlCmd.Connection.Close();
return result;
}
catch(Exception e)
{
if(SqlCmd.Connection.State.ToString() == "Open") SqlCmd.Connection.Close();
LogService.Write ("ExecQuery(SqlCommand SqlCmd, int i)");
LogService.Write ("在执行数据库查询时发生错误。");
LogService.Write (e.Message);
return null;
}
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根据SQL语句查询执行数据库查询并返回结果
/// </summary>
/// <param name="sql">执行数据库查询的查询结果</param>
/// <param name="i">为1时表示执行ExecuteNonQuery(),为2时表示执行ExecuteReader(),为3时表示执行ExecuteScalar(),其它情况返回null,发生异常也返回null</param>
/// <returns>查询结果</returns>
/// **************************************************************************
public static object ExecQuery(string sql, int i)
{
//声明并创建一个SqlConnection对象的实例
SqlConnection Connection = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
//声明并创建一个SqlCommand对象的实例
System.Data.SqlClient.SqlCommand SqlCmd = Connection.CreateCommand();
//给SqlCommand对象指定到某个SQL语句
SqlCmd.CommandText = sql;
return ExecQuery(SqlCmd, i);
}
/// **************************************************************************
/// END
/// **************************************************************************
/// **************************************************************************
/// BEIGIN
/// <summary>
/// 根据带参数的SQL语句查询执行数据库查询并返回结果
/// </summary>
/// <param name="sql">执行数据库查询的查询结果</param>
/// <param name="i">为1时表示执行ExecuteNonQuery(),为2时表示执行ExecuteReader(),为3时表示执行ExecuteScalar(),其它情况返回null,发生异常也返回null</param>
/// <returns>查询结果</returns>
/// **************************************************************************
public static object ExecQuery(string sql, string[]ParaTypes, string[] ParaNames, object[] ParaValues, int i)
{
//声明并创建一个SqlConnection对象的实例
SqlConnection Connection = new SqlConnection (com.unicafe.common.Configuration.GetDBConnectionString());
//声明并创建一个SqlCommand对象的实例
System.Data.SqlClient.SqlCommand SqlCmd = Connection.CreateCommand();
//给SqlCommand对象指定到某个SQL语句
SqlCmd.CommandText = sql;
SqlCmd.Parameters.Clear();
for(int j=0; j<ParaNames.Length; i++)
{
SqlCmd.Parameters.Add(ParaNames[j], ParaValues[j]);
}
return ExecQuery(SqlCmd, i);
}
/// **************************************************************************
/// END
/// **************************************************************************
/// <summary>
/// 加密字符串的方法
/// </summary>
/// <param name="PasswordString">要加密的字符串</param>
/// <param name="PasswordFormat">加密的类型</param>
/// <returns>加密后的字符串</returns>
public static string EncryptPassword(string PasswordString,string PasswordFormat)
{
string EncryptPassword;
if (PasswordFormat=="SHA1")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
}
else if (PasswordFormat=="MD5")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
}
else
{
EncryptPassword="PasswordString";
}
return EncryptPassword;
}
/// <summary>
/// 根据加密类型获得加密字符串
/// </summary>
/// <param name="EncryptString">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string EncryptString(string EncryptString)
{
string EncryptType=System.Configuration.ConfigurationSettings.AppSettings["EncryptType"].ToString();
string EncryptedString=CommonService.EncryptPassword(EncryptString,EncryptType);
return EncryptedString;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -