那种平板可以玩平板电脑qq游戏大厅

AppSettings和ConnectionStrings的区别-快网- |
&您的位置: &&
&& 文章正文
AppSettings和ConnectionStrings的区别供稿:李杨 & 时间: 0:34:53
AppSettings是ASP.NET1.1时期用的,在.NET Framework 2.0中,新增了ConnectionStrings. 1.&connectionStrings& &connectionStrings& &add name="ConnectionStringName" connectionString="Data Source=服务器名;Initial Catalog=数据库名;User ID=用户;Password=密码" providerName="System.Data.SqlClient" /& &/connectionStrings& 或是 &connectionStrings& &add name="ConnectionStringName" connectionString="sever=服务器名;database=数据库名;User ID=用户;Password=密码" providerName="System.Data.SqlClient" /& &/connectionStrings& 在 &connectionStrings& 里,在页面还可以这样引用&%$ ConnectionString:Name%&. 2.&appSettings& &add key="connectionstringName" value="data source=服务器名或IP;initial catalog=数据库名;persist security info=Fuser id=用户;password=密码;packet size=4096"& &/add& 1)AppSettings 是在2003中常用的,ConnectionStrins是2005中常用的. 2)使用ConnectionString的好处: 第一:可将连接字符串加密,使用MS的一个加密工具即可。 第二:可直接邦定的数据源控件,而不必写代码读出来再赋值给控件。 第三:可方便的更换数据库平台,如换为Oracle数据库,只需修改providerName。 3)写在 &appSettings &中用System.Configuration.ConfigurationManager.AppSettings["name"]检索值。 写在 &ConnectionStrings&中用System.Configuration.ConfigurationManager.ConnectionStrings["name"]检索值。
在 .NET Framework 2.0中,ConfigurationManager类新增了两个属性AppSettings和ConnectionStrings专门用来获取配置文件AppSettings和ConnectionStrings节的数据,使用方法如下:
using System.C public partial class _Default : System.Web.UI.Page&& { &&& protected void Page_Load(object sender, EventArgs e) &&& { &&&&&&& Label1.Text = ConfigurationManager.ConnectionStrings["connectionStringsName"].ToString(); &&&&&&& Label2.Text = ConfigurationManager.AppSettings["appSettingsName"].ToString(); &&& } }
&connectionStrings&和&appSettings&这个的区别1.&connectionStrings& &connectionStrings& &add name="SYXTConnectionString" connectionString="Data Source=LEIKE;Initial Catalog=SYXT;User ID=Password=sa" providerName="System.Data.SqlClient" /& &/connectionStrings&
2.&appSettings&:
&add key="connectionstring1" value="data source=192.168.123.201;initial catalog=persist security info=Fuser id=password=KSDChagd2004;packet size=4096"& &/add&
快网保留以上说明的最终解释权
& & 热门搜索:
快网客服中心的相关信息:
[ 11:17:23]v
[ 13:27:25]v
[ 14:51:37]v
[ 14:26:49]v
[ 18:07:37]
如果您在使用我们的产品中遇到问题,建议您首先在“”中查询解决方法;如果没有找到该问题的解决方法,您可以在“”中进行搜索;如果搜索后没有找到满意答案,您可以“”,我们会尽快给您答复。
| ::::::Copyright (C)2830人阅读
asp.net(43)
使用VS.NET进行WINFORM + MS SQL编程,一开始我们都会做以下几件事情:
连接到数据库;(如果你通常都是采用Windows集成验证,那么这篇文章可能你不需要看下去了)假设你采用的是SA + 密码的验证方式;
添加新数据源;VS.NET在这里提醒我们,连接串里面包含了敏感信息;
把连接字符串保存到程序的配置文件里,便于使用和维护;
假设我的项目名字是Test,现在当我们打开项目的App.config文件,没错,我的SA密码以明文的方式保存在那里了。
&?xmlversion=&1.0&encoding=&utf-8&&?&&configuration&&&&&&configSections&&&&&&/configSections&&&&&&connectionStrings&&&&&&&&&&addname=&Test.Properties.Settings.TestConnectionString&&&&&&&&&&&&&connectionString=&Data&Source=.;Initial&Catalog=TEST_ERP;Integrated&Security=Fuid=password=shiny&&&&&&&&&&&&&providerName=&System.Data.SqlClient&&/&&&&&&/connectionStrings&&/configuration&
生成EXE,在输出目录下面会产生一个Text.exe.config文件,刚才App.config里面的内容复制到了这个文件,这个是发布的时候需要安装到用户程序路径的,由于是文本文件,我们可以很轻松的根据用户的实际环境修改里面的连接设置。但是,sa的密码保存在里面怎么说也是很令人忐忑的事情。一个很自然的思路,我们可以把这个密码字符串经过加密再保存,要用的时候再解密,以下是我具体的做法。我采用的是DES的加密方式,也许你喜欢其他的加密算法,反正只要有对应的解密算法,都没问题。这个是我在网上找的加密工具类,公开了加密和解密的方法。
&1&&&&class&CryptClass&2&&&&{&3&&&&&&&&//默认密钥向量&4&&&&&&&&private&static&byte[]&Keys&=&{&<span style="COLOR: #x12,&<span style="COLOR: #x34,&<span style="COLOR: #x56,&<span style="COLOR: #x78,&<span style="COLOR: #x90,&<span style="COLOR: #xAB,&<span style="COLOR: #xCD,&<span style="COLOR: #xEF&};&5&&&&&&&&public&static&string&EncryptKey&=&&<span style="COLOR: #345678&;&6&&&&&&&&&7&&&&&&&&/**////&&summary&&8&&&&&&&&///&DES加密字符串&9&&&&&&&&///&&/summary&<span style="COLOR: #&&&&&&&&///&&param&name=&encryptString&&待加密的字符串&/param&<span style="COLOR: #&&&&&&&&///&&param&name=&encryptKey&&加密密钥,要求为8位&/param&<span style="COLOR: #&&&&&&&&///&&returns&加密成功返回加密后的字符串,失败返回源串&/returns&<span style="COLOR: #&&&&&&&&public&static&string&EncryptDES(string&encryptString,&string&encryptKey)<span style="COLOR: #&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&try<span style="COLOR: #&&&&&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&rgbKey&=&Encoding.UTF8.GetBytes(encryptKey.Substring(<span style="COLOR: #,&<span style="COLOR: #));<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&rgbIV&=&K<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&inputByteArray&=&Encoding.UTF8.GetBytes(encryptString);<span style="COLOR: #&&&&&&&&&&&&&&&&DESCryptoServiceProvider&dCSP&=&new&DESCryptoServiceProvider();<span style="COLOR: #&&&&&&&&&&&&&&&&MemoryStream&mStream&=&new&MemoryStream();<span style="COLOR: #&&&&&&&&&&&&&&&&CryptoStream&cStream&=&new&CryptoStream(mStream,&dCSP.CreateEncryptor(rgbKey,&rgbIV),&CryptoStreamMode.Write);<span style="COLOR: #&&&&&&&&&&&&&&&&cStream.Write(inputByteArray,&<span style="COLOR: #,&inputByteArray.Length);<span style="COLOR: #&&&&&&&&&&&&&&&&cStream.FlushFinalBlock();<span style="COLOR: #&&&&&&&&&&&&&&&&return&Convert.ToBase64String(mStream.ToArray());<span style="COLOR: #&&&&&&&&&&&&}<span style="COLOR: #&&&&&&&&&&&&catch<span style="COLOR: #&&&&&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&&&&&return&encryptS<span style="COLOR: #&&&&&&&&&&&&}<span style="COLOR: #&&&&&&&&}<span style="COLOR: #<span style="COLOR: #&&&&&&&&/**////&&summary&<span style="COLOR: #&&&&&&&&///&DES解密字符串<span style="COLOR: #&&&&&&&&///&&/summary&<span style="COLOR: #&&&&&&&&///&&param&name=&decryptString&&待解密的字符串&/param&<span style="COLOR: #&&&&&&&&///&&param&name=&decryptKey&&解密密钥,要求为8位,和加密密钥相同&/param&<span style="COLOR: #&&&&&&&&///&&returns&解密成功返回解密后的字符串,失败返源串&/returns&<span style="COLOR: #&&&&&&&&public&static&string&DecryptDES(string&decryptString,&string&decryptKey)<span style="COLOR: #&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&if&(decryptString&==&&&)<span style="COLOR: #&&&&&&&&&&&&&&&&return&&&;<span style="COLOR: #&&&&&&&&&&&&try<span style="COLOR: #&&&&&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&rgbKey&=&Encoding.UTF8.GetBytes(decryptKey);<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&rgbIV&=&K<span style="COLOR: #&&&&&&&&&&&&&&&&byte[]&inputByteArray&=&Convert.FromBase64String(decryptString);<span style="COLOR: #&&&&&&&&&&&&&&&&DESCryptoServiceProvider&DCSP&=&new&DESCryptoServiceProvider();<span style="COLOR: #&&&&&&&&&&&&&&&&MemoryStream&mStream&=&new&MemoryStream();<span style="COLOR: #&&&&&&&&&&&&&&&&CryptoStream&cStream&=&new&CryptoStream(mStream,&DCSP.CreateDecryptor(rgbKey,&rgbIV),&CryptoStreamMode.Write);<span style="COLOR: #&&&&&&&&&&&&&&&&cStream.Write(inputByteArray,&<span style="COLOR: #,&inputByteArray.Length);<span style="COLOR: #&&&&&&&&&&&&&&&&cStream.FlushFinalBlock();<span style="COLOR: #&&&&&&&&&&&&&&&&return&Encoding.UTF8.GetString(mStream.ToArray());<span style="COLOR: #&&&&&&&&&&&&}<span style="COLOR: #&&&&&&&&&&&&catch<span style="COLOR: #&&&&&&&&&&&&{<span style="COLOR: #&&&&&&&&&&&&&&&&return&&&;<span style="COLOR: #&&&&&&&&&&&&}<span style="COLOR: #&&&&&&&&}<span style="COLOR: #&&&&}
用你的密码,和自己选择好的密钥(记住要8位字符串)作为参数,调用一下上面的EncryptDES,得到加密后的密码字符串,用它把Test.exe.config里面的密码替换,现在我的config文件里面是:connectionString=&Data&Source=.;Initial&Catalog=TEST_ERP;Integrated&Security=Fuid=&&& password=&gcEHA/213uBLSOruspbHyQ==&&[注意:不要修改App.config里面的内容,这样的话,你在设计期开发环境里面的数据源就连接不上了,而且,要留意别让生成程序的时候VS把你的修改冲掉了,只需要把App.config的属性&复制到输出目录& 设置为不复制就可以了]下面我们来看看怎么在运行的时候解密出原来的密码:如果你总是自己用代码创建SQLConnection,那么只需要在创建前把ConnectionString里面的password部分解密就好了,利用上面加密类的解密函数。问题是我们都是很普遍的使用VS帮我们创建好的强类型的Dataset,和强类型的DataAdapter。而这些DataAdapter已经包含了创建SQLConnection的代码,他们通过读取程序配置的方式获得连接串,方式如下:&&&&this._connection.ConnectionString = global::Test.Properties.Settings.Default.TestConnectionS//这个属性是只读的程序配置是在程序第一次需要读取的时候加载的,我们需要改动它的默认行为,加入解密的操作。1.展开项目树下面的Properties文件夹,双击Settings.settings节点;2.在显示的窗口里面点击上面的查看代码,此时将会在你的项目下产生一个Settings.cs源文件;3.修改里面的代码如下:
namespace&Test.Properties&{&&&&using&U&&&&using&System.Data.SqlC&&&&using&System.Data.OleDb;&&&&internal&sealed&partial&class&Settings&{&&&&&&&&public&Settings()&{&&&&&&&&&&&&//&//&To&add&event&handlers&for&saving&and&changing&settings,&uncomment&the&lines&below:&&&&&&&&&&&&//&&&&&&&&&&&&//&this.SettingChanging&+=&this.SettingChangingEventH&&&&&&&&&&&&//&&&&&&&&&&&&this.SettingsLoaded&+=&this.SettingsLoadedEventH&&&&&&&&&&&&//&&&&&&&&}&&&&&&&&&&&&&&&&private&void&SettingChangingEventHandler(object&sender,&System.Configuration.SettingChangingEventArgs&e)&{&&&&&&&&&&&&//&Add&code&to&handle&the&SettingChangingEvent&event&here.&&&&&&&&}&&&&&&&&&&&&&&&&private&void&SettingsLoadedEventHandler(object&sender,System.Configuration.SettingsLoadedEventArgs&e)&{&&&&&&&&&&&&//&Add&code&to&handle&the&SettingsSaving&event&here.&&&&&&&&&&&&SqlConnectionStringBuilder&ConnSb&=&new&SqlConnectionStringBuilder();&&&&&&&&&&&&string&s1;&&&&&&&&&&&&ConnSb.ConnectionString&=&this[&TestConnectionString&].ToString();&&&&&&&&&&&&ConnSb.Password&=&CryptClass.DecryptDES(ConnSb.Password,&CryptClass.EncryptKey);&&&&&&&&&&&&s1&=&ConnSb.ConnectionS&&&&&&&&&&&&SetConstr(s1);&&&&&&&&}&&&&&&&&public&void&SetConstr(string&connstr1)&&&&&&&&{&&&&&&&&&&&&this[&TestConnectionString&]&=&connstr1;&&&&&&&&}&&&&}}
上面代码,public了SetConstr方法,以便于在程序的其他模块还可以有机会动态修改连接串。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:122069次
积分:1587
积分:1587
排名:千里之外
原创:10篇
转载:111篇
评论:20条
(23)(1)(2)(1)(4)(1)(1)(1)(2)(1)(1)(1)(7)(2)(3)(2)(1)(1)(2)(11)(7)(1)(8)(26)(1)(10)

我要回帖

更多关于 qq游戏平板电脑下载 的文章

 

随机推荐