.net的配置文件(无法传入正确的值)

发布于 2024-12-02 05:37:51 字数 1546 浏览 1 评论 0原文

我的配置文件的效果看起来像这样。

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-09 05:37:51

您使用的 .net 版本是什么?如果您使用的是 3.5 或更高版本,请勿将其放在 appSettings 区域中,而是将其放在 connectionStrings 部分中。

要将连接字符串读入代码中,请使用 ConfigurationSettings 类,如下所示:

Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("FooBar").ConnectionString)

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 the connectionStrings section.

To read the connection string into your code, use the ConfigurationSettings class like this:

Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("FooBar").ConnectionString)
旧时光的容颜 2024-12-09 05:37:51

ConfigurationSettings.AppSettings 方法返回一个String。因此,您有两个选择:

1)将变量声明为字符串:

Dim ConfUpperBound as String = ConfigurationSettings.AppSettings("UppBound")

2)将字符串值转换为整数:

Dim ConfUpperBound as Int = Convert.ToInt32(ConfigurationSettings.AppSettings("UppBound"))

The ConfigurationSettings.AppSettings method returns a String. Thus you have two options:

1) declare your variable as String:

Dim ConfUpperBound as String = ConfigurationSettings.AppSettings("UppBound")

2) Convert string value to an integer:

Dim ConfUpperBound as Int = Convert.ToInt32(ConfigurationSettings.AppSettings("UppBound"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文