bool CMssqlConnection::Open() { CMssqlConnectionInformation *pCI = static_cast<CMssqlConnectionInformation*>(GetConnectionInformation()); m_lasterror = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_hEnv); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetEnvAttr(m_hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLAllocHandle(SQL_HANDLE_DBC,m_hEnv,&m_hDbc); if(!SQL_SUCCEEDED(m_lasterror)) return false; cvs::wstring strConn; cvs::swprintf(strConn,80,L"DATABASE={%s};DRIVER={SQL Server};Pwd={%s};Server={%s};Uid={%s};", (const wchar_t*)cvs::wide(pCI->database.c_str()),(const wchar_t*)cvs::wide(pCI->password.c_str()),(const wchar_t*)cvs::wide(pCI->hostname.c_str()),(const wchar_t*)cvs::wide(pCI->username.c_str())); m_lasterror = SQLDriverConnect(m_hDbc,NULL,(SQLWCHAR*)strConn.c_str(),(SQLSMALLINT)strConn.size(),NULL,0,NULL,SQL_DRIVER_NOPROMPT); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetConnectAttr(m_hDbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0); if(!SQL_SUCCEEDED(m_lasterror)) return false; return true; }
bool COdbcConnection::Open() { COdbcConnectionInformation *pCI = static_cast<COdbcConnectionInformation*>(GetConnectionInformation()); m_lasterror = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_hEnv); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetEnvAttr(m_hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLAllocHandle(SQL_HANDLE_DBC,m_hEnv,&m_hDbc); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLConnect(m_hDbc,(SQLCHAR*)pCI->database.c_str(),SQL_NTS,(SQLCHAR*)pCI->username.c_str(),SQL_NTS,(SQLCHAR*)pCI->password.c_str(),SQL_NTS); if(!SQL_SUCCEEDED(m_lasterror)) return false; m_lasterror = SQLSetConnectAttr(m_hDbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0); if(!SQL_SUCCEEDED(m_lasterror)) return false; return true; }
bool CMssqlConnection::Open(const char *host, const char *database, const char *username, const char *password) { CMssqlConnectionInformation *pCI = static_cast<CMssqlConnectionInformation*>(GetConnectionInformation()); pCI->hostname=host?host:"localhost"; pCI->database=database?database:""; pCI->username=username?username:""; pCI->password=password?password:""; return Open(); }
const char *CMssqlConnection::parseTableName(const char *szName) { CMssqlConnectionInformation *pCI = static_cast<CMssqlConnectionInformation*>(GetConnectionInformation()); if(!szName) return NULL; if(!pCI->prefix.size()) return szName; return cvs::cache_static_string((pCI->prefix+szName).c_str()); }
bool CMssqlConnection::Create() { CMssqlConnectionInformation *pCI = static_cast<CMssqlConnectionInformation*>(GetConnectionInformation()); cvs::string db = pCI->database; pCI->database="master"; if(!Open()) return false; pCI->database=db; Execute("create database %s",db.c_str()); if(Error()) return false; Close(); return Open(); }
bool CDb2Connection::Create() { CDb2ConnectionInformation *pCI = static_cast<CDb2ConnectionInformation*>(GetConnectionInformation()); cvs::string db = pCI->database; pCI->database="system"; // fixme.. how do you open a nonspecific database in db2? if(!Open()) return false; pCI->database=db; Execute("create database %s",db.c_str()); if(Error()) return false; Close(); return Open(); }