Sql .net的配置文件(无法传递正确的值)
我的配置文件中的一个文件看起来像这样Sql .net的配置文件(无法传递正确的值),sql,sql-server,vb.net,configuration-files,Sql,Sql Server,Vb.net,Configuration Files,我的配置文件中的一个文件看起来像这样 <?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;Integra
<?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
作为参考,我的整个代码如下所示
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
公共类表单1
Dim ConfUpperBound为整数=Configuration.AppSettings(“上限”)
Dim ConfConnString as String=Configuration.AppSettings(“ConnString”)
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
Dim SQLCONN作为新的SqlConnection(“”)
//以下行失败(无法打开连接)
SQLCONN.Open()
//我可以在查询中成功地传递值,这样我就可以连接到conf文件
Dim SQLCMD作为新的SqlCommand(“myquery等”)
SQLCMD.ExecuteNonQuery()
SQLCONN.Close()
端接头
末级
编辑:使用Visual Studio 2005时,我能够完美地传入字符串。但是当使用VS2003和Loower.net框架时,它不会允许我使用,除非我硬编码
是否可以在这些VS实例之一上启用/禁用某些内容?或者这只是旧版本的.net的一个错误?我想我在2003年使用了1.1???(必须检查)。配置设置。AppSettings方法返回一个
字符串。因此,您有两种选择:
1) 将变量声明为字符串:
Dim ConfUpperBound as String = ConfigurationSettings.AppSettings("UppBound")
2) 将字符串值转换为整数:
Dim ConfUpperBound as Int = Convert.ToInt32(ConfigurationSettings.AppSettings("UppBound"))
您使用的是什么版本的.net?如果您使用的是3.5或更高版本,请不要将其放在appSettings
区域,而是放在connectionStrings
部分
要将连接字符串读入代码,请使用如下ConfigurationSettings
类:
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("FooBar").ConnectionString)
连接字符串中“Data Source”的输入错误如何:这里只是一个输入错误,实际代码中没有输入错误。我正在使用2.0,但会尝试混淆ConfigurationManager属性。
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("FooBar").ConnectionString)