C
C#2y ago
rallez

Unable to connect to SQL

using System.Data.SqlClient;
Console.WriteLine("Database connector");
string connectionString = "Data Source=localhost,3306;Initial Catalog=testcon;User id=root;Password=~'!(J+P1W%YYM-0q9y~H;";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);

try
{
Console.WriteLine("Connecting...");
cnn.Open();
Console.WriteLine("Connected!");

}
catch(SqlException)
{
Console.WriteLine("Unable to connect to database!");
}
finally
{
cnn.Close();
}


Console.ReadLine();
using System.Data.SqlClient;
Console.WriteLine("Database connector");
string connectionString = "Data Source=localhost,3306;Initial Catalog=testcon;User id=root;Password=~'!(J+P1W%YYM-0q9y~H;";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);

try
{
Console.WriteLine("Connecting...");
cnn.Open();
Console.WriteLine("Connected!");

}
catch(SqlException)
{
Console.WriteLine("Unable to connect to database!");
}
finally
{
cnn.Close();
}


Console.ReadLine();
8 Replies
rallez
rallez2y ago
I think something maybe wrong with my connectiong string. I use xampp mysql
Anton
Anton2y ago
read the exception
rallez
rallez2y ago
how do i check that?
Anton
Anton2y ago
you're catching it in the catch block
rallez
rallez2y ago
yes i know but im not sure how to correctly use that. i catch any exception and just CW that connection was failed but how do i check exact error
Anton
Anton2y ago
look at the docs for try catch
codesandplays
codesandplays2y ago
Change the catch block to
catch(Sqlexception ex) {
Console.WriteLine(ex.message);
}
catch(Sqlexception ex) {
Console.WriteLine(ex.message);
}
and post the output here
rallez
rallez2y ago
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - Nawiązane połączenie zostało przerwane przez opr ogramowanie zainstalowane w komputerze-hoście.) I tried to rebuild solution, Restarted IDE but still same. I don't have any antyviruses. Windows defender has configured exceptions for my IDE I've tried adding
TrustServerCertificate=True
TrustServerCertificate=True
or
Encrypt=false
Encrypt=false
to my connection string but issue persist