ODBC ConnectionString - c#

I have written a program in C# to pull some data using OdbcConnection :
using System.Data.Odbc;
......
OdbcConnection OdbcConn =
new OdbcConnection(Properties.Settings.Default.ConnectionString);
OdbcCommand cmd = new OdbcCommand();
//open connection
if (OdbcConn.State != ConnectionState.Open)
{
OdbcConn.Open();
}
In my settings file, I have this ConnectionString:
Dsn=****;uid=userID;pwd=password
However I cannot establish a connection. I have an iseries access driver from IBM corp installed, but if I try MS access then I am able to connect. Any suggestions?

When in doubt (and it involves connections strings): http://www.connectionstrings.com/

On a Windows 64 bit machine, make sure you check if your C# code is compiled in x86 (32-bit), x64, or "Any CPU". Note that if you compile as "Any CPU," it'll choose x64 bit drivers by default.
The 32-bit drivers can be found at C:\windows\SysWOW64\odbcad32.exe. The 32-bit drivers can be found at C:\windows\system32\odbcad32.exe.
First, make sure you verify your connection works with the ODBC Data Source Administrator using the paths I provided earlier. I.e. make a DSN and test it as Turbot suggested. Once you verified this connection works, your connection string can either use the DSN you just created or you can use a DSN free connection string.
For a quick reference, here is a sample of a DSN free connection string using a ODBC driver:
Driver={Progress OpenEdge 11.3 Driver};HOST=wfdscr11.wf.local;Port=1234;DB=MyDatabaseName;UID=John;PWD=Doe
In this example, I had to connect to a Progress database from my C# code and this is the connection string I used without having to specify a DSN. You can see below that the name of the driver is "Progress OpenEdge 11.3 Driver."

I always like to verify the connection using Data source(ODBC) in control panel (assume you are in window environment). Make sure you see the drive available in your ODBC selection and follow the steps to test the connectivity.
as also mentioned above the connections strings website would give you idea what properties and format on which particular driver connectivity

Related

PostgreSQL Connection String with ODBC driver in C#, Keyword not supported: driver

Lately I've been trying to connect a PostgreSQL 9.6 database on my C# project but I've been struggling setting up the connection string correctly due to "Keyword not supported:driver".
After having read tons of information, I downloaded the ODBC.NET, I registered PostgreSQL in ODBC Data Sources (32x), referenced the Microsoft.Data.Odbc.dll in my project's directory and managed to have a successful connection with my database in the Visual Studio's Ultimate 2013, but when it came to accessing the data something went wrong.
This is my connection string and a glimpse of my project:
Furthermore this is my data source information:
If you want to use ODBC to connect to PostgreSQL database use OdbcConnection
You can then instantiate a connection using DSN as ODBC data source identifier.
OdbcConnection DbConnection = new OdbcConnection("DSN=PostgreSQL30");
as specified here
There are also other ways to connect to PostreSQL without using ODBC driver. You might want to look at Npgsql .net library or EntityFramework with Npgsql library. More info about usage can be found here

connect to access databae without dsn

I am using the following connection string to connect with my access database from vb.net application, but not get connected.
Driver={Microsoft Access Driver (*.mdb)}; Dbq=D:\Projects\tempdb.mdb
It shows me following error
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
My code is below:
Dim odbcConn As OdbcConnection = New OdbcConnection(m_connectionString)
'' Build thr odbc Dataadapter
Dim odbcAdpt As OdbcDataAdapter = New OdbcDataAdapter(QueryToExecute, odbcConn)
odbcAdpt.SelectCommand.CommandType = CommandType.Text
I had connection problems.
My project was not connecting to database because there were no default driver.
I have solved this problem.
If you are using 64-bit OS you must use 64-bit MS office. similarly if you are using 32-bit OS you must need 32-bit MS office.
Be sure that you have all application of same number of bits.
Hopefully it will solve your problem.

How to Connect Crystal Reports to MySQL directly by C# code without DSN or a DataSet

How can I connect a Crystal Report (VS 2008 basic) to a MySQL DB without using a DSN or a preload DataSet using C#?
I need install the program on several places, so I must change the connection parameters. I don't want to create a DSN on every place, nor do I want to preload a DataSet and pass it to the report engine. I use nhibernate to access the database, so to create and fill the additional DS would take twice the work and additional maintenance later. I think the best option would be to let the crystal reports engine to connect to MySQL server by itself using ODBC.
I managed to create the connection in the report designer (VS2008) using the Database Expert, creating an ODBC(RDO) connection and entering this connection string
"DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain"
and in the "Next" page filling the "User ID", "Password" and "Database" parameters. I didn't fill the "Server" parameter. It worked. As a matter of fact, if you use the former connection string, it doesn't matter what you put on the "Server" parameter, it seems the parameter is unused. On the other hand, if you use "DRIVER={MySQL ODBC 5.1 Driver}" as a connection string and later fill the "Server" parameter with the FQDN of the server, the connection doesn't work.
How can I do that by code? All the examples I've seen till now, use a DSN or the DataSet method. I saw the same question posted but for PostgreSQL and tried to adapt it to mysql, but so far, no success. The first method:
Rp.Load();
Rp.DataSourceConnections[0].SetConnection("DRIVER={MySQL ODBC 5.1 Driver};SERVER=myserver.mydomain", "database", "user", "pass");
Rp.ExportToDisk(ExportFormatType.PortableDocFormat, "report.pdf");
raise an CrystalDecisions.CrystalReports.Engine.LogOnException during ExportToDisk
Message="Logon failed.\nDetails: IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.\rError in File temporal file path.rpt:\nUnable to connect: incorrect log on parameters.
the InnerException is an System.Runtime.InteropServices.COMException with the same message and no InnerException
The "no default driver specified" makes me wonder if the server parameter is unused here too (see above). In that case: How can I specify the connection string?
I haven't tried the second method because it doesn't apply.
Does anybody know the solution?
I think it'll likely be quicker to generate the Dataset via nHibernate, or do a direct ADO.NET query, then trying to solve the issue.

Turning on multiple result sets in an ODBC connection to SQL Server

I have an application that originally needed to connect to Sybase (via ODBC), but I've needed to add the ability to connect to SQL Server as well. As ODBC should be able to handle both, I thought I was in a good position.
Unfort, SQL Server will not let me, by default, nest ODBC commands and ODBCDataReaders - it complains the connection is busy (Connection is busy with results for another command).
I know that I had to specify that multiple active result sets (MARS) were allowed in similar circumstances when connecting to SQL Server via a native driver, so I thought it wouldn't be an issue.
The DSN wizard has no entr
y when creating a SystemDSN.
Some people have provided registry hacks to get around this, but this did not work (add a MARS_Connection with a value of Yes to HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\system-dsn-name).
Another suggestion was to create a file-dsn, and add "MARS_Connection=YES" to that. Didn't work.
Finally, a DSN-less connection string. I've tried this one (using MultipleActiveResultSets - same variable as a Sql Server connection would use),
"Driver={SQL Native Client};Server=xxx.xxx.xxx.xxx;Database=someDB;Uid=u;Pwd=p;MultipleActiveResultSets=True;"
and this one:
"Driver={SQL Native Client};Server=192.168.75.33\\ARIA;Database=Aria;Uid=sa;Pwd=service;MARS_Connection=YES;"
I have checked the various connection-string sites - they all suggest what I've already tried.
I should state that I've tried both the SQL Server driver, and the SQL Server native driver...
According to the SNI documentation on Using Multiple Active Result Sets (MARS):
The SQL Server Native Client ODBC
driver supports MARS through additions
to the SQLSetConnectAttr and
SQLGetConnectAttr functions.
SQL_COPT_SS_MARS_ENABLED has been
added to accept either
SQL_MARS_ENABLED_YES or
SQL_MARS_ENABLED_NO, with
SQL_MARS_ENABLED_NO being the default.
In addition, a new connection string
keyword, Mars_Connection, as been
added. It accepts "yes" or "no"
values; "no" is the default.
Make sure your client loads the right drivers, use Mars_Connection=yes, and validate in the app by checking SQL_COPT_SS_MARS_ENABLED on SQLGetConnectAttr.

Why does this OdbcConnection throw me a System.InvalidOperationException?

I am building a Winforms C# 2.0 application.
I have successfully been able to connect to my SLQ Server database using the following:
m_connexion = new SqlConnection("server=192.168.xxx.xxx;uid=...;pwd=...;database=...");
Because my company wanted to be able to use any database, I went on to use the Odbc driver and my commands went on like this:
m_connexion = new OdbcConnection("server=192.168.xxx.xxx;uid=...;pwd=...;database=...");
However, this throws out a System.InvalidOperationException. Any idea why?
I'm also trying to use a DSN, but the commend
OdbcConnection connection = new OdbcConnection("DSN=MyDataSourceName"); suggested here but it likewise throws my a System.InvalidOperationException
The connection string needs a Provider= so that the ODBC drivers know which server you're connecting to. In this case Provider=SQLSERVER I believe.
UPDATE: Should have been Provider=SQLOLEDB
I think you need to specify a driver. Look here for details: http://connectionstrings.com/sql-server-2005#21
If you specify a DSN, you have to configure the DSN using the ODBC control panel. It's called "Set up data sources (ODBC)" under Administrative Tools. The panel also has a "test" button, which might tell you more about what's going wrong.
P.S. Being "database independent" is much more work than using ODBC connection, command and datareader. You'd have to make sure your queries run on each target database, which you will not be able to do if you don't have a test server of each. So if I were you, I'd code it up using SqlConnection, since you already got that working.

Categories