一:代码。
- /**连接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();
- }
- {
- QMessageBox::@R_980_4045@ion(this, tr("提示"), tr("sql Server数据库连接失败!"), tr("确定"));
- qDebug() <<"error_sqlServer:\n" << db.lastError().text();
- }
- db.close();
- }
二:安装。
参考http://www.voidcn.com/article/p-uqpsvgbl-eh.html(安装图解)
三:连接过程遇到的问题。
问题:"[Microsoft][ODBC sql Server Driver][dbnETLIB]sql Server 不存在或访问被拒绝 [Microsoft][ODBC sql Server Driver][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
- */
- ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; color:rgb(0, 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,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();
- }
- {
- QMessageBox::@R_980_4045@ion(ottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; color:rgb(0, tr("确定"));
- qDebug() <<"error_sqlServer:\n" << db.lastError().text();
- }
- db.close();
- }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。