InvalidOperationException When connecting to mySQL database in C# - c#

I'm currently using .NET Framework 4.8 and I can't seem to get into my database. The password and other connection info has been tested elsewhere and is valid, so does anyone know why I continue to get this exception?
System.InvalidOperationException:'Internal connection fatal error.'
When I run the following simple code?
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection("...");
conn.Open();
conn.Close();
I'm just trying to get into the database, not even query yet...
Any help would be greatly appriciated.

System.Data.SqlClient is the SQL Server driver. You need the MySql driver, which is available on NuGet.

Related

ASP.NET MVC C# change code from accessing a mySQL database to a MSSQL database

I have an application that I used to connect to a mySQL database. I have installed the MySQL.Data package and I use this code to build my connection:
services.AddScoped<System.Data.IDbConnection>((s) =>
{
IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("databasename"));
conn.Open();
return conn;
});
This works fine as I am able to read/write data from the database.
Now, I need to have the same application access a MS SQL database. The tables are all the same and the fields on each table are the same as well. I am having a hard time finding what I should change my code to (or what package I should include). When I remove the MySQL.Data package, the MySqlConnection function becomes invalid (as would be expected).
Any idea what package to include and what function to use to establish the connection?
Any assistance is greatly appreciated.
Thank you!
EDIT #1
I found an old stackoverflow post (I should have referenced it here but I closed the window) that talks about this very issue. The suggestion was to add
using System.Data.SqlClient;
and then change the assignment code to
IDbConnection conn = new SqlConnection(Configuration.GetConnectionString("databasename"));
I made these changes and now I get this error within the code:
Im at a loss as to how to resolve this. I verified that my project is in framework .NET core 3.1
Not sure about size of your project and detailed requirements but I would suggest to use libraries like:
Entity Framework - no SQL knowledge needed, can connect to any Db technology, almost no code changes required when switching between Db providers
or
Dapper - fast lightweight also supports multiple Db providers but you need to write correct SQL commands yourself specific for each Db technology you use

Connecting to an SQL database in c# [duplicate]

This question already has an answer here:
SqlConnection not being able to open the connection
(1 answer)
Closed 6 years ago.
I am trying to connect to my free sql database that I opened in: www.freesqldatabase.com/account/.
When using this www.phpmyadmin.co/ admin tool I can get into the databse, add tables and all that. But i can't connect to this database from my project.
using this code:
SqlConnection sql = new SqlConnection("Server=sql7.freesqldatabase.com;Database=sql7115***;User Id=sql7115***;Password =*****;");
sql.Open();
I get an SqlException saying that "Could not find the server".
Am I missing something?
By the way i am getting those warnings
and if i click "here" i see this.
P.S:
When i ping the host with the right port (that was sent to me with the email) I get a reply, so it is probably listening.
At the moment, the service you are using only offers MySql databases (though they intend offering MS SQL Server databases in the near future.
The problem is that the SqlConnection class you are using is deliberately tailored to MS Sql Server Databases, and - as I just said - that's not what you are talking to.
You will want to find an ADO.Net solution for MySQL - something like this.
Alternatively, you might be able to use OLEDB if you have suitable drivers installed...
You can use the NuGet Package MySql.Data. To use it in your project insert following in the Package Manager Console.
Install-Package MySql.Data
After that you can use the class MySqlConnection.
Don't forget to include the reference in your class!
using MySql.Data;
using MySql.Data.MySqlClient;
You should be able to establish a connection like this:
string connectionString = "server=sql7.freesqldatabase.com;user=sql7115***;database=sql7115***;password=******;";
MySqlConnection mySqlConnection= new MySqlConnection(connectionString);
mySqlConnection.Open();
For further information you can look in the tutorial.

How to Connect From WindowsApplication Sqlite Database

I have a Windows Application written in C#, using Sqlite3 as its database. I want to update my database in Host with my Windows Application database.
You can connect to an SQLite3 database using the following code.
SQLiteConnection db = new SQLiteConnection("Data Source=/path/to/file.db;Version=3;");
db.Open();
If you want to specify additional connection parameters, ConnectionStrings.com is always a good resource. Also, you might check out this question which is similar to yours.

Connecting to an external SQL Database using odbc commands and C#

I am trying to write C# code to access a database using odbc commands, the database is not local.
My connection string looks like that:
Driver={SQL Server};Server=serverName;Database=dbname;UID=username;PWD=apassword;
Somehow I cannot get the connection to open....
Can someone explain why? am i missing something in the string?
Try this ODBC connection string:
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;
as described here

SQL Server CE Database Connection Issues

I have an application with a database called voodoobase.sdf.
Using .NET Framework Data Provider for Microsoft SQL Server Compact 3.5
I can see it in Server Explorer and connect to it fine from there. The DB File is located in:
c:\Users\me\Documents\VisualStudio2010\Projects\testproj\voodoobase.sdf
The same named DB under Solution Explorer is said to reside at the same location.
c:\Users\me\Documents\VisualStudio2010\Projects\testproj\voodoobase.sdf
Assuming they are the same... why can my application which compiles successfully alwways crash with a connection error:
SqlConnection dbCon = new SqlConnection(Properties.Settings.Default.voodoobaseConnectionString);
dbCon.Open();
Throws an error on dbCon.Open() saying that could not get a connection to the SQL server. Let me know if further detail is required.
Do not use the SqlConnection class, but the SqlCeConnection class.

Categories

Resources