代码都比较简单.不用多做说明:
Code
aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="加密" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="解密" /><br />
<br />
<asp:TextBox ID="txtConnectionStr" runat="server" Width="717px"></asp:TextBox><br />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="保存鏈接字串" />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="測試連線" /></div>
</form>
</body>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.Sections["connectionStrings"];
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.ConnectionStrings;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected void Button4_Click(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["PEP"].ToString();
Response.Write(ConnectionTesting(str));
}
protected void Button3_Click(object sender, EventArgs e)
{
//測試數據庫鏈接字串是否正確.
if (ConnectionTesting(txtConnectionStr.Text) == "鏈接字串設置成功")
{
//保存當前鏈接字串到config
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");
section.ConnectionStrings["PEP"].ConnectionString = txtConnectionStr.Text;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected string ConnectionTesting(string connstr)
{
string msg = "";
string str = string.IsNullOrEmpty(connstr) ? "" : connstr;
SqlConnection conn = new SqlConnection(str);
try
{
conn.Open();
msg = "鏈接字串設置成功";
}
catch (Exception ex)
{
msg = ex.Message.ToString();
}
finally
{
conn.Close();
}
return msg;
}
aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="加密" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="解密" /><br />
<br />
<asp:TextBox ID="txtConnectionStr" runat="server" Width="717px"></asp:TextBox><br />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="保存鏈接字串" />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="測試連線" /></div>
</form>
</body>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.Sections["connectionStrings"];
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.ConnectionStrings;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected void Button4_Click(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["PEP"].ToString();
Response.Write(ConnectionTesting(str));
}
protected void Button3_Click(object sender, EventArgs e)
{
//測試數據庫鏈接字串是否正確.
if (ConnectionTesting(txtConnectionStr.Text) == "鏈接字串設置成功")
{
//保存當前鏈接字串到config
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");
section.ConnectionStrings["PEP"].ConnectionString = txtConnectionStr.Text;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected string ConnectionTesting(string connstr)
{
string msg = "";
string str = string.IsNullOrEmpty(connstr) ? "" : connstr;
SqlConnection conn = new SqlConnection(str);
try
{
conn.Open();
msg = "鏈接字串設置成功";
}
catch (Exception ex)
{
msg = ex.Message.ToString();
}
finally
{
conn.Close();
}
return msg;
}