接口练习

前台代码:

<form id="form1" runat="server">
    <div>
        见面时间:<asp:TextBox ID="MeetTime" runat="server"></asp:TextBox><br />
        见面地点:<asp:TextBox ID="MeetAddress" runat="server"></asp:TextBox><br />
        当天天气:<asp:TextBox ID="MeetWeather" runat="server"></asp:TextBox><br />
        就餐地点:<asp:TextBox ID="EatAddress" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSave" runat="server" Text="保存" οnclick="btnSave_Click" /><br />
        查找键的值: <asp:TextBox ID="FindKey" runat="server"></asp:TextBox>
        <asp:Button ID="btnFindKey" runat="server" Text="查找键值"
            οnclick="btnFindKey_Click" /><br />
        <asp:Button ID="btnShowKey" runat="server" Text="显示所有的键"
            οnclick="btnShowKey_Click" /><br />
        <asp:Button ID="btnIsExist" runat="server" Text="判断某个要素是否存在"
            οnclick="btnIsExist_Click" />
    </div>
    </form>

 

后台:

protected void btnSave_Click(object sender, EventArgs e)
        {
            ClassLibrary1.IMeetYou meet = GetType();
            meet.SetInfo("MeetTime", MeetTime.Text);
            meet.SetInfo("MeetAddress", MeetAddress.Text);
            meet.SetInfo("MeetWeather", MeetWeather.Text);
            meet.SetInfo("EatAddress", EatAddress.Text);
            ViewState["meet"] = meet;
        }

        protected void btnFindKey_Click(object sender, EventArgs e)
        {
            if (ViewState["meet"] != null)
            {
                IMeetYou meet = ViewState["meet"] as IMeetYou;
                string meetValue = meet.GetInfo(FindKey.Text);
                Response.Write(meetValue);
            }
        }

        protected void btnShowKey_Click(object sender, EventArgs e)
        {
            if (ViewState["meet"] != null)
            {
                IMeetYou meet = ViewState["meet"] as IMeetYou;
                string[] infos = meet.name;
                foreach (string item in infos)
                {
                    Response.Write(item + "    ");
                }
            }
        }

        protected void btnIsExist_Click(object sender, EventArgs e)
        {
            if (ViewState["meet"] != null)
            {
                IMeetYou meet = ViewState["meet"] as IMeetYou;
                Response.Write(meet.isExist("1111"));
            }
        }

        private IMeetYou GetType()
        {
            ClassLibrary1.IMeetYou meet = null;
            string conType = ConfigurationManager.AppSettings["conType"];
            if (conType.ToUpper() == "MEMORY")
            { meet = new MemorySetting(); }
            else if (conType.ToUpper() == "ENVIRONMENT")
            { meet = new EnvironmentSettings(); }
            else if (conType.ToUpper() == "SQLSERVER")
            { meet = new SqlServerSettings(); }
            return meet;
        }

---------------------------EnvironmentSettings.cs--------------------------

namespace ClassLibrary1
{
    [Serializable]
     public class EnvironmentSettings:IMeetYou
    {
        
        public void SetInfo(string name, string value)
        {
            Environment.SetEnvironmentVariable(name, value,EnvironmentVariableTarget.User);
        }

        public string GetInfo(string name)
        {
           return  Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User);
        }

        public string[] name
        {
            get {
                IDictionary dic = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
                List<string> list = new List<string>();
                foreach (object item in dic.Keys)
                {
                    list.Add(item.ToString());
                }
                return list.ToArray();
            }
        }

        public bool isExist(string name)
        {
            IDictionary dic = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
            return dic.Contains(name);
        }
    }
}

 

----------------------------------IMeetYou.cs---------------------------

     public interface IMeetYou
    {
         void SetInfo(string name, string value);//用来保存数据
         string GetInfo(string name);//根据某项的键,查找值
         string[] name { get; }//获得所有项的值
         bool isExist(string name);//判断某项的值是否存在
    }

----------------------------------MemorySetting.cs-------------------------------

namespace ClassLibrary1
{
    [Serializable]
     public class MemorySetting:IMeetYou
    {
        
         Dictionary<string, string> dic = new Dictionary<string, string>();
        public void SetInfo(string name, string value)
        {
            dic.Add(name, value);
        }

        public string GetInfo(string name)
        {
            return dic[name];
        }

        public string[] name
        {
            get { return dic.Keys.ToArray(); }
        }

        public bool isExist(string name)
        {
            return dic.ContainsKey(name);
        }
    }
}

-------------------------------SqlServerSettings.cs----------------------------

namespace ClassLibrary1
{
    [Serializable]
    public class SqlServerSettings:IMeetYou
    {
       
        //public IDbConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111");
        public void SetInfo(string name, string value)
        {
            IDbConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111");

            conn.Open();
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into T_Meet(T_MeetKey,T_MeetValue) values(@meetkey,@meetvalue)";
            cmd.Parameters.Add(new SqlParameter("@meetkey",name));
            cmd.Parameters.Add(new SqlParameter("@meetvalue",value));
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Dispose();
        }

        public string GetInfo(string name)
        {
            IDbConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111");

            conn.Open();
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select T_MeetValue from T_Meet where T_MeetKey=@meetkey";
            cmd.Parameters.Add(new SqlParameter("@meetkey",name));
            object obj = cmd.ExecuteScalar();
            cmd.Dispose();
            conn.Dispose();
            return obj.ToString();
        }

        public string[] name
        {
            get {
                IDbConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111");

                List<string> list = new List<string>();
                conn.Open();
                IDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "select T_MeetKey from T_Meet";
                IDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                         list.Add(reader["T_MeetKey"].ToString());
                }
                return list.ToArray();
            }
        }

        public bool isExist(string name)
        {
           IDbConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111");

            conn.Open();
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select T_MeetValue from T_Meet where T_MeetKey=@meetkey";
            cmd.Parameters.Add(new SqlParameter("@meetkey", name));
            IDataReader read = cmd.ExecuteReader();
            bool b=false;
            if (read.Read())
            { b = true; }
            cmd.Dispose();
            conn.Dispose();
            return b;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值