OdbcConnection DataName and DataSource not found - c#

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.

Related

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# code connection to mysql in localhost. Is provider wrong?

I have already installed mysql connector 6.7.4 and following reference is already added in code
using MySql.Data.MySqlClient;
I am using this string to connect database
string connStr = ("Provider = MySQLProv; SERVER =localhost; DATABASE=test;User ID=root;Password=");
OdbcConnection conn1 = new OdbcConnection();
conn1.ConnectionString = connStr;
OdbcCommand cmd1 = conn1.CreateCommand();
cmd1.CommandText = "SELECT * FROM patientinfo WHERE medicareNo = " + "" + p.getMedicare() + "";
conn1.Open();
It says,
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
What did i do wrong ?? I have sqltest database with the required table.
update: odbc connector 5.2 is installed and used as:
using System.Data.Odbc.OdbcConnection
When installing your MySql Server did you install the service in a specific port? this could be a potential problem, just in case try this connection string which specifies the port
3306 is the default MySql port, yours may differ if you specified it during installation.
Server=yourServerName;Port=3306;Database=test;Uid=root;
Pwd=yourPassword;

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.

C# Error in accessing an Access Database over LAN

Here's my connection string:
sConnection = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\" + lstNet.SelectedItem.ToString() + "\SharedDocs\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
lstNet is a listbox that contains all computers found in the network.
I'm assuming something else is wrong with my connection string.
According to Connection Strings website, to access a database over LAN, the following connection string format is used:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\serverName\shareName\folder\myDatabase.mdb;User Id=admin;Password=;
I'm assuming that shareName is where my Connection String fails. What is a shareName? And what are the shareNames of Windows XP and Windows Vista / 7 if, say, I placed my database in their Shared Documents / Public Files?
I've tried modifying my connection string into the following:
\C$\Users\Public\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
And I still get the following error:
"Format of Initialization string does not conform to specification"
May I have some help on this, please?
EDIT: Tried accessing the database in the Public\Documents section of a Windows Vista PC on my network with the following connection string:
\Public\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
I also tried to access my own (Windows 7 PC) local Public\Documents section using the same connection string, since the serverName can be changed using the program.
Still nothing.
Try this:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\serverName\shareName\folder\myDatabase.mdb; Jet OLEDB:Database Password=g3n3r4l;Persist Security Info=False;"
You must need to test first if you can access the shared path folder on client P.C. And if it can access it there will be no problem.Make sure also that the client user is administrator so it can do CRUD using you app.
Regards
Well, I actually solved it. Wow.
It turns out, there was an extra " at the very end of the connection string from the .ini file.
//Try This One...
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
Con = new OleDbConnection(#constr);
Con.Open();
Com = new OleDbCommand();
Com.Connection = Con;

Data source name not found and no default driver specified

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.

Categories

Resources