Qt远程连接SQlServer数据库
来源:互联网 发布:徕卡全站仪传输软件 编辑:程序博客网 时间:2024/06/05 10:47
一:代码。
- /**连接sql server数据库
- *数据库名:abc
- *表名:SQL_2000
- *用户名:sa
- *密码:123
- *端口号:(默认)1433
- */
- void MainDialog::connectSql(QString sIp, int iPort, QString sDbNm, QString sUserNm, QString sPwd)
- {
- db = QSqlDatabase::addDatabase("QODBC");
- QString dsn = QString("Driver={sql server};SERVER=%1;PORT=%2;DATABASE=%3;UID=%4;PWD=%5;")
- .arg(sIp)
- .arg(iPort)
- .arg(sDbNm)
- .arg(sUserNm)
- .arg(sPwd);
- db.setDatabaseName(dsn);
- /*连接sql 2000*/
- bool r = db.open();
- if (r)
- {
- qDebug() << "SQL Server 2000 Connect OK!";
- /* 计算当前表中id*/
- QSqlQuery query1 = QSqlQuery(db);
- query1.clear();
- query1.prepare("select top 1 ID from SQL_2000 order by ID desc");
- bool a = query1.exec();
- int id;
- if (a)
- {
- while(query1.next())
- {
- id = query1.value(0).toInt();
- }
- }
- /*插入数据*/
- QSqlQuery query2 = QSqlQuery(db);
- QString sq1 = QObject::tr("insert into SQL_2000(Id,Ip,Port,UserName,Password,DbType,DbName)"
- "values (?, ?, ?, ?, ?, ?, ?)");
- bool b = query2.prepare(sq1);
- if(b)
- {
- qDebug() << "insert data success!";
- }
- query2.bindValue(0, id+1);
- query2.bindValue(1, sIp);
- query2.bindValue(2, iPort);
- query2.bindValue(3, sUserNm);
- query2.bindValue(4, sPwd);
- query2.bindValue(5, sDbType);
- query2.bindValue(6, sDbNm);
- /*查询数据*/
- QSqlQuery query3 = QSqlQuery(db);
- query3.prepare("select * from SQL_2000 where Id=1");
- bool c = query3.exec();
- if (c)
- {
- qDebug() << "select data success!";
- while(query3.next())
- {
- qDebug() << query3.value(0);
- qDebug() << query3.value(1);
- qDebug() << query3.value(2).toInt();
- }
- }
- else
- {
- qDebug() << query3.lastError().text().data();
- }
- /*删除数据*/
- QSqlQuery query4 = QSqlQuery(db);
- query4.prepare("delete from SQL_2000 where Id=1");
- bool d = query4.exec();
- if (d)
- {
- qDebug() << "delete data success!";
- }
- else
- {
- qDebug() << query3.lastError().text().data();
- }
- }
- else
- {
- QMessageBox::information(this, tr("提示"), tr("Sql Server数据库连接失败!"), tr("确定"));
- qDebug() <<"error_SqlServer:\n" << db.lastError().text();
- }
- db.close();
- }
二:安装。
参考http://blog.csdn.net/mingxia_sui/article/details/7723978(安装图解)
三:连接过程遇到的问题。
问题:"[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝 [Microsoft][ODBC SQL ServerDriver][DBNETLIB]ConnectionOpen (Connect()). [Microsoft][ODBC SQL Server Driver]无效的连接字符串属性QODBC3: Unable to connect"
通过http://topic.csdn.net/u/20100429/10/586ed537-0a66-48ac-97d6-e662e5199339.html对比,我发现问题:
服务器没有在1433端口侦听。
(测试方法:在dos下输入netstat -a -n或者netstat -an;结果:找不到tcp 127.0.0.1 1433 listening的项)
解决方法:
安装补丁(我的版本对应sp4)
打补丁的过程中出现问题:不能打开要写入的文件C:\WINDOWS\system32\ntwdblib.dll。
重启还是不行,我就把sp4安装包中的ntwdblib.dll直接拷贝到C:\WINDOWS\system32中。
再试telnet 127.0.0.1 1433,居然连接上了。
网卡设置:端口没被侦听,也可能是网卡的问题。
本地连接--->属性--->Internet协议(TCP/IP)--->属性--->高级--->选项--->属性--->全部允许TCP端口。
- /**连接sql server数据库
- *数据库名:abc
- *表名:SQL_2000
- *用户名:sa
- *密码:123
- *端口号:(默认)1433
- */
- void MainDialog::connectSql(QString sIp, int iPort, QString sDbNm, QString sUserNm, QString sPwd)
- {
- db = QSqlDatabase::addDatabase("QODBC");
- QString dsn = QString("Driver={sql server};SERVER=%1;PORT=%2;DATABASE=%3;UID=%4;PWD=%5;")
- .arg(sIp)
- .arg(iPort)
- .arg(sDbNm)
- .arg(sUserNm)
- .arg(sPwd);
- db.setDatabaseName(dsn);
- /*连接sql 2000*/
- bool r = db.open();
- if (r)
- {
- qDebug() << "SQL Server 2000 Connect OK!";
- /* 计算当前表中id*/
- QSqlQuery query1 = QSqlQuery(db);
- query1.clear();
- query1.prepare("select top 1 ID from SQL_2000 order by ID desc");
- bool a = query1.exec();
- int id;
- if (a)
- {
- while(query1.next())
- {
- id = query1.value(0).toInt();
- }
- }
- /*插入数据*/
- QSqlQuery query2 = QSqlQuery(db);
- QString sq1 = QObject::tr("insert into SQL_2000(Id,Ip,Port,UserName,Password,DbType,DbName)"
- "values (?, ?, ?, ?, ?, ?, ?)");
- bool b = query2.prepare(sq1);
- if(b)
- {
- qDebug() << "insert data success!";
- }
- query2.bindValue(0, id+1);
- query2.bindValue(1, sIp);
- query2.bindValue(2, iPort);
- query2.bindValue(3, sUserNm);
- query2.bindValue(4, sPwd);
- query2.bindValue(5, sDbType);
- query2.bindValue(6, sDbNm);
- /*查询数据*/
- QSqlQuery query3 = QSqlQuery(db);
- query3.prepare("select * from SQL_2000 where Id=1");
- bool c = query3.exec();
- if (c)
- {
- qDebug() << "select data success!";
- while(query3.next())
- {
- qDebug() << query3.value(0);
- qDebug() << query3.value(1);
- qDebug() << query3.value(2).toInt();
- }
- }
- else
- {
- qDebug() << query3.lastError().text().data();
- }
- /*删除数据*/
- QSqlQuery query4 = QSqlQuery(db);
- query4.prepare("delete from SQL_2000 where Id=1");
- bool d = query4.exec();
- if (d)
- {
- qDebug() << "delete data success!";
- }
- else
- {
- qDebug() << query3.lastError().text().data();
- }
- }
- else
- {
- QMessageBox::information(this, tr("提示"), tr("Sql Server数据库连接失败!"), tr("确定"));
- qDebug() <<"error_SqlServer:\n" << db.lastError().text();
- }
- db.close();
- }
- Qt远程连接SQlServer数据库
- Qt远程连接SQlServer数据库
- 远程连接SqlServer数据库
- sqlserver连接远程数据库
- sqlServer 2008远程连接数据库
- 使用QT连接SqlServer数据库
- Qt远程连接access数据库
- Qt远程连接MySql数据库
- Qt远程连接Oracle数据库
- Qt远程连接Oracle数据库
- Qt远程连接Oracle数据库
- Qt远程连接MySql数据库
- 数据库扫描器远程连接Oracle、SqlServer、Mysql
- 服务器sqlserver数据库远程连接的设置
- 远程连接sqlserver数据库报错53
- Qt连接access、Oracle、MySql、SqlServer数据库
- QT与数据库的远程连接
- sqlserver 远程连接数据源
- MSSQL数据库孤立用户解决过程一则
- HDU GCC(HDU 3123)解题报告
- 今天我进来了CSDN论坛了,开始了我的IT生活
- Asterisk behind NAT
- linux shell中kill 和kill all 分析
- Qt远程连接SQlServer数据库
- javascript学习-3
- 了解多态
- ARM处理器的指令系统一 ARM处理器寻址方式
- 存储过程样例--获取数据
- alsa驱动学习
- Keeping Eclipse running clean (解决eclipse build慢的一篇文章)
- taglist安装与应用
- Android 中的拿来主义(编译,反编译,AXMLPrinter2,smali,baksmali)!