C
C#7mo ago
Janek

✅ Microsoft sql unable to connect. What I'm missing?

Hey! Does anybody know what I'm missing as I'm trying to connect to mysql? I get InvalidOperationExpection on SqlConnection.Open() function call. (System.InvalidOperationException: 'Internal connection fatal error.) Here is the ConnectionString: Data Source = "127.0.0.1, 3307"; Initial Catalog = mysql_db; User ID = root; Password = root Ip, port, db, username and password is correct as I was able to login as root use using MySQL Workbench software. GetBuilder function:
internal SqlConnectionStringBuilder GetBuilder()
{
return new SqlConnectionStringBuilder
{
DataSource = Server,
UserID = Username,
Password = Password,
InitialCatalog = Database
};
}
internal SqlConnectionStringBuilder GetBuilder()
{
return new SqlConnectionStringBuilder
{
DataSource = Server,
UserID = Username,
Password = Password,
InitialCatalog = Database
};
}
GetConnection function
internal SqlConnection GetConnection(SqlConnectionStringBuilder builder = null)
{
SqlConnection connection = null;
try
{
connection = builder == null ? new SqlConnection(GetBuilder().ConnectionString)
: new SqlConnection(builder.ConnectionString);
}
catch (Exception err)
{
Console.WriteLine($"ERROR::Db_Connection::GetConnection:: {err}");
}
return connection;
}
internal SqlConnection GetConnection(SqlConnectionStringBuilder builder = null)
{
SqlConnection connection = null;
try
{
connection = builder == null ? new SqlConnection(GetBuilder().ConnectionString)
: new SqlConnection(builder.ConnectionString);
}
catch (Exception err)
{
Console.WriteLine($"ERROR::Db_Connection::GetConnection:: {err}");
}
return connection;
}
SendQuery function calls GetConnection after connection.Open is called which gives me the error.
SqlConnection connection = GetConnection();
connection.Open();
SqlConnection connection = GetConnection();
connection.Open();
25 Replies
Pobiega
Pobiega7mo ago
SqlConnection can't be used with MySQL
Janek
JanekOP7mo ago
Wait, really?
Pobiega
Pobiega7mo ago
you will need to get a mysql connector library yes all sql servers have their own protocol SqlConnection only works with MSSQL (Microsoft SQL Server) and Express
Janek
JanekOP7mo ago
I see. I was wondering why the hell my code is not working. This explains everything. Thank you!
Pobiega
Pobiega7mo ago
MySqlConnector 2.3.7
A truly async MySQL ADO.NET provider, supporting MySQL Server, MariaDB, Amazon Aurora, Azure Database for MySQL, Google Cloud SQL, and more.
Janek
JanekOP7mo ago
@Pobiega Do you know why it tries to connect to 172.18.0.0.1 instead of 127.0.0.1?
Janek
JanekOP7mo ago
Connection string on top, and on Error it says 172.18.0.1
No description
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Janek
JanekOP7mo ago
But I don't really understand why it changes ip address to private from local.. This is something totally new to me
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Janek
JanekOP7mo ago
But I don't get why access is denied. I have full access on MySql Workbench with same username
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Janek
JanekOP7mo ago
password ? I used docker to set up mysql
services:
mysql:
container_name: mysql_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'admin'
MYSQL_PASS: 'admin'
volumes:
- mysql_db_data:/var/lib/mysql
ports:
- "3307:3306"
volumes:
mysql_db_data:
services:
mysql:
container_name: mysql_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_USER: 'admin'
MYSQL_PASS: 'admin'
volumes:
- mysql_db_data:/var/lib/mysql
ports:
- "3307:3306"
volumes:
mysql_db_data:
internal string Server { get; private set; } = "127.0.0.1, 3307";
internal string Username { get; private set; } = "root";
internal string Password { get; private set; } = "root";
internal string Database { get; private set; } = "mysql_db";
internal string Server { get; private set; } = "127.0.0.1, 3307";
internal string Username { get; private set; } = "root";
internal string Password { get; private set; } = "root";
internal string Database { get; private set; } = "mysql_db";
I can't really see what part is wrong..
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Janek
JanekOP7mo ago
ahh, I totally forgot that it can be different on different sql.. I used ; before (https://www.connectionstrings.com/sql-server-2008/) and after that I used ,
SQL Server 2008 connection strings - ConnectionStrings.com
Connection strings for SQL Server 2008. Connect using Microsoft.Data.SqlClient, SqlConnection, SQLNCLI11 OLEDB, SQLNCLI10 OLEDB, ODBC Driver 17 for SQL Server.
Pobiega
Pobiega7mo ago
MSSQL uses the comma, but mysql seems to prefer Port in general, avoid mysql - its not a very alternative these days
Janek
JanekOP7mo ago
I tried to get MSSQL to work with docker but no success, so I moved to mysql.
Pobiega
Pobiega7mo ago
curious i run mssql on docker myself and it works very well
Janek
JanekOP7mo ago
I'm new to this all, so I probably failed to write working docker-compose.yml. I also tried to google and felt like I found decent answers, but no success
Pobiega
Pobiega7mo ago
sqlserver:
image: mcr.microsoft.com/mssql/server
hostname: "sqlserver"
container_name: mssql
restart: "always"
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "aaaAAA!!!"
MSSQL_PID: Developer
ports:
- "1433:1433"
volumes:
- "C:/Docker/mssql/data:/var/opt/mssql/data"
- "C:/Docker/mssql/backup:/var/opt/mssql/backup"
- "C:/Docker/mssql/log:/var/opt/mssql/log"
sqlserver:
image: mcr.microsoft.com/mssql/server
hostname: "sqlserver"
container_name: mssql
restart: "always"
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "aaaAAA!!!"
MSSQL_PID: Developer
ports:
- "1433:1433"
volumes:
- "C:/Docker/mssql/data:/var/opt/mssql/data"
- "C:/Docker/mssql/backup:/var/opt/mssql/backup"
- "C:/Docker/mssql/log:/var/opt/mssql/log"
thats my docker-compose or well, the sql service part of it 🙂
Janek
JanekOP7mo ago
I found something similar, but it kept shutting down. Let me try again. Yeah same issue, running for like 10 seconds then shutting down :S
Pobiega
Pobiega7mo ago
try checking the logs?
Janek
JanekOP7mo ago
ohh, such a stupid error.. SQL Server password policy requirements because it is too short... Edit, seems like that is not all :S Got it to work. Thank you so much @Pobiega !
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX7mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server