当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Global styles and icons

Go --- automatic recompilation of air

CPDA | how to have data analysis thinking?

一种比读写锁更快的锁,还不赶紧认识一下

82. (cesium article) cesium points move on 3D models

One article to understand pychar shortcut key

LeetCode每日一练 —— 链表中倒数第 k 个结点

Recommend a powerful search tool listary

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

How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help
随机推荐
[Numpy] 广播机制(Broadcast)
自定义学习率
记一次restTemplate.getForEntity携带headers失败,restTemplate. exchange
原生对象、内置对象、宿主对象的区别
认识传输介质通信方式
Hexagon_V65_Programmers_Reference_Manual(6)
MySQL design optimization generates columns
go --- air自动重新编译
Do you know about data synchronization?
Download of MySQL driver jar package -- nanny tutorial
如何让个性化推荐即刻触达?云原生数据库GaussDB(for Redis)来助力
坚持做一件事情
Technology blog and tutorial
如何对话CIO/CTO
文件上传绕过WAF的技巧大全
Hcip day 5
[today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
[Numpy] 数组索引和切片
Riding lantern case
认识网络模型OSI模型