微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Qt远程连接SQlServer数据库

一:代码

  1. /**连接sql server数据库 
  2.   *数据库名:abc 
  3.   *表名:sql_2000 
  4.   *用户名:sa 
  5.   *密码:123 
  6.   *端口号:(认)1433 
  7. */  
  8. void MainDialog::connectsql(QString sIp, int iPort,  QString sdbnm, QString sUserNm, QString sPwd)  
  9. {  
  10.     db = QsqlDatabase::addDatabase("QODBC");  
  11.     QString dsn = QString("Driver={sql server};SERVER=%1;PORT=%2;DATABASE=%3;UID=%4;PWD=%5;")  
  12.                   .arg(sIp)  
  13.                   .arg(iPort)  
  14.                   .arg(sdbnm)  
  15.                   .arg(sUserNm)  
  16.                   .arg(sPwd);  
  17.     db.setDatabaseName(dsn);  
  18.   
  19.   
  20.     /*连接sql 2000*/  
  21.     bool r = db.open();  
  22.     if (r)  
  23.     {  
  24.         qDebug() << "sql Server 2000 Connect OK!";  
  25.         /* 计算当前表中id*/  
  26.         QsqlQuery query1 = QsqlQuery(db);  
  27.         query1.clear();  
  28.         query1.prepare("select top 1 ID from sql_2000 order by ID desc");  
  29.         bool a = query1.exec();  
  30.         int id;  
  31.         if (a)  
  32.         {  
  33.             while(query1.next())  
  34.             {  
  35.                 id = query1.value(0).toInt();  
  36.             }  
  37.         }  
  38. /*插入数据*/  
  39.         QsqlQuery query2 = QsqlQuery(db);  
  40.         QString sq1 = QObject::tr("insert into sql_2000(Id,Ip,Port,UserName,Password,DbType,dbname)"  
  41.                                   "values (?, ?, ?)");  
  42. bool b = query2.prepare(sq1);  
  43.         if(b)  
  44.         {  
  45.             qDebug() << "insert data success!";  
  46.         query2.bindValue(0, id+1);  
  47.         query2.bindValue(1, sIp);  
  48.         query2.bindValue(2, iPort);  
  49.         query2.bindValue(3, sUserNm);  
  50.         query2.bindValue(4, sPwd);  
  51.         query2.bindValue(5, sDbType);  
  52.         query2.bindValue(6, sdbnm);  
  53.         /*查询数据*/  
  54.         QsqlQuery query3 = QsqlQuery(db);  
  55.         query3.prepare("select * from sql_2000 where Id=1");  
  56. bool c = query3.exec();  
  57. if (c)  
  58.             qDebug() << "select data success!";  
  59. while(query3.next())  
  60.                 qDebug() << query3.value(0);  
  61.                 qDebug() << query3.value(1);  
  62.                 qDebug() << query3.value(2).toInt();  
  63. else  
  64.             qDebug() << query3.lastError().text().data();  
  65. /*删除数据*/  
  66.         QsqlQuery query4 = QsqlQuery(db);  
  67.         query4.prepare("delete from sql_2000 where Id=1");  
  68. bool d = query4.exec();  
  69. if (d)  
  70.             qDebug() << "delete data success!";  
  71.         }  
  72. else  
  73.             qDebug() << query3.lastError().text().data();  
  74.     }  
  75.          {  
  76.         QMessageBox::@R_980_4045@ion(this, tr("提示"), tr("sql Server数据库连接失败!"), tr("确定"));  
  77.         qDebug() <<"error_sqlServer:\n" << db.lastError().text();  
  78.     }  
  79.     db.close();  
  80. }  

二:安装。

    参考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数据库 
  1.   *数据库名:abc 
  2.   *表名:sql_2000 
  3.   *用户名:sa 
  4.   *密码:123 
  5.   *端口号:(认)1433 
  6. */  
  7. 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)  
  8. {  
  9.     db = QsqlDatabase::addDatabase("QODBC");  
  10.     QString dsn = QString("Driver={sql server};SERVER=%1;PORT=%2;DATABASE=%3;UID=%4;PWD=%5;")  
  11.                   .arg(sIp)  
  12.                   .arg(iPort)  
  13.                   .arg(sdbnm)  
  14.                   .arg(sUserNm)  
  15.                   .arg(sPwd);  
  16.     db.setDatabaseName(dsn);  
  17.   
  18.   
  19.     /*连接sql 2000*/  
  20.     bool r = db.open();  
  21.     if (r)  
  22.     {  
  23.         qDebug() << "sql Server 2000 Connect OK!";  
  24.         /* 计算当前表中id*/  
  25.         QsqlQuery query1 = QsqlQuery(db);  
  26.         query1.clear();  
  27.         query1.prepare("select top 1 ID from sql_2000 order by ID desc");  
  28.         bool a = query1.exec();  
  29.         int id;  
  30.         if (a)  
  31.         {  
  32.             while(query1.next())  
  33.             {  
  34.                 id = query1.value(0).toInt();  
  35.             }  
  36.         }  
  37. /*插入数据*/  
  38.         QsqlQuery query2 = QsqlQuery(db);  
  39.         QString sq1 = QObject::tr("insert into sql_2000(Id,dbname)"  
  40.                                   "values (?, ?)");  
  41. bool b = query2.prepare(sq1);  
  42.         if(b)  
  43.         {  
  44.             qDebug() << "insert data success!";  
  45.         query2.bindValue(0, id+1);  
  46.         query2.bindValue(1, sIp);  
  47.         query2.bindValue(2, iPort);  
  48.         query2.bindValue(3, sUserNm);  
  49.         query2.bindValue(4, sPwd);  
  50.         query2.bindValue(5, sDbType);  
  51.         query2.bindValue(6, sdbnm);  
  52.         /*查询数据*/  
  53.         QsqlQuery query3 = QsqlQuery(db);  
  54.         query3.prepare("select * from sql_2000 where Id=1");  
  55. bool c = query3.exec();  
  56. if (c)  
  57.             qDebug() << "select data success!";  
  58. while(query3.next())  
  59.                 qDebug() << query3.value(0);  
  60.                 qDebug() << query3.value(1);  
  61.                 qDebug() << query3.value(2).toInt();  
  62. else  
  63.             qDebug() << query3.lastError().text().data();  
  64. /*删除数据*/  
  65.         QsqlQuery query4 = QsqlQuery(db);  
  66.         query4.prepare("delete from sql_2000 where Id=1");  
  67. bool d = query4.exec();  
  68. if (d)  
  69.             qDebug() << "delete data success!";  
  70.         }  
  71. else  
  72.             qDebug() << query3.lastError().text().data();  
  73.     }  
  74.          {  
  75.         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("确定"));  
  76.         qDebug() <<"error_sqlServer:\n" << db.lastError().text();  
  77.     }  
  78.     db.close();  
  79. }  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐