?? 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 + -