-----------------------IMeetYouable.cs------------------------------
namespace ClassLibrary1
{
public interface IMeetYouable
{
void SetInfo(string name,string value);
string GetInfo(string name);
string[] Name { get; }
bool IsExits(string name);
}
}
------------------------EnvironmentSettings.cs---------------------------------第一种方法
namespace ClassLibrary1
{
[Serializable]
public class EnvironmentSettings:IMeetYouable
{
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 IsExits(string name)
{
IDictionary dic = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
return dic.Contains(name);
}
}
}
-------------------------MemorySettings.cs------------------------------------第二种方法
namespace ClassLibrary1
{
[Serializable]
public class MemorySettings:IMeetYouable
{
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 IsExits(string name)
{
return dic.ContainsKey(name);
}
}
}
-------------------------SqlServerSettings.cs--------------------------------第三种方法
namespace ClassLibrary1
{
[Serializable]
public class SqlServerSettings : IMeetYouable
{
public void SetInfo(string name, string value)
{
IDbConnection conn = new SqlConnection(@"Data Source=PC-20130106GGEO;Initial Catalog=lianxi;Persist Security Info=True;User ID=sa;Password=111111");
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into T_Meet(MeetKey,MeetValue) values(@meetkey,@meetvalue)";
cmd.Parameters.Add(new SqlParameter("@meetkey", name));
cmd.Parameters.Add(new SqlParameter("@meetvalue", value));
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();
}
public string GetInfo(string name)
{
IDbConnection conn = new SqlConnection(@"Data Source=PC-20130106GGEO;Initial Catalog=lianxi;Persist Security Info=User ID=sa;Password=111111");
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select MeetValue from T_Meet where MeetKey=@meetkey";
cmd.Parameters.Add(new SqlParameter("@meetkey", name));
conn.Open();
object obj = cmd.ExecuteScalar();
cmd.Dispose();
conn.Dispose();
return obj.ToString();
}
List<string> list = new List<string>();
public string[] Name
{
get
{
DbConnection conn = new SqlConnection(@"Data Source=PC-20130106GGEO;Initial Catalog=lianxi;Persist Security Info=User ID=sa;Password=111111");
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_Meet";
conn.Open();
DataTable dt = new DataTable();
DbDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
foreach (DataRow row in dt.Rows)
{
list.Add(row["MeetKey"].ToString());
}
return list.ToArray();
}
}
public bool IsExits(string name)
{
return list.Contains(name);
}
}
}
------------------------------Web.config-----------------------
<appSettings>
<add key="setting" value="MEMORY"/>
</appSettings>
<connectionStrings>
<add name="sqlservercon" connectionString="Data Source=.;Initial Catalog=lianxi;Persist Security Info=True;User ID=sa;Password=111111"/>
</connectionStrings>
------------------------WebForm1.aspx----------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
见面时间
</td>
<td>
<asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
见面地点
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
当天天气
</td>
<td>
<asp:TextBox ID="txtWeather" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
就餐地点
</td>
<td>
<asp:TextBox ID="txtEat" runat="server"></asp:TextBox>
</td>
</tr>
<tr><td>
<asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click" /> </td><td>
</td></tr>
</table>
</div>
<asp:TextBox ID="txtquery" runat="server"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server" Text="查询"
onclick="btnSelect_Click" />
<br />
<asp:Button ID="btnSelectAll" runat="server" Text="查询所有要素" onclick="btnSelectAll_Click"
/>
<asp:Button ID="btnQuery" runat="server" Text="判断某个要素是否存在" onclick="btnQuery_Click"
/>
</form>
</body>
</html>
---------------------WebForm1.aspx.cs-----------------------------
namespace 接口实现状态保存
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
ClassLibrary1.IMeetYouable meet = GetSettings();
meet.SetInfo("MeetTime", txtTime.Text);
meet.SetInfo("MeetAddress", txtAddress.Text);
meet.SetInfo("MeetWeather", txtWeather.Text);
meet.SetInfo("EatAddress", txtEat.Text);
ViewState["meet"] = meet;
}
private IMeetYouable GetSettings()
{
IMeetYouable meet = null;
string settings = ConfigurationManager.AppSettings["setting"];
if (settings.ToUpper() == "MEMORY")
{
meet = new MemorySettings();
}
else if (settings.ToUpper() == "ENVIROMENT")
{
meet = new EnvironmentSettings();
}
else if (settings.ToUpper() == "SQLSERVER")
{
meet = new SqlServerSettings();
}
return meet;
}
protected void btnSelect_Click(object sender, EventArgs e)
{
if (ViewState["meet"] != null)
{
IMeetYouable meet = ViewState["meet"] as IMeetYouable;
string meettime = meet.GetInfo(txtquery.Text);
Response.Write(meettime);
}
}
protected void btnSelectAll_Click(object sender, EventArgs e)
{
if (ViewState["meet"] != null)
{
IMeetYouable meet = ViewState["meet"] as IMeetYouable;
string[] infos = meet.Name;
foreach (string item in infos)
{
Response.Write(item);
}
}
}
protected void btnQuery_Click(object sender, EventArgs e)
{
if (ViewState["meet"] != null)
{
IMeetYouable meet = ViewState["meet"] as IMeetYouable;
Response.Write(meet.IsExits("MeetWeather1"));
}
}
}
}