.net的配置文件(无法传入正确的值)
我的配置文件的效果看起来像这样。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="loglocal" value="C:\here.txt" />
<add key="ConnString" value="Data Source=DBNAME;InitialCatalog=CataName;Integrated Security=True"/>
<add key="UppBound" value="100" />
<add key="LowBound" value="100" />
</appSettings>
</configuration
我试图将一些值传递到我的 .net 程序中,唯一有效的是 loglocal
它正在选择一个目录字符串。
我可以将边界传递到查询中,但无法传递连接字符串并打开连接。
在传递长查询之前,我也尝试过传递我自己的一组引号:
Dim SQLCONN as new SqlConnection("""" & nameofconfvalue & """")
作为参考,我的整个代码如下所示。
Public Class Form1
Dim ConfUpperBound as Integer = Configuration.AppSettings("UppBound")
Dim ConfConnString as String = Configuration.AppSettings("ConnString")
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim SQLCONN as new SqlConnection("<either from config(fails) or hardcoded(works)>")
//fails on the below line (cannot open Connection)
SQLCONN.Open()
//I can successfully pass in values in query so I know i can connect to conf file
Dim SQLCMD as New SqlCommand("myquery etc etc")
SQLCMD.ExecuteNonQuery()
SQLCONN.Close()
End Sub
End Class
我能够完美地传递字符串。但是当使用 VS 2003 和 loiwer .net 框架时,它不会让我传递,除非我硬编码它。
编辑:当使用 Visual Studio 2005 时, 在这些 VS 实例之一上启用/禁用?或者这可能只是旧版本 .net 的错误?我想我在 2003 年使用的是 1.1??(必须检查)。
An exert from my config file looks liek this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="loglocal" value="C:\here.txt" />
<add key="ConnString" value="Data Source=DBNAME;InitialCatalog=CataName;Integrated Security=True"/>
<add key="UppBound" value="100" />
<add key="LowBound" value="100" />
</appSettings>
</configuration
I am attempting to pass in some values into my .net program, and the only one that works is the loglocal
which is picking a directory string.
I can pass in my bounds into queries but I cannot pass in the Connection string and open the connection.
I have tried passing in my own set of quotes as well before passing in the long query like:
Dim SQLCONN as new SqlConnection("""" & nameofconfvalue & """")
For reference my entire code looks like this.
Public Class Form1
Dim ConfUpperBound as Integer = Configuration.AppSettings("UppBound")
Dim ConfConnString as String = Configuration.AppSettings("ConnString")
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim SQLCONN as new SqlConnection("<either from config(fails) or hardcoded(works)>")
//fails on the below line (cannot open Connection)
SQLCONN.Open()
//I can successfully pass in values in query so I know i can connect to conf file
Dim SQLCMD as New SqlCommand("myquery etc etc")
SQLCMD.ExecuteNonQuery()
SQLCONN.Close()
End Sub
End Class
EDIT: I am able to pass in the string perfectly when using Visual Studio 2005. But when using VS 2003 and a loiwer .net framework it will not let me, unless I hard code it in.
Could something be enabled/disabled on one of these instances of VS? Or could this just be an error with the older versions of .net? I think I am using 1.1??(will have to check) in 2003.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的 .net 版本是什么?如果您使用的是 3.5 或更高版本,请勿将其放在
appSettings
区域中,而是将其放在connectionStrings
部分中。要将连接字符串读入代码中,请使用
ConfigurationSettings
类,如下所示:What version of .net are you using? If you're using 3.5 or above don't put it in the
appSettings
area, but instead put it in theconnectionStrings
section.To read the connection string into your code, use the
ConfigurationSettings
class like this:ConfigurationSettings.AppSettings
方法返回一个String
。因此,您有两个选择:1)将变量声明为字符串:
2)将字符串值转换为整数:
The
ConfigurationSettings.AppSettings
method returns aString
. Thus you have two options:1) declare your variable as String:
2) Convert string value to an integer: