我试图连接到名为“Remisiones”的SQL Server 2012数据库。我所做的确保连接字符串正确的是从项目属性(设置部分,我从那里添加连接字符串并成功测试连接)创建它。这是导致连接字符串:ASP.NET未连接到本地数据库
<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? – Fabio
我可能做了错误的程序 – rdelfin