- 论坛徽章:
- 0
|
MS sql server+c/c++
- //-------------------------------------------------------------------------
- // InitSqlServer 初始化访问SQLSERVER数据库的结构
- //-------------------------------------------------------------------------
- bool
- InitSQL (const char*UserName,const char* Password,const char *DBName,const char*DBAddr)
- {
- if (!dbinit () )
- {
- LogCCSInfo(SQL_DBLIB_INIT_ERROR);
- return false; /* initialize dblib */
- }
-
-
- login = dblogin ();
- if (!login )
- {
- printf (SQL_DBHANDLE_CREATE_ERROR_PROMPT);
- LogCCSInfo(SQL_DBHANDLE_CREATE_ERROR);
- return false;
- }
- DBSETLUSER (login,UserName);
- DBSETLPWD (login, Password);
-
- printf (SQL_CONNECT_WAIT_PROMPT);
- if(dbsetlogintime(20)!=SUCCESS){
- cout<<SQL_PARAM_SET_FAIL_PROMPT<<endl;
- return false;
- }
- dbproc = dbopen (login, DBAddr);
- if (!dbproc )
- {
- printf (SQL_OPENDATASOURCE_FAIL_PROMPT);
- LogCCSInfo(SQL_DATASOURCE_OPEN_FAIL);
- return false;
- }
- if (dbuse (dbproc, DBName) != SUCCEED)
- {
- printf (SQL_USE_DB_LIB_ERROR);
- LogCCSInfo(SQL_DATABASE_OPEN_FAIL);
- return false;
- }
- printf (SQL_INIT_SUCCEED);
- dbsetopt (dbproc, DBBUFFER, "100");
- return true;
- }
- //=========================================================================
- // CloseSqlServer 退出
- //=========================================================================
- void
- CloseSQL ()
- {
- dbexit ();
- }
- //-----------------------------------------------------------------------------------------
- // 功能:执行一条SQL语句,成功返回0失败返回-1
- //-----------------------------------------------------------------------------------------
- int
- ExecSQLCmd (const char *Cmd)
- {
- static int SqlFailCounter; //SQL失败告警统计
- if(!SysInfo.SQLIsConnect)
- return -1;
-
- RETCODE retcode;
- dbcmd (dbproc, Cmd);
- dbsqlexec (dbproc);
- retcode=dbresults (dbproc);
- if (retcode== SUCCEED){
- SqlFailCounter=0;
- return 0;
- }
-
- SqlFailCounter++;
- if(dbdead(dbproc)||SqlFailCounter>;1){
- dbclose (dbproc);
- SysInfo.SQLIsConnect=false;
- }
- return -1;
- }
复制代码 |
|