void ProgSettings::openSettingConnection() { DlgSettingConnect *dlg = new DlgSettingConnect(QApplication::activeWindow()); dlg->SetDBType(DBType()); dlg->SetDBName(DBName()); dlg->SetDBHostName(DBHostName()); dlg->SetDBUserName(DBUserName()); dlg->SetDBPassword(DBPassword()); dlg->SetDBPort(DBPort()); if ((dlg->exec() == QDialog::Accepted) && (dlg->IsModified())) { SetDBType(dlg->DBType()); SetDBName(dlg->DBName()); SetDBHostName(dlg->DBHostName()); SetDBUserName(dlg->DBUserName()); SetDBPassword(dlg->DBPassword()); SetDBPort(dlg->DBPort()); if (QSqlDatabase::database().isValid() & QSqlDatabase::database().isOpen()) { QSqlDatabase::database().close(); QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName()); } } }
DBSqlServer::DBSqlServer() { SetDBIP(""); SetDBUID(""); SetDBPwd(""); SetDBName(""); SetIsLinking(false); }
bool DBSqlServer::Connect(const char* ip, const char* uid, const char* pwd, const char* db) { if (GetIsLinking()) return false; SetDBIP(ip); SetDBUID(uid); SetDBPwd(pwd); SetDBName(db); std::string connString = GenerateLinkString(); //SQLCHAR ConnStrIn[255] = "DRIVER={SQL Server}; SERVER=127.0.0.1;UID=test; PWD=test123456;Trusted_Connection=yes;DATABASE=mytest; "; //FULOG(connString); RETCODE retcode; SQLHENV henv = SQL_NULL_HENV; SQLHDBC hdbc = SQL_NULL_HDBC; //1.Á¬½ÓÊý¾ÝÔ´ //1.»·¾³¾ä±ú retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); /*µÚ¶þ¸ö²ÎÊýÔNULL*/ retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER); //2.Á¬½Ó¾ä±ú retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); retcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)(connString.c_str()), SQL_NTS, NULL, NULL, NULL, SQL_DRIVER_NOPROMPT); //ÅжÏÁ¬½ÓÊÇ·ñ³É¹¦ if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO)) { FULOG("connect db failed [%s],[%d]!", connString.c_str(), retcode); Destroy(); return false; } m_hEnv = (char*)henv; m_hDbc = (char*)hdbc; SetIsLinking(true); return true; }