Why does this OdbcConnection throw me a System.InvalidOperationException? - c#

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.

Related

Authentication with old password no longer supported, use 4.1 style passwords

I have my website with hostgator and I want to access mysql database with C# windows application but when I tried to connect got this message:
"Authentication with old password no longer supported, use 4.1 style
password"
I have tried given solution:
SET SESSION old_passwords=0;
SET PASSWORD FOR user#host=PASSWORD('your pw here');
first query executed successfully but I got the error "Access denied for user#host" when second query executed. I can't understand why there is this problem. I am using MySQL-connecter-net 6.6.5.
I successfully connect my database with MySql workbench 5.2.47.
Can anyone help me what I can do more?
I have contact my hosting site and they make changes to my.cnf file to use 4.1 style password. and i am able to connect with mysql
mysql -u <username> -p <password> -h <hostname>;
but when i tried to connect with C# with mySQL connecter net 6.6.5 i again got this error.
I am using Visual Studio 2012 with MySQL connector 6.6.5 on Windows 8 64 bit. this is the code i used to connect
using MySQL.Data.MySQLClient;
private void button1_Click(object sender, EventArgs e)
{
string connStr = String.Format("server={0};port={1};uid={2};password={3};database={4}",
txtserver.Text, txtPort.Text, txtUser.Text, txtPassword.Text, txtDatabase.Text);
conn = new MySqlConnection(connStr);
try
{
conn.Open();
MessageBox.Show("Test Connection Succeded");
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
I can't understand where the problem is. Please Help me
I had the same problem. In my case just ran the command below connected on my database by the Workbench:
SET SESSION old_passwords=0;
SET PASSWORD FOR my_user=PASSWORD('my_password');
After that I could connnect using MySql Connector 6.6.5 in the c# code with the common mysql connection string:
"server=my_server_ip;user=my_user;database=my_db;port=3306;password=my_password;"
Today I had a similar problem in my C# application. I tried connecting to a server at hostgator.com (mysql) and connecting to localhost. After reading many posts and making many alterations, following every step but I couldn't connect to my hostgator mysql.
The solution in my case was to change the MySql Connector/NET library from version 6.* to version 5.*. After the change my application connected successfully.
While this might not be a viable solution for everyone, it may work for some.
I met this problem today, but I'v fixed it as below:
SET old_passwords=FALSE;
SET PASSWORD = PASSWORD('new pwd here');
Well, maybe the server's connection version is lower than the client, so you have to run this:"SET old_passwords=FALSE;"
Good Luck!
Had the same problem using MySqlClient package for C#. Let's break it down:
We need the database to not use old style passwords, thus we perform
SET old_passwords=0;
NOTICE there is no "SESSION" keyword. This is what allowed only your first query to work, "SESSION" implies temporary and any session settings are lost after a session ends.
Next we need to set the password of the user#host to the new password style
SET PASSWORD FOR user#host = PASSWORD('password');
This two should allow your MySqlClient
Just wanted to add to #diangelisj answer and say the root of the problem is the version of PhpMyAdmin installed on your server. In order to use the latest MySQL Data Connector class, you have to have the latest PhpMyAdmin (or issues arise). In my case, I have PhpMyAdmin 4.0.10.7 and tried using MySQL Data Connector 6.7.9, which gave the error.
What I did:
Download version 5.2.7 from https://dev.mysql.com/downloads/connector/net/6.9.html
Add .dll as reference to application
Presto!
Forget about applying SET old_password=0 since if the server is configured to use new password there is no reason to change the flag on the server if your app does not access anymore. Investigate the connector NET that you are using.
Connector 6.5.4 is the right one to use old_password, rencent version are using the new one.
The best practice would be to avoid to install the connector on the machine, just leave the NET to handle the dll if you already have it or anyway try to find MySQL.Data.DLL version 6.5.4.0 (384 kbytes)
I just solved my problem just like this:
1) Connect to the db with mysql workbench (select the "use old auth" and "send pass in cleartext"
2) Using the workbench, I ran the following several times (because it kept saying '0 rows affected'): (also change userID and password to your proper stuff)
SET SESSION old_passwords=0;
SET PASSWORD FOR userID=PASSWORD('password');
SET SESSION old_passwords=false;
SET PASSWORD FOR userID=PASSWORD('password');
3) Now go back to your app and it should run...
That is how I did it at least. I am using IX mysql and they do use old auth on their server so you must do something on your end...
I have not managed to solve the problem as the others who have responded, although I tried out everything that has been proposed.
In the end it turned out that the server on which stood my DB is not updated phpMyAdmin since version 3.5.5, and is currently being used everywhere version 4.1.14 and it is a problem.
The solution is to update your host phpMyAdmin or your own mySQL back to an earlier version in order to continue working on remote DB.
You may need this knowledge in order to solve some doubts that have arisen. :)
Today I found the same problem. I downloaded Workbench. There are some settings for workbench to connect database, as follows:
1.select tab SSL , select option 'no' in "Use SSL".
2. select tab advanced, select in Use the ole authentication Protocol.
after that Workbench working.
and follow tai1001' post.
That are all I found all steps that work.

Find connection string for connecting to MySQL Server 5.1.50 using OleDbConnection

I am interested how can I find the connection string in order to be able to connect to MySQL Server 5.1.50 using OleDbConnection(C#).
I used this auto generated string (after adding new data source in Visual Studio):
server=localhost;User Id=MyID;password=MyPassword;database=MyDatabase
but I always get the same error message:
An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.
I have tried different providers but neither of them seems to work.
Is there a reason you aren't using the MySQL .NET connector? Anyways, I think you need to add
Provider=MySQL Provider; to your connection string.
try this as your connection string:
Provider=MySQL Provider;server=localhost;User Id=MyID;password=MyPassword;database=MyDatabase;
The MySQL.NET connector fully implements the ADO.NET interface. Every command is identical to using the System.Data.SqlClient namespace.

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.

Crystal report - Running thru .NET

I created a crystal report using test a DB. I run the report using .NET ReportDocument class. Everything works fine until I connect to the test DB.
When same report is pointed to UAT DB (all required DB objects are available in UAT too), I am getting error. To fix this, I have to change the server name to UAT DB manually in the RPT file.
How to fix this?
A solution is to create a system DSN (ODBC) for connecting to your target database. You can then switch the ODBC to whichever database you want (local,test,stage etc). Also, if you ensure an ODBC connection of the same name is available on all your servers, moving the report from dev->test->stage->production should be easy.
Using push reports should solve your issue. You can set up the report to accept a strongly-typed ADO.NET Dataset, and supply that dataset at runtime.
Isn't that how it's supposed to work? It has to know to what DB it's connecting?
Maybe I'm missing something but it sounds like you just needed to get the connection right.

Categories