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
- QSqlDatabase OpenDB ()
- {
- QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC ");
- // Note that the suffix \ sqlexpress must be added to the express version database.
- QString dsn = "DRIVER = {SQL SERVER}; SERVER = 192.168.10.22 \ sqlexpress; DATABASE = sqlscada ";
- Db. setDatabaseName (dsn );
- Db. setUserName ("sa ");
- Db. setPassword ("scada ");
-
- If (! Db. open ())
- {
- QDebug ("Error: % s", qPrintable (db. lastError (). text ()));
- Return db;
- }
- Return db;
- }
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
- QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC ");
- Db. setDatabaseName ("testdsn ");
- Db. setUserName ("sa ");
- Db. setPassword ("scada ");