VB .NET ODBC与Windows身份验证的连接

时间:2014-09-02 18:07:51

标签: sql-server vb.net dns odbc

我正在使用Windows Server Datacenter。我正在使用.Net 4.0版的VB .Net 2010 Express Edition。

我遇到了在VB .Net上与SQL Server进行ODBC连接的问题。

这是我与数据库连接的代码片段。

Imports System.Data.Odbc

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cn As Odbc.OdbcConnection
        cn = New Odbc.OdbcConnection("DRIVER={SQL Server};SERVER=<machine_name>\SQLEXPRESS;UID=<machine_name>\<windows_username>;" & _
                               "PWD=<windows_password>;DATABASE=testdb;")

        Dim mystring As String = "select * from Customers"
        Dim cmd As Odbc.OdbcCommand = New Odbc.OdbcCommand(mystring)
        cn.Open()
        MsgBox("Connected")
        cn.Close()
    End Sub
End Class

运行此程序我收到以下错误:

ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '<machine_name>\<windows_username>'.

主要问题是提供的用户名和密码错误。但我使用相同的用户名和密码来访问系统。

我认为问题在于我为连接字符串提供了用户名和密码的方式。

我通过与同一个数据库建立DNS连接来尝试它。我创建了系统DNS,并尝试连接下面的连接字符串。

DRIVER={SQL Server};DNS=<DNS_name>;UID=<windows_username>;PWD=<windows_password>;

在这种情况下,我收到了以下错误。

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

我已检入32位和64位DNS,指定的DNS名称在两个版本中均可用。

我错在哪里连接?指定连接字符串有什么问题吗?如果是,那么请建议正确的连接字符串,以便在VB.Net 2010 Express Edition中进行SQL Server的ODBC连接。

1 个答案:

答案 0 :(得分:0)

http://support.microsoft.com/kb/310985
DSN见第4点  将以下代码添加到DSN按钮:     Dim cn as OdbcConnection     cn =新的OdbcConnection(&#34; dsn = MyDSN; uid = sa; pwd = myPassword;&#34;)     Dim mystring As String =&#34;从客户中选择*&#34;     Dim cmd As OdbcCommand = New OdbcCommand(mystring)     cn.Open()     MSGBOX(&#34;连接&#34)     cn.Close()

 Standard security
    Driver={SQL Server Native Client 11.0};Server=myServerAddress;
    Database=myDataBase;Uid=myUsername;Pwd=myPassword;

您使用的是SQL Server 2012 Express吗?不要错过服务器名称语法Servername \ SQLEXPRESS,您可以使用SQL Server 2012 Express安装所在的计算机名称替换Servername。

-----------------------------------

Trusted Connection
    Driver={SQL Server Native Client 11.0};Server=myServerAddress;
    Database=myDataBase;Trusted_Connection=yes;
------------------------

Connecting to an SQL Server instance
The syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.
Driver={SQL Server Native Client 11.0};Server=myServerName\theInstanceName;
Database=myDataBase;Trusted_Connection=yes;

------------------
Prompt for username and password
This one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.
oConn.Properties("Prompt") = adPromptAlways

oConn.Open "Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase;"
------------------

Enable MARS
    Driver={SQL Server Native Client 11.0};Server=myServerAddress;
    Database=myDataBase;Trusted_Connection=yes;MARS_Connection=yes;
-------------------

Encrypt data sent over network
Driver={SQL Server Native Client 11.0};Server=myServerAddress;
Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;

-------------------

Attach a database file on connect to a local SQL Server Express instance
    Driver={SQL Server Native Client 11.0};Server=.\SQLExpress;
    AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

------------------------------
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
    Driver={SQL Server Native Client 11.0};Server=.\SQLExpress;
    AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
    Trusted_Connection=Yes;
-----------------
Database mirroring
If you connect with ADO.NET or the SQL Native Client to a database that is being mirrored, your application can take advantage of the drivers ability to automatically redirect connections when a database mirroring failover occurs. You must specify the initial principal server and database in the connection string and the failover partner server.
    Driver={SQL Server Native Client 11.0};Server=myServerAddress;
     Failover_Partner=myMirrorServerAddress;Database=myDataBase;
    Trusted_Connection=yes;
---------------