现在的位置: 首页 > 综合 > 正文

[原创]使用自定义类库实现中间件的功能

2014年09月05日 ⁄ 综合 ⁄ 共 7581字 ⁄ 字号 评论关闭
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using WebTools;

namespace News
{
/// <summary>
/// TestDll 的摘要说明。
/// </summary>
public class TestDll : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public Tools Wtools=new Tools();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Write("<h3>DLL组件引用测试</h3>");
Response.Write("Bin/Dll测试(读取DLL中的参数):"+Wtools.connStr.ConnectionString+"<BR/>");
Response.Write("Bin/Dll测试(执行DLL中的加密函数):<br/>");
string SourceInfo="admin";
string EndInfo=Wtools.PswdFormat(SourceInfo,"MD5");
Response.Write("源字串:"+SourceInfo);
Response.Write("<br/>");
Response.Write("加密字串:"+EndInfo);
string msg="弹出信息提示";
string megnotes=Wtools.ShowMsgBox(msg,"close");
Response.Write(megnotes);

string sql1="delete user_info where id=17";
string errorstr1=null;
errorstr1=Wtools.SqlCmd(sql1,Wtools.connStr);
if (errorstr1==null)
{
Response.Write("删除成功!");
}

string sql2="select * from user_info order by id desc";

DataSet ds1=Wtools.sqlDataSet(sql2,Wtools.connStr,"user_info");//调用自定义类库的方法

DataGrid1.DataSource=ds1.Tables["user_info"].DefaultView;
DataGrid1.DataBind();

string sql="select * from user_info order by id desc";
SqlDataReader read;
read=Wtools.SqlReader(sql,Wtools.connStr);
string nr="";
while(read.Read())
{
nr+="<table><tr><td>";
nr+=read.GetValue(0).ToString();
nr+="</td><td>";
nr+=read.GetValue(1).ToString();
nr+="</td><td>******</td><td>";
nr+=read.GetValue(3).ToString();
nr+="</td><td>";
nr+="<a href=modify.aspx?id="+read.GetValue(0)+">修改</a>";
nr+="&nbsp;&nbsp;&nbsp;&nbsp;<a href=manage.aspx?commandtype=del&id="+read.GetValue(0)+">删除</a>";
nr+="</td></tr></table>";

}

Response.Write(nr);
read.Close();

}//显示数据库

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

自定义类库:

namespace WebTools
{
using System;
using System.IO;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mail;
    public class Tools{
        public SqlConnection connStr=new SqlConnection("server=127.0.0.1;uid=netboyc;pwd=123456;database=aspx");
        //站点参数
       
        public string ChangeStr(string str,int num){
        string add_str="";
        if (str.Length>num) add_str="..";
        string addstr=str+"                                                        ";
        return addstr.Substring(0,num).Trim().Replace(" ","&nbsp;").Replace("/n","<br/>")+add_str;
        }//文本字符替换
       
        public string CheckStr(string strname,string str,int minnum,int maxnum){
        string estr="";
        byte[] bstr = System.Text.Encoding.Default.GetBytes(str);
        if((bstr.Length>maxnum)||(bstr.Length<minnum)) estr+="字符数目不符,应该在"+minnum+"-"+maxnum+"之间!";
        Char[] spestr={'?','=','&','+','"','/'',';',',','//','<','>','|'};
        if(str.IndexOfAny(spestr)>-1) estr+="含有非法字符:问号 等号 加号 分号 单引号 双引号  // () <> | &等!";
        //if(System.Web.HttpUtility.HtmlEncode(str).IndexOfAny(spestr)>-1) estr+="含有其他不允许的隐藏字符或HTML标签!";
            if(estr!=""){
                return "“"+strname+"” :"+estr+"<br/>";
            }else{
                return estr;
            }
        }//客户输入检查
       
        public string PswdFormat(string str,string format){       
            string returnstr="";
            if (format=="SHA1")returnstr= System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str ,"SHA1");
            if (format=="MD5")returnstr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str ,"MD5");
            return returnstr;
        }//ASP.NET的加密方式
       
        public SqlDataReader SqlReader(string sql,SqlConnection connstr){
        SqlDataReader sqldr=null;
        SqlCommand cmd=new SqlCommand(sql,connstr);
        if (cmd.Connection.State.ToString()=="Closed") cmd.Connection.Open();
            try{
            sqldr=cmd.ExecuteReader();
            }catch(Exception e){
            if (e!=null) sqldr=null;
            }
        return sqldr;
        }//数据库读取连接

        public DataSet sqlDataSet(string sql,SqlConnection connstr,string sqlTable)
        {   
           
            SqlDataAdapter sda=new SqlDataAdapter(sql,connstr);
            DataSet ds=new DataSet();
            //try
            //{
                //填充数据集
                sda.Fill(ds,sqlTable);
               
            //}
            //catch(Exception e)
            //{
               
            //}
            //finally{}
            return ds;           
           
        }//DataSet操作数据库
       
       
        public string SqlCmd(string sql,SqlConnection connstr){
        string errorstr=null;
        SqlCommand sqlcmd= new SqlCommand(sql,connstr);
        if (sqlcmd.Connection.State.ToString()=="Open") sqlcmd.Connection.Close();
        sqlcmd.Connection.Open();
            try{
            sqlcmd.ExecuteNonQuery();
            }catch(Exception e){
            if (e!=null) errorstr=e.ToString();
            }
        sqlcmd.Connection.Close();
        return errorstr;
        }//数据库操作连接
       
        public void Wfile(string filename,string filecont){
        FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write);
        StreamWriter wfile=new StreamWriter(fs);
        wfile.BaseStream.Seek(0,SeekOrigin.End);
        wfile.WriteLine(filecont);
        wfile.Flush();
        wfile.Close();
        }//写入记录文件
       
        public string Rfile(string filename,int startline,int endline){
        string ReadFile="";
        FileStream fs=new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Read);
        StreamReader rfile=new StreamReader(fs);
        rfile.BaseStream.Seek(0,SeekOrigin.Begin);
        for (int i=0;i<=endline;i++){
            if ((i>=startline)&&(rfile.Peek()>-1)){
                ReadFile+=rfile.ReadLine()+"/n";
            }else{
                if (rfile.Peek()>-1){
                    rfile.ReadLine();
                }else{
                break;
                }
            }
        }
        return ReadFile;
        }//读取文件
       
        public void SendMail(string fromad,string sendad,string msubject,string mbody){
        MailMessage mm = new MailMessage();
        mm.From = fromad;//发送地址
        mm.To = sendad;//目的地址
        //mm.Bcc = "";//密送地址
        //mm.Cc = "";//抄送地址
        mm.Subject = msubject;//邮件主题
        mm.Body =mbody;//邮件内容
        mm.BodyFormat = MailFormat.Html;//正文格式
        mm.Priority =MailPriority.High;//优先级
        //mm.Attachments.Add(new MailAttachment(" c://lixi.txt"));//附件
        SmtpMail.Send(mm);
        }//邮件发送
       
        public string ShowMsgBox(string Msg,string Action){
        string AddAction="";
            if (Action!=null){
                switch(Action){
                case "back":
                    AddAction="window.history.back(1);";
                    break;
                case "close":
                    AddAction="top.close();";
                    break;
                default:
                    AddAction="location.href='"+Action+"';";
                    break;
                }
            }
        return "<script language='javascript'>alert('"+Msg+"');"+AddAction+"<"+"/script>";
        }//提示信息
       
       
        public string PutToHtml(string HttpFile,string SaveFile){
            string ReturnMsg="";
            if((HttpFile!="")&&(SaveFile!="")){
                WebClient Wclient=new WebClient();
                try
                {
                    Wclient.DownloadFile(HttpFile,SaveFile);
                    ReturnMsg= "1";
                }
                catch(WebException ex)
                {
                    if(ex!=null) ReturnMsg= "0";
                }
            }else{
                ReturnMsg= "-1";
            }
            if(ReturnMsg=="")ReturnMsg="-1";
            return ReturnMsg;
        }//Html下载
    }
}

cd/
C:
cd C:/WINNT/Microsoft.NET/Framework/v1.0.3705/
csc /t:library /r:System.Web.dll  /r:System.dll /out:E:/AspxProject/News/bin/WebTools.dll E:/AspxProject/News/bin/WebTools.cs
CMD

抱歉!评论已关闭.