1. Pro Add
QT +=sql
2.
#include<QSettings>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlError>
#include<QtSql/QSqlQuery>
#include<QDebug>
voidMainwindow::connectsqlserver () {qsettings*setini =NewQsettings ("Config.ini", Qsettings::iniformat); Setini->begingroup ("Databaseserver"); QString ServerIP=setini->value ("ServerIP"). toString (); QString DatabaseName=setini->value ("DatabaseName"). toString (); QString User=setini->value ("User"). toString (); QString Password=setini->value ("Password"). toString (); Setini-endgroup (); Delete Setini;//Qsqldatabase db = Qsqldatabase::adddatabase ("Qodbc");db = Qsqldatabase::adddatabase ("Qodbc"); Db.setdatabasename (QString ("Driver={sql SERVER};" "server=%1;" "database=%2;" "uid=%3;" "pwd=%4;"). Arg (ServerIP). Arg (databaseName). Arg (user). Arg (password)); if(!Db.open ()) {Qdebug ()<<"++++++++++++db error+++++++++++++"<<db.lasterror (). Databasetext (); Statuslabel->settext ("Server Connection Bug"+db.lasterror (). Databasetext ()); } Else { //qdebug () << "Connect to Database success!";Statuslabel->settext ("The service is connected properly"); } } voidmainwindow::insertdb (qstringlist lst) {qsqlquery query=qsqlquery (db); Query.prepare ("INSERT into Sny_file (SNY01,SNY02,SNY03,SNY04,SNY05,SNY06,SNY07,SNY08,SNY09,SNY10)" "VALUES (: S01,:S02,:S03,:S04,:S05,:S06,:S07,:S08,:S09,:S10)"); Query.bindvalue (": S01", lst.at (0));//work ListQuery.bindvalue (": S02", lst.at (2));//Manufacturing ProcessQuery.bindvalue (": S03", lst.at (3));//OnlineQuery.bindvalue (": S04", lst.at (4));//Import (I/O)Query.bindvalue (": S05", lst.at (5));//numberQuery.bindvalue (": S06", lst.at (6));//DateQuery.bindvalue (": S07", lst.at (7));//hoursQuery.bindvalue (": S08", lst.at (8));//Scale CodesQuery.bindvalue (": S09", lst.at (1));//Employee's passwordQuery.bindvalue (": S10","N");//this linked fields writes into N query.exec ();}
QT uses SQL Server