QT Login Timeout expired QODBC3:Unable to connect ERROR - c++

I am trying to connect my SQL Server to the qt project with this code
QString servername = "OLI-PC";
QString dbname = "Translator";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
qDebug()<<"Dupa connection details";
QString dsn = QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;Trusted_Connection=Yes").arg(servername).arg(dbname);
db.setDatabaseName(dsn);
qDebug()<<"Dupa set data base";
if(db.open())
{
qDebug()<<"open";
}
else
{
qDebug()<<"Error = "<<db.lastError().text();
}
And it all worked as expected. I could open the database, read from it, perform certain queries, but not after I used a VPN, I can no longer connect to the database.
I have tried to disconnect from the VPN but with no success I still cannot connect.
The error code is:

One solution that worked for me was to change the driver to SQL Server Native Client 11.0:
DRIVER={SQL Server Native Client 11.0};
You can see the name of the drivers in ODBC Data Source Administrator.
check this post: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Related

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection'

i'm trying to connect my qt application to a Mysql database and don't know why it's showing the following error message : QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
can someone help me please, this is my code :
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("pmkfinal");
if(db.open()){
QMessageBox::information(this,"Connection","Database Connected Successfully");
}else{
QMessageBox::information(this,"Connection","Database not Connected Successfully");
}
}
Thank you All !!
Do not connect to the database repeatedly. Or you can provide an alternate connection name to avoid the warning message.

How to connect Qt Desktop application to mysql database running on AWS or any online mysql database

I am trying to connect a Qt desktop application to mySql db on AWS or online mysql database hosted on free domain. can somebody point me in the correct direction for the same?
I tried creating account on AWS and create mysql db but connection does not work
db = QSqlDatabase::addDatabase("QODBC");
userName = "sql9298899";
password = "**********";
databaseName = "sql9298899";
hostName = "sql9.freesqldatabase.com";
//create database connection here
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostName);
db.setUserName(userName);
db.setPassword(password);
db.setDatabaseName(databaseName);
DB.open always returns false with lastError as :
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified QODBC3: Unable to connect

Qt ODBC Microsot SQL Server 2014 issue which happens once a month

I have developed for windows server 2016, a server application written in C++ with Qt/ODBC/MS Sql Server 2014 which works well. Users log into the application with their login/password. The server application make requests to MS SQL server thanks to ODBC driver.
But about once a month, the login functionnality stops to work and it seems that the application cannot access to the database anymore.
The login process uses this peace of code :
SQLServerConnexion::SQLServerConnexion(const std::string &dbname)
{
dbname_ = QString(dbname.c_str());
database_ = QSqlDatabase::addDatabase("QODBC", generatedUUID().c_str());
database_.setDatabaseName(dbname_);
if (database_.open()) {
qDebug() << dbname_ << "opened";
}
else {
qDebug() << "Cannot open " << dbname_;
}
}
dbname follows the following pattern "DB_NAME; MARS_Connection=YES;"
When I restart the application, users can log into the application.
I don't understand why this issue happens about once a month. Have you got some ideas to solve that issue ?

Unable to connect to the AWS RDS database from ASP.NET Hangfire

I am developing an ASP.NET Core Web API project. In my project, I am using Hangfire to run the background task. Therefore, I am configuring the Hangfire to use the database like this.
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration =>
{
configuration.UseSqlServerStorage("Server=(LocalDB)\\MSSQLLocalDB;Integrated Security=true;");
});
//
}
In the above code, I am using Local DB. Now, I am trying to use AWS RDS database since I am deploying my application on the AWS Elastic Beanstalks. I created a function for getting the connection
public static string GetRDSConnectionString()
{
string dbname = "ebdb";
if(string.IsNullOrEmpty(dbname)) return null;
string username = "admin";
string password = "password";
string hostname = "cxcxcxcx.xcxcxcxc.eu-west-2.rds.amazonaws.com:1234";
string port = "1234";
return "Data Source=" + hostname + ";Initial Catalog=" + dbname + ";User ID=" + username + ";Password=" + password + ";";
}
I got the above code from the official AWS documentation. In the above code, what I am not clear is the database name, is the database name always be "ebdb"? I tried to find out the database name. But could not. In the tutorial, it is saying to use ebdb. So, I used it.
Then in configuration, I changed to this.
configuration.UseSqlServerStorage(AppConfig.GetRDSConnectionString());
When I run the code, it is giving me this error.
Win32Exception: The parameter is incorrect
Unknown location
SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, object providerInfo, bool redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, bool applyTransientFaultHandling)
Win32Exception: The parameter is incorrect
Basically, it cannot connect to the database when I run my application. But I set the correct credentials. the only thing I double is the database name (ebdb). What is wrong with my configuration and what is wrong? How can I fix it?
Calling a few things out here just incase...
You have your port specified both in your host variable and as a separate port variable...but never use port.
Can you confirm that you are able to access your SQLServer via another means, such as from SQL Management Studio?
RDS uses SSL by default now for connections, my .NET is rusty but would you need to inform the connection string that it needs to run over a secure protocol?
& finally, regarding the AWS Security Group on your RDS instance. Have you opened up the correct port to your machine/network/IP?
This is the screenshot of the RDS db instance security group section in the console.

Why am I not being able to connect to my database?

I've followed numerous tutorials to the tee, yet I still cannot connect to my database from Qt. I always get this error:
Closed! "[Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server does not exist or
access denied. [Microsoft][ODBC SQL Server Driver][DBMSLPCN]ConnectionOpen
(Connect()). [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
QODBC3: Unable to connect"
or if I use SQL NATIVE CLIENT (i.e. instead of SQL SERVER) as the DRIVER, I get the following error:
Closed! "[Microsoft][ODBC Driver Manager] Data source name not found and no default
driver specified QODBC3: Unable to connect"
Been sitting here for hours trying to figure this out, but I can't see what I'm doing wrong. The server, database, etc all seem to have the right names. I just don't see why it's not connecting.
Code below
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase ("QODBC","Chessgames");
db.setConnectOptions();
db.setDatabaseName("DRIVER={SQL SERVER};SERVER=MYNAME\\SQLEXPRESS;DATABASE=Chessgames;Uid=sa;Password=xxxxx;");
if(db.open())
{
qDebug() << "Opened!";
db.close();
}
else
{
qDebug() << "Closed! " << db.lastError().text();
}
}
login::~login()
{
delete ui;
}
Got it to open by changing the hostname to the ip address and port.
db.setDatabaseName("DRIVER={SQL SERVER};SERVER=192.168.X.X;Port=port#;
DATABASE=Chessgames;Uid=sa;Password=xxxxx;");