2017-09-14 137 views
0

我是否需要在其他地方添加连接字符串,就像我们在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") 
+0

在你的项目中你应该有一个_app.config_文件。这是使用的文件 – Steve

+0

我正在使用Vb.net 2015窗口窗体。我在哪里可以找到app.config文件? – Doll

+0

在您的解决方案资源管理器中,它应该是列表中的第4项。 – braX

回答

2

它不工作,因为你是分配连接字符串后'新'的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") 
+0

现在我得到了新的错误..可以不打开System.Data.SqlClient.SqlException:用户登录失败 – Doll

+1

你试图连接到什么样的数据库?它会是SQL服务器?错误点,它正试图连接,但无法登录到服务器。查看https://www.connectionstrings.com/该站点列出了适用于各种数据库类型的正确连接字符串。 – AustinS90