Connecting to an SQL database in c# [duplicate] - c#

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.

Related

InvalidOperationException When connecting to mySQL database in 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.

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

Problems with SQLite ADO.net connection and Deployment

Because of what seem to be overwhelming difficulties around Entity Framework, I’m in the process of moving to Dapper. I have an application that uses both a remote MS SQL Server database and a small local database (SQLite). Dapper against the MS SQL Server database is working great, but for some reason I can’t get SQLite connection working.
From the way I understand it, Dapper simply needs an ADO connection. I installed a NuGet package “sqlite-net”, and used the following code to create the connection:
SQLiteConnection con = new SQLiteConnection(“data source=C:\Data\OneDrive\Data\TestDB.sqlite”);
and
SQLiteConnection con = new SQLiteConnection(“data source=C:\Data\OneDrive\Data\TestDB.sqlite; Version=3”);
Neither one of these work and they throw an exception “Could not open database file”. The file SQLite.cs (coming from the NuGet package) threw the error.
Because of my past problems with deployment, I’ve hesitated installing the downloaded files from System.Data.sqlite.org.
Please can someone help me?
Looking at the nuget package and specifically the SQLite.cs file:
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs
I see the following constructor definition (line 180):
public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true)
: this (databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks)
{
}
This shows that the first argument is just the path, and should be called like:
SQLiteConnection con = new SQLiteConnection(#"C:\Data\OneDrive\Data\TestDB.sqlite”);
The # sign escapes the \ characters from being interpreted as character codes. These two issues look like the problem that you have and I would try it out and see if it works.

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.

Create connection to database in VS2010 without SQL Client - dynamically

How to create connection to database in VS2010 without SQL Client dynamically?
I tried using ConnectionStringSettings and SqlConnection and I succeeded, but I'm not supposed to use SQL Client.
I'm using Massive.
You should be able to use System.Data.OleDb.OleDbConnection.
Use a connection string like this:
Provider=SQLOLEDB;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;
It might be that you just miss to reference System.Configuration in your project and consequently Massive cannot find the connection string in your app.config.
Following the instructions on github I could query my SQL Express database without any problems using Massive having a plain SqlClient connection string in my app.config.

Categories

Resources