【问题标题】:ASP.NET not connecting to local databaseASP.NET 未连接到本地数据库
【发布时间】:2012-06-15 20:41:31
【问题描述】:

我正在尝试连接到名为“Remisiones”的 SQL Server 2012 数据库。我为确保连接字符串正确所做的工作是从项目属性中创建它(设置部分,我从那里添加了连接字符串并成功测试了连接)。这是生成的连接字符串:

<configuration>
    <connectionStrings>
        <add name="Remisiones.Properties.Settings.ConnString" connectionString="Data Source=ComputerName\SQLEXPRESS;Initial Catalog=Remisiones;Integrated Security=True"
        providerName="System.Data.SqlClient" />
    </connectionStrings>
    More configuration...
</configuration>

然后,使用它并像这样连接到数据库:

using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["Remisiones.Properties.Settings.ConnString"].ConnectionString))
{
    connection.Open();
    using (OdbcCommand command = new OdbcCommand("SELECT ID, Date FROM Remisiones", connection))
    using (OdbcDataReader dr = command.ExecuteReader())
    {
        while (dr.Read())
        {
            Result.Text += dr["ID"].ToString();
            Result.Text += "\n";
            Result.Text += dr["Date"].ToString();
            break;
        }
        Result.Text += "</table>";
    }
    connection.Close();
}

如您所见,这应该打印数据库中前两项的日期和 ID 列值。问题是,它给了我错误:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Odbc.OdbcException: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

我做错了什么?

编辑:错误发生在connection.Open();

【问题讨论】:

  • 为什么不使用 SqlConnection 而不是 OdbcConnection?
  • 我可能做错了程序

标签: asp.net visual-studio-2010 database-connection sql-server-2012


【解决方案1】:

您正在使用 System.Data.Odbc 连接和命令,但尝试将 System.Data.SqlClient 连接字符串传递给它。两者不可互换。

要么将连接字符串更改为 Odbc 版本,要么将代码更改为 SqlClient 版本。 (如果是 SQL Server 数据库,我会推荐后者。)

您的代码,使用选项 2 将更改为

您需要更改代码文件顶部的“使用”语句(在您的代码示例中不可见,但我确定它在那里)以包含

using System.Data.SqlClient;

而不是

using System.Data.Odbc;

然后下面的 using 语句应该可以工作(我没有检查它是否有语法错误,但如果我有错误,Intellisense 应该会提供帮助)

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Remisiones.Properties.Settings.ConnString"].ConnectionString))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand ("SELECT ID, Date FROM Remisiones", connection))
    using (SqlDataReader dr = command.ExecuteReader())
    {
        while (dr.Read())
        {
            Result.Text += dr["ID"].ToString();
            Result.Text += "\n";
            Result.Text += dr["Date"].ToString();
            break;
        }
        Result.Text += "</table>";
    }
    connection.Close();
}

大量的连接字符串示例,包括 System.Data.SqlClient 和 System.Data.OleDb 可以在 here 找到。

【讨论】:

  • 我必须将其更改为 sqlClient 字符串吗?
【解决方案2】:

您的连接字符串似乎是用于providerName="System.Data.SqlClient",但您的代码使用的是OdbcConnection - 更改为SqlConnection(和SqlCommand、SqlDataReader`等)应该可以解决您的问题

【讨论】:

    【解决方案3】:

    您需要在 odbc 配置工具(在控制面板中)中设置 odbc 连接。 或者,您可以使用 sqlconnection 而不是 odbc。

    【讨论】:

      【解决方案4】:

      您的连接字符串是SqlConnection 字符串而不是ODBCConnection 字符串。 ODBC 连接字符串如下所示:

      Driver={Microsoft Access 驱动程序 (*.mdb)};DBQ=C:\Samples\Northwind.mdb

      使用SqlConnection 或将您的连接字符串更新为有效的 ODBC 连接字符串。

      【讨论】:

        猜你喜欢
        • 2018-10-12
        • 2015-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 2016-07-19
        • 2016-03-17
        相关资源
        最近更新 更多