Why is this OdbcConnection throwing a System.InvalidOperationException at me?
I am creating a Winforms C # 2.0 application.
I was successfully able to connect to my SLQ Server database using the following: m_connexion = new SqlConnection ("server = 192.168.xxx.xxx; uid = ...; pwd = ...; database = ...");
Since my company wanted to use any database I kept using the Odbc driver and my commands continued like this:
m_connexion = new OdbcConnection ("server = 192.168.xxx.xxx; uid = ...; pwd = ...; database = ...");
However, this throws a System.InvalidOperationException exception. Any idea why?
I am also trying to use DSN, but comment out OdbcConnection = new OdbcConnection ("DSN = MyDataSourceName"); suggested here but it also throws my System.InvalidOperationException
a source to share
I think you need to specify the driver. Look here for details: http://connectionstrings.com/sql-server-2005#21
a source to share
If you specify a DSN, you need to configure the DSN using the ODBC control panel. It is called "Configuring Data Sources (ODBC)" in the Administration section. The panel also has a "test" button that can tell you more about what is going wrong.
PS Being "database independent" is much more than using an ODBC connection, command and datareader. You have to make sure that your queries run on every target database, which you cannot do if you don't have a test server. So if I were you, I would code it using SqlConnection, since you already got this working.
a source to share