1#include <QtSql/QSqlDatabase>2#include <QMessageBox>3#include <QSqlError>4#include <QSqlQuery>5#include <QDebug>6 7 8 intMainintargcChar*argv[])9 {Ten One //method One: Establish a connection through DSN. The DSN is the data source name. A //The following example is on the Dbhomework database on SQL Server. The name of the connection specified in the program is Dbtemp and can be written casually. -Qsqldatabase db = Qsqldatabase::adddatabase ("Qodbc","dbhomework"); - //specifies that the data source name is: MSSQLServer theQString DSN = Qstring::fromlocal8bit ("Driver={sql SERVER}; Server=sanlary;database=homework"); - db.setdatabasename (DSN); - if(!Db.open ()) { -Qmessagebox::about (0, Qobject::tr ("Database Error"), Db.lasterror (). text ()); + //if the DSN name is incorrectly set, the "[Microsoft][odbc Driver Manager] Data source name not found and no default Driver specified Q will appear at Db.open () Odbc3:unable to connect " - //"[MICROSOFT][ODBC Driver Manager] did not discover the data source name and did not specify a default driver qodbc3:unable to connect" error. + } A Else at { -Qsqlquery query (DB);//The following executes the related SQL statement -Query.exec ("if exists fuck drop fuck"); -Query.exec ("CREATE TABLE fuck (id int primary key,name varchar )"); -Query.exec ("insert INTO fuck values (2, ' Xiafang ')"); -Query.exec ("insert INTO fuck values (7, ' Wanghua ')"); inQuery.exec ("insert INTO fuck values (3, ' aisq ')"); -Query.exec ("insert INTO fuck values (4, ' Fucan ')"); toQuery.exec ("Select Id,name from fuck where id>=2"); + while(Query.next ()) { - intId=query.value (0). ToInt ();//query.value (0) in 0 represents the value of the first field, which is the value of the ID theQString Name=query.value (1). ToString (). trimmed (); *Qdebug () <<"id="<<id<<" "<<"name="<<name<<Endl; $ }Panax Notoginseng } - the /* + //* Method Two: The host name and user name password to establish a connection. A //The following example connects to a database on SQL Server named homework on 192.168.21.106. The name of the connection specified in the program is Dbtemp and can be written casually. the Qsqldatabase db = Qsqldatabase::adddatabase ("Qodbc", "homework"); + //Specify the IP address of the server host, which computer the database is on. Can be the IP address of your own computer. - db.sethostname ("192.168.21.106"); $ //ODBC connect the SQL Server-driven format. "Driver={sql server};server=servername;database=dbname;uid=sa;pwd=pass;" $ QString DSN = Qstring::fromlocal8bit ("Driver={sql SERVER}; Server=192.168.21.106;database=homework "); - //Can be self-Baidu if you start ODBC data in Control panel far to set the DSN data source name - //If DSN is already set, it can be written as Qstring::fromlocal8bit ("DSN data source name") the db.setdatabasename (DSN); - //qstring::fromlocal8bit ("MSSQLServer");Wuyi //Even if UID and passwd are already set in DSN, steps for Setusername and SetPassword still need to be performed the //Also note that the user name here has permission to create the table, otherwise the following table student will not succeed. - db.setusername ("Yanxueke"); Wu Db.setpassword ("zhanghan223"); - if (!db.open ()) { About qmessagebox::about (0, Qobject::tr ("Database Error"), Db.lasterror (). text ()); $ } - //*//* - Else - { A qsqlquery query (db); The following executes the related SQL statement + query.exec ("If exists fuck drop fuck"); the query.exec ("CREATE TABLE student (ID int primary key,name varchar)"); - query.exec ("INSERT into student values (2, ' Xiafang ')"); $ query.exec ("INSERT into student values (7, ' Wanghua ')"); the query.exec ("INSERT into student values (3, ' aisq ')"); the query.exec ("INSERT into student values (4, ' Fucan ')"); the query.exec ("Select Id,name from student where id>=2"); the While (Query.next ()) { - int id=query.value (0). ToInt (); Query.value (0) in 0 represents the value of the first field, which is the value of the ID in QString name=query.value (1). ToString (). trimmed (); the qdebug () << "id=" <<id<< "<<" name= "<<name<<endl; the } About } the db.close ();*/ the}