connect to SQL Server Management studio
-
hello friends
i want connect my Qt to SSMS and try this code:QSqlDatabase data1; data1 = QSqlDatabase::addDatabase("QODBC"); data1.setConnectOptions(); QString servername="DESKTOP-J2F5IR3\\SQLEXPRESS"; QString dbname="NRTL_B"; QString dsn = QString("DRIVER={SQL Native Client};Server=%1;Database=%2;Trusted_Connection=True;").arg(servername).arg(dbname); data1.setDatabaseName(dsn); if(data1.open()) { qDebug() << "opened"; } else { qDebug() << data1.lastError().text(); }
but it not opened and i received:
QSqlQuery::exec: database not open
how can i fix that??
-
@SeyMohsenFls
I don't think you mean "connect my Qt to SSMS"/"connect to SQL Server Management studio". You can't do that. SSMS is a GUI application interface to MS SQL. You are just talking about connecting to a SQL Express instance.QSqlQuery::exec: database not open
You do not show any
QSqlQuery
. If you execute one against a non-opened database that is the error you will get.qDebug() << data1.lastError().text();
I am astounded you do not say what this produces, since you know this is where the issue lies.
-
@SeyMohsenFls said in connect to SQL Server Management studio:
"DESKTOP-J2F5IR3\SQLEXPRESS"
Apart from @JonB's observations I would guess this string is wrong since I think a server name needs a double
\\
and not a single one. -
@Christian-Ehrlicher
Actually I don't think so. SQL Server instance names are addressed asSERVER_NAME\INSTANCE_NAME
. The OP'sDESKTOP-J2F5IR3\SQLEXPRESS
looks good.I would have hoped
qDebug() << data1.lastError().text()
would give something other thanQSqlQuery::exec: database not open
, unless the OP is saying that's what he gets. -
Are you relying on Windows authentication, or should you be specifying username and password?
Is the code running on DESKTOP-J2F5IR3 or are you trying to connect to a remote machine? SQL Express does not allow remote connections by default.
Is the server listening on the default port?
Is a firewall blocking access to that port?You could also try ".\SQLExpress", "localhost\SQLEXPRESS", or "(local)\SQLEXPRESS" if running on the same machine
-
If you have the instance running on a different computer you will likely have to adjust settings to accept remote connections and ensure the port is not being blocked by the firewall. Also its been my experience if you have one instance of SQL server you dont always need to specify that instance name (i.e. SQLEXPRESS is not needed).
Are you able to connect to the instance just fine using SSMS?