A summary of the practice of QT-connected SQL Server2008 via ODBC

Source: Internet
Author: User
Tags dsn odbc

There are many ways to connect to a database, and here is one of the most common and useful ways to develop programs on windows that are easy to use and the way to connect to a database remotely .

Prerequisite work:

SQL Server 2008 is installed under Win7 and database mydb is created in SQL Server.

QT connects to SQL Server 2008 via ODBC Overview:

When QT connects to a database through ODBC, the database name used is not written directly to the database name, but the DSN name.

A brief introduction to DSN:

Formerly known as: Data Source Name
Chinese name: Data Source Name
DSN defines an established database for ODBC and the ODBC driver that must be used. Each ODBC driver defines the information required to create a DSN for a database that is supported by the driver. This means that after installing the ODBC driver and creating a database, you must create a DSN.

There are two ways to use DSN names:

1, configure the DSN in the operating system;

2. Use the DSN connection string in the QT program code to connect directly to the ODBC database.

The following details the procedures for connecting to SQL Server 2008 under Configure DSN mode in the operating system

Configuring DSN in the operating system

1. Configure the DSN detail plot in the Win7 system

2. After the DSN is configured, you can use this DSN to connect to the database in the QT program:

Where the main.cpp code is as follows:

#include <QtGui/QApplication>

#include "Dialog.h"
#include <QSqlDatabase>
#include <QStringList>
#include <QDebug>
#include <QMessageBox>
#include <QtSql>
#include <QSqlQuery>
int main (int argc, char *argv[])
{
    Qapplication A (argc, argv);
    Dialog W;
    W.show ();
    Qsqldatabase db = Qsqldatabase::adddatabase ("qodbc");
    Qdebug () << "ODBC driver?" <<db.isvalid ();
    QString DSN = Qstring::fromlocal8bit ("Qtdsn");
    Db.sethostname ("127.0.0.1");
    Db.setdatabasename (DSN);
    Db.setusername ("sa");
    Db.setpassword ("198811200");
    if (!db.open ())
    {
        Qdebug () <<db.lasterror (). text ();
        Qmessagebox::critical (0, Qobject::tr ("Database error"), Db.lasterror (). text ());
        return false;
    }
    Else
        Qdebug () << "Database open success!";
    Qsqlquery query (DB);
    Query.exec ("select * from student");
    while (Query.next ())
    {
        Qdebug () <<query.value (0). toString ();
        Qdebug () <<query.value (1). ToString ();
        Qdebug () <<query.value (2). toString ();
        Qdebug () <<query.value (3). ToString ();
        Qdebug () << "";
    }
    return A.exec ();
}

Test output results:

ODBC driver? True

Database Open success!

"John Doe"

"Harry"

"Zhao Liu"

"Sabahan"

"Zhengguo"

"Fen"

"As a result"

"Zhu"

Reference:

Http://www.xuebuyuan.com/619048.html

Http://hi.baidu.com/hzau_edu/item/091aa61a12b4e98488a956a5

http://blog.163.com/luminary_fan/blog/static/1941080892013069100349/

Http://wenku.baidu.com/link?url=8752chxOKRLOJ4yXwZGlwRUbWhvkNDwVioZd0ZwGMGizQDclNsvtnb8137LGXtce_ Vqnpy1mijak9h7zcyoevyl1ldfocy8ypmxx83vskek

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.