VB.NET打开与Postgres的连接

时间:2017-11-24 08:08:57

标签: .net vb.net postgresql

我正在开发一个需要连接到Postgresql的项目,我已经尝试过以下代码:

Public Module Main
Dim _desktop As Desktop = Agent.Desktop

Public Sub Main(args As IDictionary(Of String, Object))
    Dim conn As New Odbc.OdbcConnection
    conn.ConnectionString = "Driver={PostgreSQL ANSI};database=postgres;server=127.0.0.1;port=5432;uid=postgres;sslmode=disable;readonly=0;protocol=9.4;User ID=postgres;password=12345678;"

    conn.Open()
    If conn.State = ConnectionState.Open Then
        MsgBox("Connected To PostGres", MsgBoxStyle.MsgBoxSetForeground)
    End If
    conn.Close()
End Sub
End Module

但我有问题Type 'Odbc.OdbcConnection' is not defined.。有没有人可以帮我解决问题?该错误表明我没有导入依赖项,但我不知道正确的导入方式。

1 个答案:

答案 0 :(得分:0)

此代码可能对您有所帮助:

 Dim MyCon As New Odbc.OdbcConnection
MyCon.ConnectionString = "Driver={PostgreSQL ANSI};database=database_name;server=127.0.0.1;port=5432;uid=postgres;sslmode=dis
 able;readonly=0;protocol=7.4;User ID=postgres;password=password;"

  MyCon.Open()
    If mCon.State = ConnectionState.Open Then
        MsgBox("Connected To PostGres", MsgBoxStyle.MsgBoxSetForeground)
    End If

请务必输入正确的IP,用户名和密码。但在所有内容之前,请安装必需的drivers