关闭

網站類代碼

736人阅读 评论(0) 收藏 举报

using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Diagnostics;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.Web.Security;

namespace staff.Components
{
 /// <summary>
 /// Summary description for Config.
 /// </summary>
 public class Config
 {
  public SqlConnection cnn;   
  public SqlTransaction tran;  
  public Config()
  {
  }

  //----------------------訪問數據庫----------------------------------------------------
  public void open()
  {
   string dataconnection =  System.Configuration.ConfigurationSettings.AppSettings["Sql"];
   cnn=new SqlConnection(dataconnection);
   if (cnn.State != ConnectionState.Open)
    cnn.Open();
  }
  

  public bool IsDate(string str)
  {
   bool result = true;
   DateTime date;
   try
   {
    date = Convert.ToDateTime(str);
   }
   catch
   {
    result = false;
   }

   return result;
  }

  public bool IsInt32(string str)
  {
   bool result = true;
//   int vInt32;
//   try
//   {
//    vInt32 = Convert.Int32(str);
//   }
//   catch
//   {
//    result = false;
//   }

   return result;
  }

 

  public void close()//關閉數據連接;
  {
   if (cnn.State == ConnectionState.Open)    
    cnn.Close();
  }  
   

  public string getsysdatetime()
  {
   open();
            string nowstr = "";
   SqlCommand cmd = new SqlCommand(" select getdate() as nowdate ",cnn);
   SqlDataReader dr = cmd.ExecuteReader();
   while(dr.Read())
   {
               nowstr = dr["nowdate"].ToString();
   }
           return(nowstr);
   dr.Close();
   cmd.Dispose();
   cnn.Close();
  }
  

  public string getdate()
  {
   open();
   string nowstr = "";
   SqlCommand cmd = new SqlCommand("select convert(nvarchar(10),getdate(),111) as date ",cnn);
   SqlDataReader dr = cmd.ExecuteReader();   
   while(dr.Read())
   {
    nowstr = dr["date"].ToString();
   }
   return(nowstr);
   dr.Close();
   cmd.Dispose();
   cnn.Close();
  }

  public void AddDropList(DropDownList drop1 ,string sql_txt,string textfield,string valuefield,string tabname)
  {
   //加入表tabname上的字段到指定的drop中.
   drop1.Items.Clear();
   this.open();
   SqlDataAdapter ada= new SqlDataAdapter(sql_txt,cnn);
   DataSet ds = new DataSet();
   ada.Fill(ds,tabname);
   drop1.DataValueField = valuefield;
   drop1.DataTextField = textfield;
   drop1.DataSource = ds;
   drop1.DataBind();
   ada.Dispose();
   cnn.Close();   
  }

 

  public void createMenu()
  {
   DateTime now = new DateTime();
   string nowstring = now.ToString("yyyy/MM/dd");
   this.open();
   SqlCommand cmd10 = new SqlCommand("select top 1 * from Module ",cnn);    
   SqlDataReader dr;
   dr = cmd10.ExecuteReader();
   string pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("1","SHA1");
   if (!dr.Read())   
   {  
    this.close();
    this.open();
    SqlCommand cmm=new SqlCommand("Create_menu",cnn);
    cmm.CommandType=CommandType.StoredProcedure;
    cmm.Parameters.Add(new SqlParameter("@password",SqlDbType.NVarChar,40));        
    cmm.Parameters["@password"].Value =  pass.Trim();
    cmm.ExecuteNonQuery();
    cmm.Dispose();
    this.close();
   }
   cmd10.Dispose();
   dr.Close();
   this.close();    
  }

  public void Alert(string str_Message,Page page)//write message
  {
   page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
  }


  public void DropListForQuery(DropDownList drop1 ,string sql_txt,string textfield,string valuefield,string tabname)
  {
   ListItem item;
   
   drop1.Items.Clear();
   this.open();
   
   SqlDataAdapter ada= new SqlDataAdapter(sql_txt,cnn);
   DataSet ds = new DataSet();
   ada.Fill(ds,tabname);

   item=new ListItem("","");
   item.Selected=true;
   drop1.Items.Add(item);
   foreach(DataRow drRow in ds.Tables[0].Rows)
   {
    item=new ListItem(drRow[textfield].ToString(),drRow[valuefield].ToString());
    drop1.Items.Add(item);
   }
      
   ada.Dispose();
   cnn.Close();   
  } 

  public bool batchDel(DataGrid DataGrid1,string checkBoxName,int num,string sqltxt,string delstring)
  { 
   bool result = true;
   CheckBox isChecked; //設定一個CheckBox;
   this.open();
   string sql_Del="";
   SqlCommand cmd = new SqlCommand();   
   SqlTransaction myTran = null;     
   try
   {    
    myTran = cnn.BeginTransaction();
    cmd.Connection = cnn; 
    cmd.Transaction = myTran;
    foreach(DataGridItem dataitem in DataGrid1.Items)
    {
     
     isChecked = (CheckBox)dataitem.FindControl(checkBoxName);
     if (isChecked.Checked)
     {
      sql_Del = delstring+dataitem.Cells[num].Text.ToString().Trim(); 
      cmd.CommandText = sql_Del; 
      cmd.ExecuteNonQuery();            
     }      
    }
    result = true;
    myTran.Commit();
    this.dgdatabind(DataGrid1,sqltxt);
   }
    
    
   catch(System.Data.SqlClient.SqlException e)
   {
    result = false;
    //myTran.Rollback();   
   }  
 
   return(result);
   cmd.Dispose();
   cnn.Close();   
  }


  public static SqlConnection Create()
  {
   return new SqlConnection("server=rd00-88;database=hr;uid=sa;pwd=;");
  }

  public ArrayList getChecked(System.Web.UI.WebControls.DataGrid Datagrid1,string checkNmae,int n)
  {
   ArrayList saveIdChecked=new ArrayList();
   CheckBox isChecked;
   //遍曆每一行
   foreach(DataGridItem dataItem in Datagrid1.Items)
   {
    //獲取checkbox
    isChecked=(CheckBox)dataItem.FindControl(checkNmae);
    //取Id到arraylist
    if(isChecked.Checked==true)
    {
     string idChecked=dataItem.Cells[n].Text;
     saveIdChecked.Add(idChecked);
    }
   }
   return saveIdChecked;
  }

  public void justclose(Page page)//write message
  {
   page.RegisterStartupScript("","<script>window.close();</script>");
  }


  public void WriteStatus(string str,Page page)//write message
  {
   page.RegisterStartupScript("","<script>window.status ='"+ str +"';</script>");
  }


  public DataSet Get_ds(string sql_txt)//取得數據表資料
  {
   open();
   SqlDataAdapter ada=new SqlDataAdapter(sql_txt,cnn);
   DataSet ds=new DataSet();
   ada.Fill(ds);
   cnn.Close();
   return ds;
  }


  public bool ifcandelete(string sqltxt,string keystring)//是否可以刪除,在刪除前做判斷
  {          
   this.open();
   SqlCommand cmd = new SqlCommand(sqltxt,cnn);
   SqlDataReader dr = cmd.ExecuteReader();
   return(dr.Read()); 
   dr.Close();
   cmd.Dispose();
   cnn.Close();   
  }

  public bool EditDB(string strSql)
  {
   int intX;
   if (strSql ==null && strSql.Length<1)
   {
    return false;
   }
   try
   {
    open();   
    SqlCommand cmd = new SqlCommand(strSql,cnn);
    intX=cmd.ExecuteNonQuery();
    if (intX>0 )
    {
     return true;
    }
    else
    {
     return false;

    }
    
   }
   catch(System.Data.SqlClient.SqlException ex1)
   {
    return false;

   }
   catch(System.Data.OleDb.OleDbException ex2)
   {
    return false;

   }
   catch(System.Exception ex3)
   {
    return false;
   }

  }
  public void dgdatabind(DataGrid dgrid1, string sql_txt)//綁定數據源
  {
   dgrid1.DataSource = this.Get_ds(sql_txt);
   //if (dgrid1.CurrentPageIndex >0)
    //dgrid1.CurrentPageIndex = 0;
   dgrid1.DataBind();
  }

  public void getright(string userno,string menuno,out bool[] rightValue)
  {

   //取得用戶(userno)在模塊(menuno)下的權限
   //right01表示新增
   //right02表示修改
   //right03表示刪除
   //right04表示查詢
   //right05表示打印
   //right06表示確認
   //right07表示反確認
   //right08表示模塊
   rightValue = new bool[8]{false,false,false,false,false,false,false,false}; 
   if (userno.Trim().ToUpper() == "SUPER_ADMIN")
   {
    for (int i =0;i<=7;i++)
     rightValue[i] = true;
   }
   else
   {

    this.open();
    SqlCommand cmd = new SqlCommand("select top 1  right01,right02,right03,right04,right05,right06,right07,right08 from [Right] where userno='"+ userno.Trim() +"' and ModuleNo='"+ menuno.Trim() +"' ",cnn);
    SqlDataReader dr = cmd.ExecuteReader();        
    while(dr.Read())
    {    
     for (int i=0;i<=7;i++)
     {  
      int n = i + 1;
      rightValue[i] = (dr["right0"+n.ToString()].ToString().Trim()=="True")?true:false;   
     }   
    }  
    dr.Close();
    cmd.Dispose();
    this.close();
   }
  }


  public bool UserLogon(string userno,string password,out string fac,out string departname,out string departno,out string LogonUserNmae)//檢查是否合法用戶
  {  
   bool result = false;
   open();
   string pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"SHA1");
   SqlCommand cmd = new SqlCommand("select   top 1 a.* ,b.department_no,b.factory,c.department_name from [User] a left join trans_staff b on a.userno=b.staff_no left join department c on b.department_no=c.department_no where  a.userno='"+ userno +"' and a.password='"+ pass +"'",cnn);
   SqlDataReader dr = cmd.ExecuteReader();
   fac = "";
   departname = "";
   departno = "";
   LogonUserNmae = "";
   while (dr.Read())
   {
                fac = dr["factory"].ToString();
    if (fac =="")
    {
     fac = "A";
    }
    departname = dr["department_name"].ToString().Trim();
    if (departname =="")
    {
     departname = "電腦室";
    }

    departno = dr["department_no"].ToString().Trim();
    if (departno =="")
    {
     departno = "010115";
    }

    LogonUserNmae = dr["UserNa"].ToString().Trim();
    if (LogonUserNmae == "")
    {
                  LogonUserNmae = "Admin";
    }

    result = true;
   }
   return(result); 
   dr.Close();
   cmd.Dispose();
   cnn.Close();   
  }


  public bool grtmenuright(string userno,string menuno)
  {

   bool enable = false;
   if (userno.Trim().ToUpper() == "SUPER_ADMIN")
   {
    enable = true;
   }
   else
   {
    this.open();
    SqlCommand cmd = new SqlCommand("select top 1 right08 from [Right] where userno='"+ userno.Trim() +"' and ModuleNo='"+ menuno.Trim() +"' ",cnn);
    SqlDataReader dr = cmd.ExecuteReader(); 
    while(dr.Read())
    {   
     string menuright  = dr["right08"].ToString();
     if ((menuright.ToString().Trim() =="True") || (menuright.ToString().Trim() =="1"))
      enable = true;
    }     
    dr.Close();
    cmd.Dispose();
    this.close();
   }
   return(enable);
  }


  

  public void  Addxueli(string procname,string xueli_no,string xueli_name,string createuser,string updateuser,int flag)
  {
   open();
   DateTime now  = DateTime.Now;    
   SqlCommand cmm=new SqlCommand(procname,cnn);
   cmm.CommandType=CommandType.StoredProcedure;

   cmm.Parameters.Add(new SqlParameter("@xueli_no",SqlDbType.NVarChar,3));
   cmm.Parameters.Add(new SqlParameter("@xueli_name",SqlDbType.NVarChar,20));
   cmm.Parameters.Add(new SqlParameter("@createuser",SqlDbType.NVarChar,12));
   cmm.Parameters.Add(new SqlParameter("@createdate",SqlDbType.DateTime,8));
   cmm.Parameters.Add(new SqlParameter("@updateuser",SqlDbType.NVarChar,12));
   cmm.Parameters.Add(new SqlParameter("@updatedate",SqlDbType.DateTime,8));
   cmm.Parameters.Add(new SqlParameter("@flag",SqlDbType.TinyInt,1));

   cmm.Parameters["@xueli_no"].Value = xueli_no.Trim();
   cmm.Parameters["@xueli_name"].Value = xueli_name.Trim();

   cmm.Parameters["@createuser"].Value = createuser.Trim();
   cmm.Parameters["@createdate"].Value = now;
   cmm.Parameters["@updateuser"].Value = updateuser.Trim();
   cmm.Parameters["@updatedate"].Value = now;
   cmm.Parameters["@flag"].Value = flag;

   cmm.ExecuteNonQuery();
   cmm.Dispose();
   cnn.Close();
  }


  public bool ifunique(string sqltxt)
  {
   open();   
   SqlCommand cmd = new SqlCommand(sqltxt,cnn);
   SqlDataReader dr = cmd.ExecuteReader();
   return(dr.Read()); 
   dr.Close();
   cmd.Dispose();
   cnn.Close();  
  }


  public void IfHaveThisNoOrName(string sqltxt,string readCol,out string pubName)
  {
   this.open();
   SqlCommand cmd = new SqlCommand(sqltxt,cnn);
   SqlDataReader dr = cmd.ExecuteReader();
   pubName = "";
   if (dr.Read())
   {
    pubName = dr[readCol].ToString();
   }   
   dr.Close();
   cmd.Dispose();
   cnn.Close();  
  }

  private SqlCommand createcommand(string ProcName,SqlParameter [] prams)
  {
   open();
   SqlCommand cmm=new SqlCommand(ProcName,cnn);
   cmm.CommandType=CommandType.StoredProcedure;
   if (prams!=null)
   {
    foreach(SqlParameter parameter in prams)
     cmm.Parameters.Add(parameter);
   }
   
   cmm.Parameters.Add(
    new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,string.Empty,DataRowVersion.Default,null)
    );
   return cmm;  
  }


  public void btngoNum(DataGrid grid1,TextBox txtGoPage,Page page)
  {
   //頁碼跳轉   
   if (!regex_int(txtGoPage.Text.Trim()))
   {
    Alert("請輸入正整數!",page);
   }
   else
   {
    if (grid1.PageCount<Int32.Parse(txtGoPage.Text.Trim())) 
    {
                   Alert("你輸入的頁數已大於最大頁,請重新輸入!!!",page);
    }
    else
    {
     if(txtGoPage.Text.Trim().Length>0)
     {
      int PageI=Int32.Parse(txtGoPage.Text.Trim())-1;
      if (PageI >=0 && PageI < (grid1.PageCount))
       grid1.CurrentPageIndex = PageI ;    
     }   
    }
   }
  }

  public void FirstNextAndPrewLast(object sender, DataGrid grid1)
  {
   //得到LinkButton的Id
   string arg = ((LinkButton)sender).CommandArgument;                           
   switch(arg)
   {
    case ("next"):
     if (grid1.CurrentPageIndex < (grid1.PageCount - 1))
      grid1.CurrentPageIndex ++;         
     break;
    case ("prev"):
     if (grid1.CurrentPageIndex > 0)
      grid1.CurrentPageIndex --;
     break;
    case ("fist"):
        grid1.CurrentPageIndex=0;
     break;
    case ("last"):
        grid1.CurrentPageIndex = (grid1.PageCount - 1);
     break;
    default:
     //本頁值
       grid1.CurrentPageIndex = Convert.ToInt32(arg);
     break;
   }
   //綁定數據 
  }           

  public void showSattuePage(DataGrid grid1,Label lblCurrentIndex,Label lblPageCount)
  {
   lblCurrentIndex.Text = "當前為第:"+(grid1.CurrentPageIndex + 1).ToString()+"頁";
   lblPageCount.Text = "共:"+grid1.PageCount.ToString()+"頁";
  }

  public void RunProc(string ProcName,SqlParameter [] prams)
  {
   SqlCommand cmm=createcommand(ProcName,prams);
   cmm.ExecuteNonQuery();
   close();
  }
  
  public  bool regex_text(string text)//數字 字母 下劃線
  {
   return Regex.IsMatch(text, @"^/w+$");
  }

  public  bool regex_HTML(string text)//HTML
  {
   return Regex.IsMatch(text, @"^/</s*(/S+)(/s[^>]*)?>[/s/S]*</s*///1/s*>/$");
  }

  public  bool regex_URL(string text)//URL
  {
   return Regex.IsMatch(text, @"^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$");
  }

  public  bool regex_Email(string text)//E_Mail
  {
   return Regex.IsMatch(text, @"^[//w-]+(//.[//w-]+)*@[//w-]+(//.[//w-]+)+$");
  }

  public  bool regex_int(string text)//正整數 int
  {
   return Regex.IsMatch(text,@"^[0-9]*[1-9][0-9]*$");
  }

  public  bool regex_floag(string text)//浮點數
  {
   return Regex.IsMatch(text,@"^(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*))$");
  }

 

 }
}

2:@@@

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Erp
{
 /// <summary>
 /// Summary description for Erp.
 /// </summary>
 public class Erp
 {

  public string Connectionstring;
  public SqlConnection conn;

       public void Open()
       {
        string dataconnection =  System.Configuration.ConfigurationSettings.AppSettings["Sql"];
  conn=new SqlConnection(dataconnection);
        if (conn.State != ConnectionState.Open)
        conn.Open();
        } 

  public void Close()
  {
   if(conn.State==ConnectionState.Open)
   conn.Close()  ;
  }
    
  public DataSet Data_ds(string Sql_txt)
  {
   this.Open();   
   SqlDataAdapter sdr = new SqlDataAdapter(Sql_txt, conn);
   DataSet ds = new DataSet();
   sdr.Fill(ds);
   return ds;
  }
  public void databind_drg(DataGrid drg, string Sql_txt)
  {
   this.Open();
   drg.DataSource = this.Data_ds(Sql_txt);
   drg.DataBind();
   this.Close();
  }
  public void databind_dlst(DataList dlst, string Sql_txt)
  {
   dlst.DataSource = this.Data_ds(Sql_txt);
   dlst.DataBind();
   this.Close();
  }
  public void data_Chg(string Sql_txt)
  {
   this.Open();
   SqlCommand cmd = new SqlCommand(Sql_txt, conn);
   cmd.ExecuteNonQuery();
   cmd=null;
   this.Close();
  }
 }
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:114741次
    • 积分:2009
    • 等级:
    • 排名:第19259名
    • 原创:74篇
    • 转载:37篇
    • 译文:2篇
    • 评论:2条