当前位置:网站首页>QT link MSSQL
QT link MSSQL
2022-07-27 21:04:00 【hitzsf】
Qt link MSSQL
List of articles
Reference article :
Microsoft ODBC The driver 17 for SQL Server Connection Strings - ConnectionStrings.com
The first 1 Ways of planting
Configure local ODBC Source
Reference article : Qt Connect SQL server database _Bruce-XIAO The blog of -CSDN Blog _qt Connect sqlserver
Matching code :
// The first method
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("mssql"); // Set data source name
db.setUserName("SYSTEM");
db.setPassword("SYSTEM123,.");
The first 2 Ways of planting
have access to SQL Server Link string to achieve , The way is as follows
#include <QtWidgets>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
bool OpenDatabase()
{
/* The first method QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setHostName("localhost"); db.setDatabaseName("mssql"); // Set data source name db.setUserName("sqluser"); db.setPassword("password"); */
/* The second method Link string */
QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(QString("DRIVER={SQL SERVER};"
"SERVER=%1;" // Server name
"DATABASE=%2;"// Database name
"UID=%3;" // Login name
"PWD=%4;" // password
)
.arg("192.168.1.207")
.arg("SCADA_AVIC_SERVER")
.arg("SYSTEM")
.arg("SYSTEM123,.")
);
bool ok=db.open();
if(ok)
{
qDebug()<<" Open database successfully ";
}
else
{
qDebug()<<" Failed to open database ";
}
return ok;
}
bool queryTable(QString tableName)
{
QString sql = "select * from %1";
sql = sql.arg (tableName);
QSqlQuery query;
query.exec (sql);
int i = 0;
qDebug() << query.size () << query.record ().count ();
auto record = query.record ();
QStringList colNames;
for (int i = 0; i < record.count (); ++i) {
colNames << QString("%1: ").arg (i,3,10,QChar('0')) + record.fieldName (i);
}
qDebug() << colNames.join (" , ");
while(query.next ()){
QStringList rowRecord;
rowRecord << (QString("%1").arg (++i,4,10,QChar('0')));
for (int j = 0; j < record.count (); ++j) {
rowRecord << query.value (j).toString ();
}
auto string = rowRecord.join (" ");
qDebug() << string;
}
qDebug() << query.size ();
return query.size ();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv); // This line can't be less
OpenDatabase();
queryTable("T_S_BASE_USER");
return 0;
}
summary
Obviously 2 This way will be better , There is no need to configure local ODBC Source .
边栏推荐
- Codeforces 1706E 并查集 + 启发式合并 + ST 表
- js闭包知识
- NPDP | what kind of product manager can be called excellent?
- NPDP|什么样的产品经理可以被称为优秀?
- Introduction to rk3399 platform introduction to proficient series (Introduction) 21 day learning challenge
- 自定义学习率
- [today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
- 认识传输介质通信方式
- 《SRE:Google运维解密》读后有感
- CPDA | how to have data analysis thinking?
猜你喜欢

Hcip day 5

Hexagon_V65_Programmers_Reference_Manual(7)

Hexagon_V65_Programmers_Reference_Manual(5)

How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help

自动化测试----selenium(二)

Hexagon_ V65_ Programmers_ Reference_ Manual(5)

【历史上的今天】7 月 27 日:模型检测先驱出生;微软收购 QDOS;第一张激光照排的中文报纸

82.(cesium篇)cesium点在3d模型上运动

hcip第五天

LeetCode-136-只出现一次的数字
随机推荐
Kingbasees heterogeneous database migration guide (4. Application migration process)
Slim: self supervised point cloud scene flow and motion estimation (iccv 2021)
Chapter 7 Intermediate Shell Tool I
力扣解法汇总592-分数加减运算
一种比读写锁更快的锁,还不赶紧认识一下
Source Insight 4.0使用介绍
Technology blog and tutorial
认识网络模型OSI模型
Typroa 拼写检查: 缺少对于 中文 的字典文件
Understand the encapsulation and de encapsulation of network model data
记一次restTemplate.getForEntity携带headers失败,restTemplate. exchange
[program life] "stage summary" - unwilling to be ordinary
基于ATX自动化测试解决方案
[Numpy] 广播机制(Broadcast)
Know the transmission medium, the medium of network communication
LeetCode-136-只出现一次的数字
LeetCode每日一练 —— 876. 链表的中间结点
DJI内推码(一码一用,2022年7月26日更新)
实名认证在文旅出行行业的应用场景有哪些?
认识网络模型数据的封装和解封装