Qt calls the SQL server Stored Procedure and obtains the output parameters.

Source: Internet
Author: User

Qt calls the SQL server Stored Procedure and obtains the output parameters.

Recently, a new project needs to use qt5 to connect to the SQL server on another machine. Although there are similar articles on the Internet, there are still some issues that are rarely mentioned. Therefore, we will summarize them here:

You can refer to this article for qt connection to SQL server:

Qt uses ODBC driver to connect to SQL Server

If the SQL server is connected to another machine, you do not need to perform the First Step "enable ODBC driver". In addition, you can specify the database username and password by using Uid and Pwd, that is, the parameter of the original dsn can be written as follows:

QString dsn = QString("Driver={SQL Server};Server=%1;Database=%2;Uid=%3;Pwd=%4").arg(serverName).arg(dbName).arg(userName).arg(password);

"Trusted_Connection = yes" must also be deleted. Otherwise, an error is returned.

 

For details about how to call a stored procedure, refer to the following articles:

Qt calls stored procedures in Server SQL

QSqlQuery: how to write the prepare () parameter. In fact, as long as you first call the stored procedure in SQL Server Management Studio, the calling code is automatically generated, the EXEC part can be used directly after being simplified.

We recommend that you use the Oracle format placeholder, that is, the colon method such as argname, which is better than the ODBC format? Placeholders are much more convenient. In addition, if multiple output parameters are used, you can write them as follows:

query.prepare("exec PINSERTPC :arg1 output, :arg2 output");

 

Note that SQL server must traverse all result sets before passing query. boundValue obtains the value of the output parameter. The value obtained before traversal is still query. default value for bindValue. For details, refer to bool QSqlQuery: nextResult:

Note that some databases, I. e. Microsoft SQL Server, requires non-scrollable cursors when working with multiple result sets. Some databases may execute all statements at onceWhile others may delay the execution until the result set is actually accessed, And some databases may have restrictions on which statements are allowed to be used in a SQL batch.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.