如果您想了解c#–从web.config文件获取sql连接字符串的相关知识,那么本文是一篇不可错过的文章,我们将对c#读取sqlserver进行全面详尽的解释,并且为您提供关于.net–web.con
如果您想了解c# – 从web.config文件获取sql连接字符串的相关知识,那么本文是一篇不可错过的文章,我们将对c#读取sql server进行全面详尽的解释,并且为您提供关于.net – web.config中的EF 4.1代码优先连接字符串、.net – 使用Wix编辑Web.Config连接字符串设置、.net – 加密web.config中的连接字符串、ACCESS在Web.config里设置连接字符串的有价值的信息。
本文目录一览:- c# – 从web.config文件获取sql连接字符串(c#读取sql server)
- .net – web.config中的EF 4.1代码优先连接字符串
- .net – 使用Wix编辑Web.Config连接字符串设置
- .net – 加密web.config中的连接字符串
- ACCESS在Web.config里设置连接字符串
c# – 从web.config文件获取sql连接字符串(c#读取sql server)
这是我试过的
protected void buttontb_click(object sender,EventArgs e) { System.Configuration.Configuration rootwebconfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Mohtisham"); System.Configuration.ConnectionStringSettings constring; constring = rootwebconfig.ConnectionStrings.ConnectionStrings["northwindconnect"]; sqlConnection sql = new sqlConnection(constring); sql.open(); sqlCommand comm = new sqlCommand("Insert into categories (categoryName) values ('" + tb_database.Text + "')",sql); comm.ExecuteNonQuery(); sql.Close(); }
我得到一个工具提示错误
sqlConnection sql = new sqlConnection(constring);
如
System.data.sqlClient.sqlconnection.sqlconnection(string) has some invalid arguments.
我想加载连接字符串从web.config在约束
解决方法
ConnectionStrings
集合是
ConnectionStringSettings
对象的集合,但是
SqlConnection
构造函数需要一个字符串参数.所以你不能自己通过约束.
试试这个.
sqlConnection sql = new sqlConnection(constring.ConnectionString);
.net – web.config中的EF 4.1代码优先连接字符串
<add name="MyWebSiteContext" connectionString="Server=esql2k123.discountasp.net; Database=sql2008R2_123456_test;uid=sql2008R2_123456_test_user;pwd=mypassword;" providerName="System.Data.sqlClient"/>
这不起作用,并给出一个错误,说关键字’服务器’无法识别.经过大量的谷歌搜索后,似乎问题很可能与providerName =“System.Data.sqlClient”部分有关.
所以在将其更改为providerName =“System.Data.EntityClient”时,我充满了希望.唉,同样的故事.
为了缩短(非常)冗长的故事,我尝试了许多排列(突变是操作词!! :)但似乎无法达到代码优先ef连接字符串要求的正确语法要求.
最近有没有人去过类似的箍,发现了一个有效的解决方案?
手指,脚趾和眼睛xx’d ..
[UPDATE]我设法通过格式化我的连接字符串(ALMOST)工作如下:
<add name="GBCWebSiteContext" connectionString="Data Source=esql2k123.discountasp.net;Initial Catalog=sql2008R2_123456_test;Persist Security Info=True;MultipleActiveResultSets=True;User ID=sql2008R2_123456_test_user;Password=mypass" providerName="System.Data.sqlClient"/>
我现在能够在执行linq选择时停止并调试控制器操作.但是,在尝试返回行时,我现在收到一个异常,该异常表明:触发器返回结果集,服务器选项’disallow results from triggers’为true
那么,这里拼图的最后一块是什么,是连接字符串中的一个额外选项来否定这个问题?
[更新2]
哈利卢拉 – 我已经破解了坚果.正如我已经清除了global.asax行:
Database.Setinitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MyWebSiteContext>());
我没有意识到我需要在Database.Setinitializer()中包含一个空引用.
瞧,以下是我的问题:
Database.Setinitializer<MyWebSiteContext>(null);
谢谢大家的各种建议,他们帮助我专注于这个问题.很高兴 :)
解决方法
sqlConnectionString="data source=myServer.myDomain.com;initial catalog=mydbname;user id=myUid;password=myPwd"
.net – 使用Wix编辑Web.Config连接字符串设置
因此,如果我的web.config连接字符串设置看起来像这样:(其中[DBSERVER]& [dbnAME]是从对话框中恢复的属性)
<connectionStrings> <add name="SSitacticalSolutionEntities" connectionString="Metadata=res://*/Model.TacticalSolutionModel.csdl|res://*/Model.TacticalSolutionModel.ssdl|res://*/Model.TacticalSolutionModel.msl;provider=System.Data.sqlClient;provider connection string="Data Source=sd-sql2008r2;Initial Catalog=SsitacticalSolution1.2.4;Integrated Security=True;MultipleActiveResultSets=True" /> </connectionStrings>
我在我的Product.Wsx文件中编辑我的Web.config,如下所示:
<util:XmlFile Id="ModifyConnectionString" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" Name="connectionString" Value="Data Source=[DBSERVER];Initial Catalog=[dbnAME];Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True"" Sequence="5"/>
我得到一个像这样的连接字符串:
<connectionStrings> <add name="SSitacticalSolutionEntities" connectionString="Data Source=sd-sql2008r2;Initial Catalog=SsitacticalSolution1.2.4;Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True""/> </connectionStrings>
这当然是有道理的,因为我要求它用我在值中定义的内容替换当前的连接字符串属性.
但我真正需要的是编辑我的连接字符串的特定部分并留下余数(我可以在这里使用某种替换操作),即.保留我的所有模型设置,并根据需要更换数据库服务器和名称等.我以前使用Visual Studio安装程序执行此操作没有问题,并且它非常易于使用.
所以我的问题是可以使用util.XMLFile或者util:XmlConfig来完成吗?我没有运气就试过了.
或者这不可能与util.XMLFile一起使用,我是否必须在CustomAction中执行此操作?
任何想法都会有很大的帮助,在此先感谢…
解决方法
我这样做是因为它会是开发人员,而不是那个知道模型名称和实体名称的用户(不是通过安装对话框的用户,他们不会知道这个信息),所以我有一个具有不同属性的本地化文件它像产品名称等,所以我添加了一个模型名称,并为此命名.我从用户输入的对话框中获得的所有其他内容:即数据库名称,虚拟目录,模拟用户等…
如果它对任何人有帮助,这就是我最终为我的web.config提出的内容;这是我的product.wxs中处理此问题的部分.正如您所看到的,我在顶部有一个连接字符串属性,并在我的本地化文件中设置了loc.ModelName的占位符:
<Property Id="CONNECTION_STRING" Value="Metadata=res://*/Model.!(loc.ModelName).csdl|res://*/Model.!(loc.ModelName).ssdl|res://*/Model.!(loc.ModelName).msl;provider=System.Data.sqlClient;provider connection string=""/> <!-- The root of the installer. --> <Directory Id='TARGETDIR' Name='SourceDir'> <!-- Install into the inetpub/wwwroot directory --> <Directory Id="IISMain" Name='inetpub'> <Directory Id="WWWMain" Name='wwwroot' ComponentGuidGenerationSeed='C38ED13E-E1E3-40DB-B1FA-39400C6B2BC4'> <Directory Id='INSTALLLOCATION' Name="!(loc.ProductName)"> <!-- The component to define the Virtual Directory.--> <Component Id="WebVirtualDirComponent" Guid="D814F88F-6E0C-4365-A411-2F9807522C3D"> <!-- WebVirtualDir: The virtual directory we are installing. --> <!-- Alias: Alias attribute is the name that we will see in IIS.--> <!-- Directory: The Directory attribute is the "Physical Path" property in IIS and needs to tie to the ID specified above as the install location. --> <!-- WebSite: The WebSite attribute ties to a <WebSite> element in the setup file(see below). As this is an example of installing into the "Default Web Site" so that element is not under a component.--> <iis:WebVirtualDir Id="VDir" Alias="[VIRTUALDIRECTORYVALUE]" Directory="INSTALLLOCATION" WebSite="DefaultWebSite"> <!-- This turns the Virtual Directory into a web application. --> <iis:WebApplication Id="MyWebAppApplication" Name="[VIRTUALDIRECTORYVALUE]" WebAppPool="AppPool"/> <iis:WebDirProperties Id="WebSite_Properties" AnonymousAccess="no" WindowsAuthentication="yes" DefaultDocuments="!(loc.DefaultDocument)" Script="yes" Read="yes" /> </iis:WebVirtualDir> <CreateFolder/> <RemoveFolder Id= "GuidFolders" On= "uninstall"/> </Component> <!-- Components - this decides what we want to incude in our install Here we will alter our web.config for Impersonation,debug to false and connection string. --> <Component Id="Web.config" Guid="2ED81B77-F153-4003-9006-4770D789D4B6"> <!--install our web.config file,this isnt part of our initial MSBUILD--> <File Id="Web.config" Name="Web.config" Source="$(var.solutionDir)!(loc.WebApplicationProjectName)\Web.config" diskId="1" KeyPath="yes" /> <!--Modify our web.config - here we need to add Identity impersonation,changes session settings,add connection string settings and set debug setting--> <!--Ensure that the identity setting exists--> <util:XmlFile Id="system.webidentity" File="[INSTALLLOCATION]Web.config" Action="createElement" ElementPath="/configuration/system.web" Name="identity" SelectionLanguage="XPath" Sequence="1" /> <util:XmlFile Id="system.webIdentityAttribute" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="impersonate" Value="true" SelectionLanguage="XPath" Sequence="2" /> <util:XmlFile Id="system.webIdentityAttribute2" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="password" Value="[IMPERSONATIONUSERPASSWORD]" SelectionLanguage="XPath" Sequence="3" /> <util:XmlFile Id="system.webIdentityAttribute3" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/identity" Name="userName" Value="[IMPERSONATIONUSER]" SelectionLanguage="XPath" Sequence="4" /> <util:XmlFile Id="ModifyConnectionString" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" Name="connectionString" Value="[CONNECTION_STRING]Data Source=[DBSERVER];Initial Catalog=[dbnAME];Integrated Security=True;MultipleActiveResultSets=True"" SelectionLanguage="XPath" Sequence="5"/> <!--<authentication mode="Forms">--> <util:XmlFile Id="AuthenticationModeWindows" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/authentication" Name="mode" Value="Windows" Sequence="6" /> <!--Switch off debug--> <util:XmlConfig Sequence="7" Id="SwitchOffDebug" File="[INSTALLLOCATION]\web.config" Action="create" On="install" Node="value" ElementPath="/configuration/system.web/compilation" Name="debug" Value="false" /> <!--Session configuration <sessionState mode="InProc" timeout="15" />--> <util:XmlFile Id="system.websessionState" File="[INSTALLLOCATION]Web.config" Action="createElement" ElementPath="/configuration/system.web" Name="sessionState" Sequence="8" /> <util:XmlFile Id="system.websessionStateAttribute" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/sessionState" Name="mode" Value="InProc" Sequence="9" /> <util:XmlFile Id="system.websessionStateAttribute2" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/sessionState" Name="timeout" Value="15" Sequence="10" /> <util:XmlFile Id="system.websessionStateAttribute3" Action="setValue" File="[INSTALLLOCATION]Web.config" ElementPath="/configuration/system.web/sessionState" Name="cookieName" Value="[VIRTUALDIRECTORYVALUE]" Sequence="11" /> </Component> <iis:WebSite Id='DefaultWebSite' Description='Default Web Site' Directory='INSTALLLOCATION' SiteId ='[WEBSITEVALUE]' > <iis:WebAddress Id="AllUnassigned" Port="80" /> </iis:WebSite> <iis:WebAppPool Id="AppPool" Name="[APPPOOLVALUE]" /> <CustomAction Id="MapVirtualDirectory" Directory="INSTALLLOCATION" Return="asyncNowait" ExeCommand='[ASPNETREGIIS] -norestart -s "W3SVC/[WEBSITEVALUE]/ROOT/[VIRTUALDIRECTORYVALUE]"' /> <InstallExecuteSequence> <Custom Action="MapVirtualDirectory" After="InstallFinalize" >ASPNETREGIIS AND NOT Installed</Custom> </InstallExecuteSequence> <CustomAction Id="GetIISWebSites" BinaryKey="IisManager" DllEntry="GetWebSites" Execute="immediate" Return="check" /> <CustomAction Id="GetIISAppPools" BinaryKey="IisManager" DllEntry="GetAppPools" Execute="immediate" Return="check" /> <InstallUISequence> <Custom Action="GetIISWebSites" After="CostFinalize" Overridable="yes">NOT Installed</Custom> <Custom Action="GetIISAppPools" After="CostFinalize" Overridable="yes">NOT Installed</Custom> </InstallUISequence> <Feature Id='ApplicationFeatures' Title="!(loc.ProductName)" Level='1'> <ComponentRef Id='WebVirtualDirComponent' /> <ComponentGroupRef Id="MyWebApp_Project" /> <ComponentRef Id="Web.config" /> </Feature> <!-- Specify UI --> <Property Id="WIXUI_INSTALLDIR">INSTALLLOCATION</Property> <UIRef Id="MyCustomUI"/>
这是我的本地化文件:
<?xml version="1.0" encoding="utf-8"?> <WixLocalization Culture="en-us" xmlns="http://schemas.microsoft.com/wix/2006/localization"> <!--application settings--> <String Id="LANG">1033</String> <String Id="ProductName">MyTestWebSite</String> <String Id="ProductVersion">1.0.0.0</String> <String Id="CompanyName">MyCompanyName</String> <String Id="DefaultDocument">Default.aspx</String> <String Id="WebApplicationProjectName">MyWebApp</String> <!--database settings--> <String Id="EntityName">MyEntities</String> <String Id="ModelName">MyModel</String> </WixLocalization>
.net – 加密web.config中的连接字符串
解决方法
Encrypting Connection Strings in web.config file
Securing connection strings and making secure authentication method
ACCESS在Web.config里设置连接字符串
在web.config中使用象asp那样的Server.MapPath取数据库路径 但web.config不认识Server.MapPath,此方法致使程序无法调试,行不通。 如下方法,可以方便的移植程序路径而不必再去修改
在web.config中使用象asp那样的Server.MapPath取数据库路径
但web.config不认识Server.MapPath,此方法致使程序无法调试,行不通。
如下方法,可以方便的移植程序路径而不必再去修改access数据库路径。
在web.config中的写法如下:
程序中的数据访问类中我把"ConnStr"和"dbPath"取出来再连接成一个字符串
代码如下:
///
/// (静态)返回数据库连接串。
///
protected static string GetConnString()
{
return
System.Configuration.ConfigurationSettings.AppSettings["ConnStr"]+ System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["DataPath"])+";";
}
关于c# – 从web.config文件获取sql连接字符串和c#读取sql server的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于.net – web.config中的EF 4.1代码优先连接字符串、.net – 使用Wix编辑Web.Config连接字符串设置、.net – 加密web.config中的连接字符串、ACCESS在Web.config里设置连接字符串的相关知识,请在本站寻找。
本文标签: