例外"未找到数据源名称"连接到现有DSN时

时间:2017-08-20 13:23:31

标签: sql sql-server odbc

我已经创建了一个ODBC连接(都是32/64位),其配置如下:

Microsoft SQL Server ODBC Driver Version 10.00.14393

Data Source Name: ODBCMSSQL
Data Source Description: 
Server: .\SQLEXPRESS
Database: MedicalMarketting
Language: (Default)
Translate Character Data: Yes
Log Long Running Queries: No
Log Driver Statistics: No
Use Regional Settings: No
Prepared Statements Option: Drop temporary procedures on disconnect
Use Failover Server: No
Use ANSI Quoted Identifiers: Yes
Use ANSI Null, Paddings and Warnings: Yes
Data Encryption: No

我想连接到本地MsSQL服务器,如下面的代码片段所示:

        string connectionString = "Data Source=ODBCMSSQL;Initial Catalog=MedicalMarketting;Integrated Security=True";
        con = new OdbcConnection(connectionString);
        cmd = new OdbcCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        try
        {
            this.con.Open();
            this.tr = con.BeginTransaction();
            this.cmd.Transaction = tr;
        }
        catch (Exception ex)
        {
                this.RollBack();
        }

这会抛出一个异常,其中包含如下错误消息:

  

错误[IM002] [Microsoft] [ODBC驱动程序管理器]未找到数据源名称且未指定默认驱动程序

很抱歉,如果这太基础了,但必须发布一条线索,因为不同ODBC连接的相同配置可以正常工作。

1 个答案:

答案 0 :(得分:0)

我通过将连接字符串更改为

来快速解决问题
string connectionString = "DSN=ODBCMSSQL";// best practice is to store this in a seperate config file.

实际上,指定的其他属性(初始目录,集成安全性)不是ODBC连接字符串属性,因此被忽略。可以在下面找到ODBC连接属性的完整列表。

https://msdn.microsoft.com/en-us/library/ee275047(v=bts.10).aspx