I am trying to connect to an SQL Server 2012 database called “Remisiones”. What I did to make sure that the connection string was correct was to create it from the project properties (Setting section, I added the connection string from there and tested connection successfully). This was the resulting connection string:
<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>
Then, to use it and connect to the database like so:
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();
}
This as you can see, should print the Date and ID column values of the first two items in the database. The problem is, instead it gives me the error:
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
What did I do wrong?
EDIT: The error happens in the connection.Open(); line
You’re using a System.Data.Odbc connection and command, but trying to pass it a System.Data.SqlClient connection string. The two are not interchangeable.
Either change your connection string to the Odbc version, or change your code the SqlClient version. (If it’s a SQL Server DB I’d recommend the latter.)
Your code, using option 2 would change to
You’ll need to change your “using” statement at the top of the code file (not visible in your code sample, but I’m sure it’s there) to inclue
instead of
and then the following using statement should work (I didn’t check it for syntax errors, but Intellisense should help if I’ve got mistakes)
Tons of connection string samples, including the System.Data.SqlClient and System.Data.OleDb can be found here.