我正在尝试连接到名为“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();
行