当前位置:Gxlcms > 数据库问题 > qt调用sql server存储过程并获取output参数

qt调用sql server存储过程并获取output参数

时间:2021-07-01 10:21:17 帮助过:2人阅读

 

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”也要删除,否则会报错。

 

  关于如何调用存储过程可以参考如下文章:

http://blog.chinaunix.net/uid-13830775-id-216429.html

其中QSqlQuery::prepare()的参数怎么写,其实只要在SQL Server Management Studio中先调用下存储过程就会自动生成调用代码,EXEC那部分再精简下即可直接使用了。

推荐使用Oracle格式占位符即冒号方式如:argname,相比ODBC格式的?占位符方便很多。另外如果是多个output参数,可以用这样写:

 

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

 

 

最后还有一个需要注意的地方,sql server需要在所有结果集都被遍历后才能通过query.boundValue获取output参数的值,未遍历前获取的其实仍是query.bindValue时的默认值,具体可以参见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 once while 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.

 

qt调用sql server存储过程并获取output参数

标签:

人气教程排行