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

问题描述 投票:0回答:1

我正在使用 Windows Server 数据中心。我正在使用 .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 连接。

sql-server vb.net dns odbc
1个回答
0
投票

http://support.microsoft.com/kb/310985
有关 DSN,请参阅第 4 点
将以下代码添加到 DSN 按钮: Dim cn 作为 OdbcConnection cn = 新 OdbcConnection ("dsn=MyDSN;uid=sa;pwd=myPassword;") Dim mystring As String =“从客户中选择*” Dim cmd As OdbcCommand = New OdbcCommand(mystring) cn.Open() 消息框(“已连接”) 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,其中将 Servername 替换为 SQL Server 2012 Express 安装所在的计算机的名称。

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

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;
---------------
© www.soinside.com 2019 - 2024. All rights reserved.