我的Sql Server数据库连接没有得到错误
问题描述:
我是否需要在其他地方添加连接字符串,就像我们在Asp.net的web配置中一样,但是在vb.net窗口窗体中我找不到像web config的任何东西 下面是代码连接...我的Sql Server数据库连接没有得到错误
Dim Conn As New SqlConnection
Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"
Conn = New SqlConnection
' If Conn Is Nothing Then
If Conn.State = ConnectionState.Open Then
MsgBox("Connected")
Else
MsgBox("Not Connected")
答
它不工作,因为你是分配连接字符串后'新'的SqlConnection。
这是您当前的代码;
Dim Conn As New SqlConnection
Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"
Conn = New SqlConnection
' If Conn Is Nothing Then
If Conn.State = ConnectionState.Open Then
MsgBox("Connected")
Else
MsgBox("Not Connected")
但是,你必须取出第二个新的连接字符串,应该看起来像这样;
Dim Conn As New SqlConnection
Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"
' If Conn Is Nothing Then
If Conn.State = ConnectionState.Open Then
MsgBox("Connected")
Else
MsgBox("Not Connected")
在你的项目中你应该有一个_app.config_文件。这是使用的文件 – Steve
我正在使用Vb.net 2015窗口窗体。我在哪里可以找到app.config文件? – Doll
在您的解决方案资源管理器中,它应该是列表中的第4项。 – braX