无法通过 ODBC 连接到 SQL 服务器

Cannot make connection to SQL Server via ODBC

以前用ADO访问SQL服务器,ADO连接对象的连接字符串是:

Provider=sqloledb;Data Source=MYPC;Integrated Security=SSPI;

其中 MYPC 是我的计算机名称,SQL服务器作为默认实例安装在我的计算机上。

以上连接字符串运行良好。

但是,现在据说ADO已经过时了,微软再次推荐ODBC(参见https://blogs.msdn.microsoft.com/sqlnativeclient/2011/08/29/microsoft-is-aligning-with-odbc-for-native-relational-data-access/),所以我不得不修改我的代码以改用ODBC。

所以我将与SQL服务器的连接更改为以下代码:

    CDatabase Database;

    //  Provider=sqloledb;Data Source=MYPC;Integrated Security=SSPI;
    Database.OpenEx(_T("Driver = {SQL Native Client}; Server = MYPC; Trusted_Connection = yes;"), CDatabase::noOdbcDialog);

    Database.ExecuteSQL(_T("create database [MyDB2019] on primary (name=[MyDB2019_File],filename='F:\MyDB2019.mdf')"));

    Database.Close();

但是,此代码不起作用。执行完Database.OpenEx后会有CDBException提示

Data source name not found and no default driver specified.

为什么?

注意:我使用的是 Visual C++ 2008 和 ADO

创建一个扩展名为 udl 的文件。示例:test.udl 双击该文件。使用 GUI 连接到您的数据库。完成后,使用记事本打开 UDL 文件并提取连接字符串。

https://www.dmxzone.com/go/312/how-to-generate-an-ado-connection-string/

error/exception 指出了问题所在(尽管一开始看起来有点笼统)。

"Data source name not found and no default driver specified"

Data source name not found --> You did not specify a DSN, you do not want to use a DSN, ignore this part

.

and no default driver specified --> You intend to use a driver, so this part of the error most likely applies to you.

The connection string appears syntactically correct: "Driver={Driver Name}...", so the next step is to check whether the driver you try to use, named SQL Native Client, exists on your machine.

"SQL Native Client" was/is SQL Server 2005 的驱动程序名称。 从 SQL2008 年起,驱动程序 name 获得了 version descriptor

Driver={SQL Native Client}             ,sql2005   
Driver={SQL Server Native Client 10.0} ,sql2008  
Driver={SQL Server Native Client 11.0} ,sql2012 and later  
Driver={ODBC Driver 11 for SQL Server} ,sql2012 and later, odbc appears in the name
Driver={ODBC Driver 13 for SQL Server}  
Driver={ODBC Driver 17 for SQL Server}

查找计算机上已安装的 'SQL Native' 和 'ODBC Driver for SQL Server' 驱动程序的一种简单方法是 运行 ODBC 数据源管理器。在搜索框中键入 Odbc 数据源或在 command/address 栏中键入 odbcad32.exe(对于 64 位:%windir%\system32\odbcad32.exe)

在 ODBC 数据源管理器中,切换到“驱动程序”选项卡,您会在那里找到所有 available/installed 个驱动程序,供您使用。

这是一个 powershell 脚本,它使用 odbc 打开到本地主机的连接。根据您安装的驱动程序进行相应调整

cls
$conn = new-object system.data.odbc.odbcconnection 
$conn.connectionstring = 
# all these installed on my pc and working
#"Driver={SQL Server Native Client 11.0};Server=localhost; Database=master;Trusted_Connection=yes;" 
#"Driver={SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;" 
#"Driver={ODBC Driver 11 for SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;" 
"Driver={ODBC Driver 17 for SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;" 

$conn.Open()
$conn.State
$conn.Close();

..和一个 mfc 按钮

void CMFCDBTestDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here
    CDatabase database;
    CString connectionstring = _T("Driver={SQL Server Native Client 10.0};Server=localhost;Trusted_Connection=yes;");
    CString messagetext;

    TRY{
        //database.Open(NULL, FALSE, FALSE, connectionstring, FALSE); //ok
        database.OpenEx(connectionstring, CDatabase::noOdbcDialog); //ok

        if (database.IsOpen()){
            messagetext = _T("open, database:") + database.GetDatabaseName();
            database.Close();
        }
   }CATCH(CDBException, e) {
       messagetext = _T("Database error: ")+e->m_strError;
   }
   END_CATCH;

   AfxMessageBox(messagetext, 0, 0);
}

问题出在连接字符串中的多余空格,从以下位置删除多余空格后:

"Driver = {SQL Native Client}; Server = MYPC; Trusted_Connection = yes;"

到(并更改驱动程序名称):

"Driver={SQL Server Native Client 10.0};Server=MYPC;Trusted_Connection=yes;"

连接成功。