using System;
using System.Data;
using System.Web;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebConfig.Components
{
public class MyWeb : IDisposable
{
public SqlConnection con;
public SqlCommand myCommand;
public SqlDataAdapter myAdapter;
public SqlDataReader myreader;
public DataSet ds;
public DataRow dr;
public DataView dv;
public MyWeb()
{
Open();
}
~MyWeb()
{
//析构函数
}
public void Open()
{
// 打开数据库连接
if (con == null)
{
con = new SqlConnection(ConfigurationSettings.AppSettings["connectionstring"]);
con.Open();
}
}
/// 关闭数据库连接
public void Close()
{
if (con != null)
{
con.Close();
}
}
public void Dispose()
{
// make sure connection is closed
if (con != null)
{
con.Dispose();
con = null;
}
}
//执行Sql语句不需要返回值
public void ExecuteNonQuery(string sql_nonquery)
{
try
{
myCommand = new SqlCommand(sql_nonquery, con);
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
myCommand.Dispose();
}
}
public SqlDataReader dbread(string Sql_str)
{
try
{
myCommand = new SqlCommand(Sql_str,con);
return myCommand.ExecuteReader();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
myCommand.Dispose();
}
}
public DataView Fill(string str_Sql)
{
try
{
myAdapter = new SqlDataAdapter(str_Sql, con);
ds = new DataSet();
myAdapter.Fill(ds);
return ds.Tables[0].DefaultView;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
myAdapter.Dispose();
ds.Clear();
}
}
public DataView Fill(string tabname, string str_Sql)
{
try
{
myAdapter = new SqlDataAdapter(str_Sql, con);
ds = new DataSet();
myAdapter.Fill(ds, tabname);
return ds.Tables[tabname].DefaultView;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
myAdapter.Dispose();
ds.Clear();
}
}
public DataView Fill(string str_Sql, int StartIndex, int PageSize, string tabname)
{
try
{
myAdapter = new SqlDataAdapter(str_Sql, con);
ds = new DataSet();
myAdapter.Fill(ds, StartIndex, PageSize, tabname);
return ds.Tables[tabname].DefaultView;
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
myAdapter.Dispose();
ds.Clear();
}
}
//首页显示前几条的代码
public void FillDataList(System.Web.UI.WebControls.DataList DataListName, string datalist_sql, string tablename)
{
try
{
DataListName.DataSource = Fill(tablename, datalist_sql);
DataListName.DataBind();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
public void FillDatagrid(System.Web.UI.WebControls.DataGrid DataGridName, string datalist_sql, string tablename)
{
try
{
DataGridName.DataSource = Fill(tablename, datalist_sql);
DataGridName.DataBind();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
public void FillDataRepeater(System.Web.UI.WebControls.Repeater RepeaterName, string datalist_sql, string tablename)
{
try
{
RepeaterName.DataSource = Fill(tablename, datalist_sql);
RepeaterName.DataBind();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
//返回表记录条数
public int GetRowCount(string str_Sql)
{
this.Fill(str_Sql);
try
{
return ds.Tables[0].Rows.Count;
}
catch
{
return 0;
}
finally
{
ds.Clear();
}
}
//截取字符
public string GetSub(string title_string, int num)
{
try
{
if (title_string.Length < num)
{
return title_string;
}
else
{
return title_string.Substring(1, num);
}
}
catch
{
return title_string;
}
}
//用数据填充下拉选择框
public void Pack_DropList(System.Web.UI.WebControls.DropDownList mydroplist, string sql_str, string TextFild, string ValueFild, string start_Text)
{
try
{
dv = this.Fill("MyDrop", sql_str);
mydroplist.DataSource = dv;
mydroplist.DataTextField = TextFild;
mydroplist.DataValueField = ValueFild;
mydroplist.DataBind();
this.Dispose();
mydroplist.Items.Insert(0, "-" + start_Text + "-");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
dv.Dispose();
}
}
public void Pack_DropList(System.Web.UI.WebControls.DropDownList mydroplist, string sql_str, string TextFild, string ValueFild)
{
try
{
dv = this.Fill("MyDrop", sql_str);
mydroplist.DataSource = dv;
mydroplist.DataTextField = TextFild;
mydroplist.DataValueField = ValueFild;
mydroplist.DataBind();
this.Dispose();
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
dv.Dispose();
}
}
//**************************************************************************************************************************
//S_Type 全选按纽1全选2全不选
//System.Web.UI.WebControls.DataList mydatalist 所属的控件
//myCheckBox 选择框的名称
//System.Web.UI.WebControls.Button Select_All_true 全选按纽名称
//System.Web.UI.WebControls.Button Select_All_false 取消全选按纽名称
public void Select_All(int S_Type, System.Web.UI.WebControls.DataList mydatalist, string myCheckBox, System.Web.UI.WebControls.Button Select_All_true, System.Web.UI.WebControls.Button Select_All_false)
{
try
{
switch (S_Type)
{
case 1:
{
for (int i = 0; i < mydatalist.Items.Count; i++)
{
((System.Web.UI.WebControls.CheckBox)mydatalist.Items.FindControl("" + myCheckBox + "")).Checked = true;
}
Select_All_true.Visible = false;
Select_All_false.Visible = true;
break;
}
case 2:
{
for (int i = 0; i < mydatalist.Items.Count; i++)
{
((System.Web.UI.WebControls.CheckBox)mydatalist.Items.FindControl("" + myCheckBox + "")).Checked = false;
}
Select_All_true.Visible = true;
Select_All_false.Visible = false;
break;
}
default:
break;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//任意多选项
//System.Web.UI.WebControls.DataList mydatalist 所属控件名称
//myCheckBox 选择框的名称
//IDlable 关键字的ID
public string Select_Random(System.Web.UI.WebControls.DataList mydatalist, string myCheckBox)
{
try
{
StringBuilder Selected_ck = new StringBuilder();
for (int i = 0; i < mydatalist.Items.Count; i++)
{
if (((System.Web.UI.WebControls.CheckBox)mydatalist.Items.FindControl("" + myCheckBox + "")).Checked == true)
{
Selected_ck.Append(mydatalist.DataKeys.ToString());
Selected_ck.Append(",");
}
}
return Selected_ck.ToString().TrimEnd(',');
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//**************************************************************************************************************************
//S_Type 全选按纽1全选2全不选
//System.Web.UI.WebControls.DataGrid mydatagrid 所属的控件
//myCheckBox 选择框的名称
//System.Web.UI.WebControls.Button Select_All_true 全选按纽名称
//System.Web.UI.WebControls.Button Select_All_false 取消全选按纽名称
public void Select_All(int S_Type, System.Web.UI.WebControls.DataGrid mydatagrid, string mycheck, System.Web.UI.WebControls.Button Select_All_true, System.Web.UI.WebControls.Button Select_All_false)
{
try
{
switch (S_Type)
{
case 1:
{
for (int i = 0; i < mydatagrid.Items.Count; i++)
{
((System.Web.UI.WebControls.CheckBox)mydatagrid.Items.FindControl("" + mycheck + "")).Checked = true;
}
Select_All_true.Visible = false;
Select_All_false.Visible = true;
break;
}
case 2:
{
for (int i = 0; i < mydatagrid.Items.Count; i++)
{
((System.Web.UI.WebControls.CheckBox)mydatagrid.Items.FindControl("" + mycheck + "")).Checked = false;
}
Select_All_true.Visible = true;
Select_All_false.Visible = false;
break;
}
default:
break;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//任意多选项
//System.Web.UI.WebControls.DataGrid mydatagrid 所属控件名称
//myCheckBox 选择框的名称
//IDlable 关键字的ID
public string Select_Random(System.Web.UI.WebControls.DataGrid mydatagrid, string myCheckBox)
{
try
{
StringBuilder Selected_ck = new StringBuilder();
for (int i = 0; i < mydatagrid.Items.Count; i++)
{
if (((System.Web.UI.WebControls.CheckBox)mydatagrid.Items.FindControl("" + myCheckBox + "")).Checked == true)
{
Selected_ck.Append(mydatagrid.DataKeys.ToString());
Selected_ck.Append(",");
}
}
return Selected_ck.ToString().TrimEnd(',');
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//****************************************系统小函数*************************************
public string GetDataRandom()
{
string strData = DateTime.Now.ToString();
strData = strData.Replace(":", "");
strData = strData.Replace("-", "");
strData = strData.Replace(" ", "");
Random r = new Random();
strData = strData + r.Next(100000);
return strData;
}
//**********************************上传文件******************************************
/// 限制文件大小以及文件类型,把上传文件名变换为16位随机数上传,限制:文件大小不能超过filesize值,文件类型,文件不能重名上传文件
/// <param name="filesize">限制文件大小,比如"2000000",也即是2mb</param>
/// <param name="filetype">限制文件类型,比如:"rar,zip"</param>
/// <param name="path">上传文件路径,相对路径,比如:downloads/wufeng/</param>
/// <param name="fl_Name">选择上传文件HtmlInputFile控件id号</param>
/// <param name="lbl_Error">上传提示错误Label控件id号</param>
public string UpFileRandomSizeType(int filesize, string filetype, string path, System.Web.UI.HtmlControls.HtmlInputFile fl_Name, System.Web.UI.WebControls.Label lbl_Error)
{
try
{
string filename="";
if (GetStrCount(filetype, GetLastStr(fl_Name.PostedFile.FileName, ".").ToLower()) == 0)
{
lbl_Error.Text = this.MsgAlert("请上传" + filetype + "文件格式文件");
throw (new Exception());
}
if (fl_Name.PostedFile.ContentLength > filesize)
{
lbl_Error.Text = this.MsgAlert("文件大小限定在" + filesize + "MB以内");
throw (new Exception());
}
string strPath = System.Web.HttpContext.Current.Server.MapPath(path);
filename = GetDataRandom();
filename = filename + "." + GetLastStr(fl_Name.PostedFile.FileName, ".");
string strFullPath = strPath + filename;
if (File.Exists(strFullPath))
{
lbl_Error.Text = this.MsgAlert("该文件已经存在,请改名再上传");
throw (new Exception());
}
fl_Name.PostedFile.SaveAs(strFullPath);
return filename;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
public int GetStrCount(string strOriginal, string strSymbol)
{
int count = 0;
for (int i = 0; i < (strOriginal.Length - strSymbol.Length + 1); i++)
{
if (strOriginal.Substring(i, strSymbol.Length) == strSymbol)
{
count = count + 1;
}
}
return count;
}
/// 获得某个字符串在另个字符串第一次出现时前面所有字符(strOriginal要处理的字符strSymbol符号)
public string GetFirstStr(string strOriginal, string strSymbol)
{
int strPlace = strOriginal.IndexOf(strSymbol);
if (strPlace != -1)
strOriginal = strOriginal.Substring(0, strPlace);
return strOriginal;
}
/// 获得某个字符串在另个字符串最后一次出现时后面所有字符(strOriginal要处理的字符strSymbol符号)
public string GetLastStr(string strOriginal, string strSymbol)
{
int strPlace = strOriginal.LastIndexOf(strSymbol) + strSymbol.Length;
strOriginal = strOriginal.Substring(strPlace);
return strOriginal;
}
public string StringFilter(string strValue)
{
strValue = strValue.Replace("/'", "");
strValue = strValue.Replace("/"", "");
return strValue.Trim();
}
public string ChangeHtml(string strValue)
{
strValue = strValue.Replace(">", ">");
strValue = strValue.Replace("<", "<");
strValue = strValue.Replace("/n", "<br>");
strValue = strValue.Replace(" ", " ");
strValue = strValue.Replace("<br>", "<br>");
return strValue;
}
public string MsgAlert(string strValue)
{
return String.Format(@"<script>alert('{0}')</script>", this.StringFilter(strValue));
}
public string MsgAlert(string strValue, string strUrl)
{
return String.Format(@"<script>alert('{0}');location.href='{1}'</script>", this.StringFilter(strValue), strUrl);
}
//选择那种方法加密密码
public static string EnPassWord(string Password, int Format)
{
try
{
string str = "";
switch (Format)
{
case 0:
str = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "SHA1");
break;
case 1:
str = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
break;
}
return str;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//*****************************判断用户登陆******************************************
public void Logon(char ch_A, object session, System.Web.UI.Page page,string N_page,string Y_page)// 判断是否有权限登陆该页面
{
try
{
string ErrMsg="";
if (session == null)
{
ErrMsg = "你还没有登陆.";
page.Response.Redirect(N_page + "?ErrMsg = " + ErrMsg);
}
else
{
if (session.ToString().IndexOf(ch_A) != -1 || session.ToString().IndexOf(ch_A) != -1)
{
//没有权限登陆该页面
ErrMsg = "你无权登陆这个页面.";
page.Response.Redirect(N_page + "?ErrMsg = " + ErrMsg);
}
else
{
page.Response.Redirect(Y_page);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//会员登陆判断
public bool CheckLogin(string sqlstr,int type)
{
try
{
myreader = this.dbread(sqlstr);
if (myreader.Read())
{
System.Web.HttpCookie WebCookie = new System.Web.HttpCookie("WebUser");
WebCookie.Values.Add("UserID",myreader[0].ToString());
WebCookie.Values.Add("UserName",myreader[1].ToString());
WebCookie.Values.Add("UserType", myreader[2].ToString());
switch (type)
{
case 1://一年
WebCookie.Expires = DateTime.Now.AddDays(365);
break;
case 2://一天
WebCookie.Expires = DateTime.Now.AddDays(1);
break;
case 3://一个星期
WebCookie.Expires = DateTime.Now.AddDays(7);
break;
case 4://一个月
WebCookie.Expires = DateTime.Now.AddDays(30);
break;
default://一次性登陆
WebCookie.Expires = DateTime.Now.AddMinutes(30);
break;
}
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}