Qt远程连接SQlServer数据库

来源:互联网 发布:徕卡全站仪传输软件 编辑:程序博客网 时间:2024/06/05 10:47

一:代码。

  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.   
  26.         /* 计算当前表中id*/  
  27.         QSqlQuery query1 = QSqlQuery(db);  
  28.         query1.clear();  
  29.         query1.prepare("select top 1 ID from SQL_2000 order by ID desc");  
  30.         bool a = query1.exec();  
  31.         int id;  
  32.         if (a)  
  33.         {  
  34.             while(query1.next())  
  35.             {  
  36.                 id = query1.value(0).toInt();  
  37.             }  
  38.         }  
  39.   
  40.         /*插入数据*/  
  41.         QSqlQuery query2 = QSqlQuery(db);  
  42.         QString sq1 = QObject::tr("insert into SQL_2000(Id,Ip,Port,UserName,Password,DbType,DbName)"  
  43.                                   "values (?, ?, ?, ?, ?, ?, ?)");  
  44.         bool b = query2.prepare(sq1);  
  45.         if(b)  
  46.         {  
  47.             qDebug() << "insert data success!";  
  48.         }  
  49.         query2.bindValue(0, id+1);  
  50.         query2.bindValue(1, sIp);  
  51.         query2.bindValue(2, iPort);  
  52.         query2.bindValue(3, sUserNm);  
  53.         query2.bindValue(4, sPwd);  
  54.         query2.bindValue(5, sDbType);  
  55.         query2.bindValue(6, sDbNm);  
  56.   
  57.         /*查询数据*/  
  58.         QSqlQuery query3 = QSqlQuery(db);  
  59.         query3.prepare("select * from SQL_2000 where Id=1");  
  60.         bool c = query3.exec();  
  61.         if (c)  
  62.         {  
  63.             qDebug() << "select data success!";  
  64.             while(query3.next())  
  65.             {  
  66.                 qDebug() << query3.value(0);  
  67.                 qDebug() << query3.value(1);  
  68.                 qDebug() << query3.value(2).toInt();  
  69.             }  
  70.         }  
  71.         else  
  72.         {  
  73.             qDebug() << query3.lastError().text().data();  
  74.         }  
  75.   
  76.         /*删除数据*/  
  77.         QSqlQuery query4 = QSqlQuery(db);  
  78.         query4.prepare("delete from SQL_2000 where Id=1");  
  79.         bool d = query4.exec();  
  80.         if (d)  
  81.         {  
  82.             qDebug() << "delete data success!";  
  83.         }  
  84.         else  
  85.         {  
  86.             qDebug() << query3.lastError().text().data();  
  87.         }  
  88.     }  
  89.     else  
  90.     {  
  91.         QMessageBox::information(this, tr("提示"), tr("Sql Server数据库连接失败!"), tr("确定"));  
  92.         qDebug() <<"error_SqlServer:\n" << db.lastError().text();  
  93.     }  
  94.   
  95.     db.close();  
  96. }  

二:安装。

    参考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端口。

       

[cpp] view plaincopyprint?
  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.   
  26.         /* 计算当前表中id*/  
  27.         QSqlQuery query1 = QSqlQuery(db);  
  28.         query1.clear();  
  29.         query1.prepare("select top 1 ID from SQL_2000 order by ID desc");  
  30.         bool a = query1.exec();  
  31.         int id;  
  32.         if (a)  
  33.         {  
  34.             while(query1.next())  
  35.             {  
  36.                 id = query1.value(0).toInt();  
  37.             }  
  38.         }  
  39.   
  40.         /*插入数据*/  
  41.         QSqlQuery query2 = QSqlQuery(db);  
  42.         QString sq1 = QObject::tr("insert into SQL_2000(Id,Ip,Port,UserName,Password,DbType,DbName)"  
  43.                                   "values (?, ?, ?, ?, ?, ?, ?)");  
  44.         bool b = query2.prepare(sq1);  
  45.         if(b)  
  46.         {  
  47.             qDebug() << "insert data success!";  
  48.         }  
  49.         query2.bindValue(0, id+1);  
  50.         query2.bindValue(1, sIp);  
  51.         query2.bindValue(2, iPort);  
  52.         query2.bindValue(3, sUserNm);  
  53.         query2.bindValue(4, sPwd);  
  54.         query2.bindValue(5, sDbType);  
  55.         query2.bindValue(6, sDbNm);  
  56.   
  57.         /*查询数据*/  
  58.         QSqlQuery query3 = QSqlQuery(db);  
  59.         query3.prepare("select * from SQL_2000 where Id=1");  
  60.         bool c = query3.exec();  
  61.         if (c)  
  62.         {  
  63.             qDebug() << "select data success!";  
  64.             while(query3.next())  
  65.             {  
  66.                 qDebug() << query3.value(0);  
  67.                 qDebug() << query3.value(1);  
  68.                 qDebug() << query3.value(2).toInt();  
  69.             }  
  70.         }  
  71.         else  
  72.         {  
  73.             qDebug() << query3.lastError().text().data();  
  74.         }  
  75.   
  76.         /*删除数据*/  
  77.         QSqlQuery query4 = QSqlQuery(db);  
  78.         query4.prepare("delete from SQL_2000 where Id=1");  
  79.         bool d = query4.exec();  
  80.         if (d)  
  81.         {  
  82.             qDebug() << "delete data success!";  
  83.         }  
  84.         else  
  85.         {  
  86.             qDebug() << query3.lastError().text().data();  
  87.         }  
  88.     }  
  89.     else  
  90.     {  
  91.         QMessageBox::information(this, tr("提示"), tr("Sql Server数据库连接失败!"), tr("确定"));  
  92.         qDebug() <<"error_SqlServer:\n" << db.lastError().text();  
  93.     }  
  94.   
  95.     db.close();  
  96. }