Data source name not found and no default driver specified - c#

I'm getting this error when trying to open the connection in code as follows:
string queryString = "Insert into Table;
OdbcConnection connection = new OdbcConnection();
connection.ConnectionString = Settings.Default.STIMConnectionString;
OdbcCommand command = new OdbcCommand(queryString,connection);
connection.Open();
command.ExecuteNonQuery();
My Appconfig is as follows:
<add name="WindowsFormsApplicationTransducer.Properties.Settings.STIMConnection"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source="D:\Development\SS Observer II Decoder.mdb"" />
What am i doing wrong?

Since you are using the OdbcConnection, I think you need to include the "Driver" information in your connection string.

Data Source is not a valid connection string property. ODBC originally used a data source name, or DSN=dsnname, where the DSN was configured separately on the system. However you can alternatively specify the driver and driver-specific parameters, which in the case of the Microsoft Access driver is at the minimum the file name: Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\Development\SS Observer II Decoder.mdb.

See http://connectionstrings.com/ to see what you need to have.

Related

Moving Database from Sybase to SQL Server: ODBC not recognised named parameters

I'm updating an old project, moving its data source from Sybase to SQL Server. The data structures are identical (they're both copies of a commercial database - the Sybase version has been discontinued).
Because it's pretty old, it uses ODBC to connect. The connection strings originally specified a DSN. I've managed to connect to the SQL Server using both a raw connection string and a replacement DSN.
It executes code against the database by pulling command text from a config file and adding named parameters, like so:
var args = new Dictionary<string, object>()
{
{ "#myFirstParam", "myFirstValue" },
{ "#mySecondParam", "mySecondValue" }
}
IDbCommand cmd = Connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = Command.Timeout;
cmd.CreateParameters(Command.Parameters, args);
cmd.CommandText = [fetched from config]
IDataReader reader = null;
reader = cmd.ExecuteReader();
This all used to work fine with Sybase.
However, when I run it against SQL, when the reader executes I get the following error:
ERROR [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Must declare the scalar variable "#myFirstVariable".
I get this error whether I specify the connection as a DSN or a raw string. Stepping through the code, the parameter is certainly declared and has a valid value.
Reading up around this, it appears that this behaviour is to be expected. The Microsoft Documentation on ODBC connection says you can only use named parameters if you're calling a stored procedure, which I am not. Apparently the correct method is to put "?" characters in the command text and ODBC will insert the parameters by index.
If this is correct, I have no idea how this code ever worked with Sybase. I have another project which uses the same code for data loading, specifying named parameters and passing them into SQL command specified in config, but it's pointing at a different SQL Server database.
I presume this may be something to do with the configuration of the database, but I'm pretty lost on what to try from here. Any suggestions would be greatly appreciated.

OdbcConnection DataName and DataSource not found

I am using ASP.net to create a web app. I use OdbcConnection to connect to MySQL. But it seems to create the object but not it doesn't fill up the datasource and dataname fields. I have the famous error
[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Here is my code failing:
OdbcConnection MyConnection;
string MyConString = "DRIVER={MySQL ODBC 5.1 Driver};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=debug_user;" + "PASSWORD=password;" + "OPTION=3";
MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
I also use to have a connectionstring in my web.config:
<connectionStrings>
<add name="UTRN_DB_ConnectionString" connectionString="DRIVER={MySQL ODBC 5.1 Driver};Database=cjr_db;Server=localhost;UID=cjr_db_user;PWD=password;"/>
</connectionStrings>
I have the driver set in my Data Source Administrator with MySQL. I've uninstall and reinstall everything. I keep getting this error. I Just don't understand why I get this error while I still specify everything (port, etc...) in the connection string and it keep failing
EDIT:
Here is my web.config
<connectionStrings>
<add name="UTRN_DB_ConnectionString" connectionString="Driver={MySQL ODBC 5.1 Driver};Database=cjr_db;Server=localhost;Uid=cjr_db_user;Pwd=password;providerName=(System.Data.Odbc)"/>
</connectionStrings>
and this is how i read it:
string conString = WebConfigurationManager.ConnectionStrings["UTRN_DB_ConnectionString"].ConnectionString;
string conString2 = ConfigurationManager.ConnectionStrings["UTRN_DB_ConnectionString"].ConnectionString;
using (OdbcConnection connection = new System.Data.Odbc.OdbcConnection(conString2))
{
connection.Open();
...
}
The connection string must contain Provider and Data Source as:
string MyConString = "Provider=(Data Provider);Data Source=(Path of source file)"
You have some spelling mistakes in your connection string in the web.config file. It should be Driver instead of DRIVER, Pwd instead of PWD, Uid instead of UID and so on.
Please change your code to the one below
Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
I found the answer of my problem here:
tadaaaam
Basically, you have to seek the difference of your 64bits and 32bits installed on your machine. By default VS is seeking for the 32. And in your ODBC Sources Manager it shows up the 64.
So you either configure your project to seek for the 64 OR install the 32 bits version of MySQL ODBC.

Difference between SqlConn and ODBC?

I have a control which requires SQCN connection. So I supplied it with proper credentials and there's nothing wrong with it.
string connection = "Data Source=(local);Initial Catalog=test;user ID=sa; Password=sa12345;";
SqlConnection sqcn = new SqlConnection(connection);
sqcn.Open();
Now due to requirements I need to change the dbConnection from SqlConnection to OdbcConnection. I used the same connection and replace Sql with Odbc but it shows an error
string connection = "Data Source=(local);Initial Catalog=test;user ID=sa; Password=sa12345;";
OdbcConnection odbc= new OdbcConnection(connection);
odbc.Open();
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Can someone pls tell me if im missing something.. Tnx!
You should install the proper driver for ODBC and define the data source. You can see data sources in administrative tools -> ODBC Data Source, also you can define data source here and reference it in your program via its Name.

C# ODBC ConnectionString

I was currently create a ODBC Connection to the remote server of the web-hosting.
The Access File is at the ftp home directory.
When running in this code , at the m_connection.Open();
var m_result = new DataTable();
try
{
using (OdbcConnection m_connection = new OdbcConnection(connectionDBString))
{
string sql = "SELECT * FROM product";
m_connection.Open();
OdbcDataAdapter dataadapter = new OdbcDataAdapter(sql, m_connection);
dataadapter.Fill(m_result);
m_connection.Dispose();
m_connection.Close();
}
}
catch (Exception e)
{
}
return m_result;
The following exception fails
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not
found and no default driver specified
Is there anyway to claim the dataSource for the ODBC Connection ?
In the cPanel, I only set the DSN and the Path as follows:
DSN : wealthhonesthk-website
Path : e:\virtualhost\domains\wealthhonesthk\home\website.mdb
The below is my part of the web.config
<connectionStrings>
<add
name="ODBCDataConnectionString"
connectionString="DSN=wealthhonesthk-website;Driver={Microsoft Access Driver (*.mdb)};FILEDSN=ftp:/210.245.166.72/home/website.mdb;Dbq=ftp:/210.245.166.72/home/website.mdb;Uid=;Pwd=; curly=false;"
providerName="System.Data.Odbc"
/>
</connectionStrings>
You tend to use a DSN when you don't define a connection string in a config file (like in old VB6 days). Since you're using a config file, you really don't need a DSN. Why not use a connection string like:
Driver={Microsoft Access Driver (*.mdb)};Dbq=ftp://210.245.166.72/home/website.mdb;Uid=Admin;Pwd=;?
Also, having your MDB file mapped to a FTP location may be a problem; why not map a network drive to that location? This way, your connection string might look like:
Driver={Microsoft Access Driver (*.mdb)};Dbq=X:\myNetworkLocation\website.mdb;Uid=Admin;Pwd=;
Lastly, why are you defining a FILEDSN and a Dbq parameter for your connection string?
In short, I'd recommend getting rid of the DSN part of your connection string and use a non-ftp location for your MDB file.
Here is all the information you need on connection strings:
http://www.connectionstrings.com/access
http://www.connectionstrings.com/access-2007
Have a look at following
http://www.connectionstrings.com/
Should help you.
As you are trying to connect to Oracle Database, you might need to have Oracle Client installed.

Can't create a sql connection due to the fact that it won't rcognize the data source keyword

Hello I'm trying to run a simple sql command on a DB from MS VS C# 2010 and I have encountered a error I have never seen before the relevant code is:
SqlConnection comCon = new SqlConnection(#"Data Source=C:\\Users\\George\\Desktop\\programming\\C#workspace\\Projects\\Examen\\Examen\\Companie.mdf;Initial Catalog=Proiect;Integrated Security=True"); 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE Proiect SET Buget = Buget + 500 WHERE (Buget > 0)";
cmd.Connection = comCon;                                                      
comCon.Open();
Console.WriteLine(cmd.ExecuteNonQuery().ToString());
comCon.Close();
And the error is Keyword not supported: 'data source'
The main problem is that I'm not used to creating these sqlconnections by hand so please tell me if I'm missing something.
You are using the wrong structure. To attach a database file, you need to use the following structure:
SqlConnection sqlConnection =
"Server=DatabaseServerName;AttachDbFilename=d:\Database\Database.mdf;
Database=DatabaseName; Trusted_Connection=Yes";
You need to have the right permissions on both the target file and database server to attach the databse and establish the connection.
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
If it's not an ASP.NET application don't use the DataDirectory syntax and just use the full c:... path.

Categories

Resources