首先新建反射类库

 1、命名空间:WitbankData

 2、类名:WitbankData

方法: public void Other(string accountName, string password, out int i)

具体代码如下

namespace WitbankData
{
    public class WitbankData
    {
        public static System.Web.HttpCookie MyCookieUser = new System.Web.HttpCookie("SetCookieUser");
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="accountName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="i">返回参数: 0 对不起,帐户名或密码错误!  1 登录成功  2您的账号尚未开放请先进行身份认证</param>
        public void Other(string accountName, string password, out int i)
        {
            int valueType = 0;
            //获取数据
            using (DataSet ds1 = GetData(accountName, password))
            {
                if (ds1.Tables[0].Rows.Count > 0)
                {

                    if (int.Parse(ds1.Tables[0].Rows[0]["Status"].ToString()) == 0)
                    {
                        //您的账号尚未开放请先进行身份认证!
                        valueType = 2;
                    }
                    else
                    {
                        SetCookieUser("UserID", ds1.Tables[0].Rows[0]["UserID"].ToString());
                        SetCookieUser("Account", ds1.Tables[0].Rows[0]["AccountName"].ToString());
                        SetCookieUser("UserName", ds1.Tables[0].Rows[0]["UserName"].ToString());
                        SetCookieUser("UserStructureID", ds1.Tables[0].Rows[0]["StructureID"].ToString());
                        SetCookieUser("StationNameIng", ds1.Tables[0].Rows[0]["StationName"].ToString());
                        SetCookieUser("yhjf", ds1.Tables[0].Rows[0]["Enumerable"].ToString());
                        SetCookieUser("Business", ds1.Tables[0].Rows[0]["Business"].ToString());
                        SetCookieUser("UserMark", ds1.Tables[0].Rows[0]["UserMark"].ToString());

                        SetCookiesa();
                        //登录成功
                        valueType = 1;
                    }
                }
                else
                {
                    //  '对不起,帐户名或密码错误!
                    valueType = 0;
                }
            }

            i = valueType;
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="accountName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public DataSet GetData(string accountName, string password)
        {
            string Sql = "select * from Users where AccountName = '" + accountName.ToString().Replace("'", "'") + "' and LoginPassword = '" + password.ToString().Replace("'", "'").Trim() + "'";
            using (SqlConnection conn = new SqlConnection("server=(local);User ID=sa;Password=happy;database=Elearning;Connection Reset=FALSE;Max Pool Size =512;"))//ConfigurationManager.ConnectionStrings["WitBank"].ConnectionString))
            {

                conn.Open();

                using (SqlCommand cmd = conn.CreateCommand())
                {
                    DataSet ds1 = new DataSet();
                    cmd.CommandText = Sql;

                    SqlDataAdapter adapter = new SqlDataAdapter();

                    adapter.SelectCommand = cmd;


                    adapter.Fill(ds1);
                    ds1.Dispose();
                    return ds1;
                }
            }
        }
        /// <summary>
        /// 写Cookie
        /// </summary>
        /// <param name="name"></param>
        /// <param name="val"></param>
        public void SetCookieUser(string name, string val)
        {
            try
            {
                MyCookieUser.Expires = DateTime.Now.AddDays(1);
                MyCookieUser[name] = System.Web.HttpContext.Current.Server.UrlEncode(val);
                System.Web.HttpContext.Current.Response.Cookies.Add(MyCookieUser);
            }
            catch
            {
                //System.Web.HttpContext.Current.Response.Write("errot--.");
            }
        }

        public void SetCookiesa()
        {
            try
            {
                HttpCookie MyCookie = new HttpCookie("LastVisita");
                DateTime now = DateTime.Now;
                MyCookie["UserID"] = GetCookiesUser("UserID").ToString();
                System.Web.HttpContext.Current.Response.Cookies.Add(MyCookie);
            }
            catch
            {

            }
        }
        public string GetCookiesUser(string name)
        {
            try
            {
                HttpCookie aa = System.Web.HttpContext.Current.Request.Cookies["SetCookieUser"];
                if (aa.Values[name] != null)
                {
                    return System.Web.HttpContext.Current.Server.UrlDecode(aa.Values[name]);
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 获取有效时间
        /// </summary>
        /// <returns></returns>
        public bool GetDate()
        {
            bool types = false;
            if (DateTime.Now < DateTime.Parse("2013-09-09"))
            {
                types = true;
            }
            return types;
        }
        public void GetCheck(out bool type)
        {
            bool types = false;
            if (DateTime.Now < DateTime.Parse("2013-09-09"))
            {
                types = true;
            }
            type = types;
        }

    }
}

  

 反射调用:

   private void Sub()
    {

        try
        {
            string path = System.Web.HttpContext.Current.Server.MapPath("").ToString() + "\\bin\\WitbankData.dll";//@"D:\Documents\Visual Studio 2008\Projects\localhost8022 (2)\WitbankData\bin\Debug\WitbankData.dll";

            Assembly ass;
            Type type;
            object obj;
            if (File.Exists(path))
            {
                //获取并加载DLL类库中的程序集
                ass = Assembly.LoadFile(path);

                //获取类的类型:必须使用名称空间+类名称
                type = ass.GetType("WitbankData.WitbankData");

                //获取类的方法:方法名称
                MethodInfo Other = type.GetMethod("Other");

                //对获取的类进行创建实例。//必须使用名称空间+类名称
                obj = ass.CreateInstance("WitbankData.WitbankData");

                //开始搜索方法
                Other = type.GetMethod("Other");//方法的名称1

                //定义数组,注:数组长度是3,当前只对前两个进行复制,第三个是返回参数
                object[] parts = new object[3];
                parts[0] = username.Text.ToString();//用户名
                parts[1] = password.Text.ToString();//密码

                //方法的调用
                //注:如果调用的DLL类库中方法是静态的,那么Invoke方法中第一个参数传值为NULL。
                //    如果方法不是静态的,那么Invoke方法中第一个参数传值为 obj(上面那个被实例的对象)
                Other.Invoke(obj, parts);
                //返回值
                int returnValue = (int)parts[2];

                // //您的账号尚未开放请先进行身份认证!
                if (returnValue == 2)
                {
                    Page.RegisterStartupScript("Error", "<script>alert('您的账号尚未开放请先进行身份认证!');win_linkb();</script>");
                }
                else if (returnValue == 0)
                {
                    //'对不起,帐户名或密码错误
                    Page.RegisterStartupScript("Error", "<script>alert('对不起,帐户名或密码错误!')</script>");
                }
                else if (returnValue == 3)
                {
                    Page.RegisterStartupScript("Error", "<script>alert('对不起,系统已过期!')</script>");
                }
                if (returnValue == 1)
                {
                    Page.Response.Redirect("user/index.aspx");
                }

            }
        }

        catch (Exception ee)
        {

            Page.RegisterStartupScript("Errorr", "<script>alert('数据库故障或网络不正常请稍后重试!')</script>");
        }
    }

  

posted on 2012-12-02 00:23  高兴happy  阅读(237)  评论(0编辑  收藏  举报