Connect QT to SQLServer

Source: Internet
Author: User
Although QtSql provides common database access interfaces, some databases are not very common. using QT to connect to sqlserver in windows has been a long time. i. configuration 1. after TCPIP is enabled and sqlserver2005 is installed, find the configuration tool-SQLServer Configuration Manager: SQLEXPRESS protocol in the menu, enable TCPIP, and enable the corresponding IP address;

Although QtSql provides common database access interfaces, some databases are not very common. using QT to connect to sqlserver in windows has been a long time. i. configuration 1. enable TCP/IP after sqlserver2005 is installed, find the configuration tool-SQL Server Configuration Manager: SQLEXPRESS protocol from the menu, enable TCP/IP, and enable the corresponding IP address;

Although QtSql provides common database access interfaces, some databases are not very common. using QT to connect to sqlserver in windows has been a long time.

1. Configuration

1. Enable TCP/IP

After sqlserver2005 is installed, choose configuration tools> SQL Server Configuration Manager from the menu:

SQLEXPRESS protocol, enable TCP/IP, enable the corresponding IP address; SQL Native Client configuration, Client protocol, start TCP/IP, TCP/IP attribute settings listen to port 1433

2. enable remote connection

Configuration tool-> SQL Server peripheral application configurator-> Local Connection and remote connection, check to use both TCP/IP and named pipes.

Ii. Test

Method 1:

Cpp Code

  1. QSqlDatabase OpenDB ()
  2. {
  3. QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC ");
  4. // Note that the suffix \ sqlexpress must be added to the express version database.
  5. QString dsn = "DRIVER = {SQL SERVER}; SERVER = 192.168.10.22 \ sqlexpress; DATABASE = sqlscada ";
  6. Db. setDatabaseName (dsn );
  7. Db. setUserName ("sa ");
  8. Db. setPassword ("scada ");
  9. If (! Db. open ())
  10. {
  11. QDebug ("Error: % s", qPrintable (db. lastError (). text ()));
  12. Return db;
  13. }
  14. Return db;
  15. }

When you connect to the database, the following error occurs: ConnectionOpen (Invalid Instance (). The connection is Invalid. It is found that Microsoft has restrictions on the express version database, add a suffix like \ sqlexpress in the SERVER field to connect to OK

Method 2: Set the data source and specify the dsn.

Control Panel-> system and security-> management tools-> data source (ODBC)

Code:

Cpp Code

  1. QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC ");
  2. Db. setDatabaseName ("testdsn ");
  3. Db. setUserName ("sa ");
  4. Db. setPassword ("scada ");

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.