• 108阅读
  • 1回复

[提问]数据库断网重连问题,如何重新定义netquery啊 [复制链接]

上一主题 下一主题
离线wfwjp
 

只看楼主 倒序阅读 楼主  发表于: 04-19
程序连接两个数据,定义了两个Qsqldatabase对象,netdb和localdb,两个Qsqlquery,netquery和localquery。
如果网络有很多断点,在netquery.exec()时报错,我想再重新连接一下netdb,见下面例子。在netdb重连后,如果使用QSqlQuery netquery1(netdb);是可以的,但是直接使用netquery就会报错,我需要仍然使用netquery,如果Qsqlquery netquery(netdb)重新定义就会报错,netquery如何重新定义呢?
  1. QSqlDatabase netdb=QSqlDatabase::addDatabase("QODBC","netdb");
  2.     netdb.setDatabaseName(QString("DRIVER={SQL SERVER};"
  3.                                "SERVER=%1;" //服务器名称
  4.                                "DATABASE=%2;"//数据库名
  5.                                "UID=%3;"     //登录名
  6.                                "PWD=%4;"      //密码
  7.                                ).arg(netservername)
  8.                                 .arg(netdatabase)
  9.                                 .arg(netlogid)
  10.                                 .arg(netlogpassword)
  11.                        );
  12.     if ( !netdb.open())
  13.     {
  14.         QMessageBox::critical(0, "无法连接网络数据库!",
  15.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  16.         return;
  17.     }
  18.     QSqlQuery netquery(netdb);
  19.     bool rtn=netquery.exec("select ttmc from tttzd");
  20.     if(!rtn)
  21.     {
  22.         QMessageBox::critical(0, "检索网络库错误!",
  23.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  24.         return;
  25.     }
  26.     QMessageBox::critical(0, "wait wl!",
  27.         netdb.lastError().databaseText(), QMessageBox::Cancel);
  28.     netdb.removeDatabase("netdb");
  29.     netdb=QSqlDatabase::addDatabase("QODBC","netdb");
  30.     netdb.setDatabaseName(QString("DRIVER={SQL SERVER};"
  31.                                "SERVER=%1;" //服务器名称
  32.                                "DATABASE=%2;"//数据库名
  33.                                "UID=%3;"     //登录名
  34.                                "PWD=%4;"      //密码
  35.                                ).arg(netservername)
  36.                                 .arg(netdatabase)
  37.                                 .arg(netlogid)
  38.                                 .arg(netlogpassword)
  39.                        );
  40.     if ( !netdb.open())
  41.     {
  42.         QMessageBox::critical(0, "无法连接网络库!",
  43.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  44.         return;
  45.     }
  46.     QSqlQuery netquery1(netdb);
  47.     rtn=netquery1.exec("select ttbh from tttzd");
  48.     if(!rtn)
  49.     {
  50.         QMessageBox::critical(0, "检索网络库错误!",
  51.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  52.         return;
  53.     }
  54.     netquery1.next();
  55.     QString ls_bh=netquery1.value(0).toString();
  56.     QString ls_mc=ls_bh;




离线wfwjp

只看该作者 1楼 发表于: 04-19
测试了下  

netdb.removeDatabase("netdb");
netdb=QSqlDatabase::addDatabase("QODBC","netdb");
改为
netdb=QSqlDatabase::addDatabase("QODBC","netdbtest");
就可以直接使用netquery了。

  1. QSqlDatabase netdb=QSqlDatabase::addDatabase("QODBC","netdb");
  2.     netdb.setDatabaseName(QString("DRIVER={SQL SERVER};"
  3.                                "SERVER=%1;" //服务器名称
  4.                                "DATABASE=%2;"//数据库名
  5.                                "UID=%3;"     //登录名
  6.                                "PWD=%4;"      //密码
  7.                                ).arg(netservername)
  8.                                 .arg(netdatabase)
  9.                                 .arg(netlogid)
  10.                                 .arg(netlogpassword)
  11.                        );
  12.     if ( !netdb.open())
  13.     {
  14.         QMessageBox::critical(0, "无法连接网络数据库!",
  15.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  16.         return;
  17.     }
  18.     QSqlQuery netquery(netdb);
  19.     bool rtn=netquery.exec("select ttmc from tttzd");
  20.     if(!rtn)
  21.     {
  22.         QMessageBox::critical(0, "检索网络库错误!",
  23.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  24.         return;
  25.     }
  26.     QMessageBox::critical(0, "wait wl!",
  27.         netdb.lastError().databaseText(), QMessageBox::Cancel);
  28.     //netdb.removeDatabase("netdb");
  29.     netdb=QSqlDatabase::addDatabase("QODBC","netdb1");
  30.     netdb.setDatabaseName(QString("DRIVER={SQL SERVER};"
  31.                                "SERVER=%1;" //服务器名称
  32.                                "DATABASE=%2;"//数据库名
  33.                                "UID=%3;"     //登录名
  34.                                "PWD=%4;"      //密码
  35.                                ).arg(netservername)
  36.                                 .arg(netdatabase)
  37.                                 .arg(netlogid)
  38.                                 .arg(netlogpassword)
  39.                        );
  40.     if ( !netdb.open())
  41.     {
  42.         QMessageBox::critical(0, "无法连接网络库!",
  43.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  44.         return;
  45.     }
  46.     //QSqlQuery netquery1(netdb);
  47.     rtn=netquery.exec("select ttbh from tttzd");
  48.     if(!rtn)
  49.     {
  50.         QMessageBox::critical(0, "检索网络库错误!",
  51.             netdb.lastError().databaseText(), QMessageBox::Cancel);
  52.         return;
  53.     }
  54.     netquery.next();
  55.     QString ls_bh=netquery.value(0).toString();
  56.     QString ls_mc=ls_bh;


快速回复
限100 字节
 
上一个 下一个