- 论坛徽章:
- 0
|
请教linux系统中如何通过ODBC访问远端SQL SERVER数据库
我对数据库的操作基本上的代码如下,自己感觉写的不好,见笑了。
- #ifndef __SYBASE_H__
- #define __SYBASE_H__
- class SYDatabase {
- public:
- SYDatabase(void);
- ~SYDatabase(void);
- public:
- int Initial(void);
- int Connect(const char *host, int port, const char *username, const char *passwd);
- void Disconnect(void);
- friend class SYRecordSet;
- protected:
- void *m_tds;
- void *m_login;
- void *m_context;
- };
- #endif /* __SYBASE_H__ */
复制代码
- #include <stdio.h>;
- #include <unistd.h>;
- #include <stdlib.h>;
- #include <string.h>;
- #include <locale.h>;
- #include <sys/time.h>;
- #include <time.h>;
- #include <assert.h>;
- #include <tds.h>;
- #include <tdsconvert.h>;
- #include "sybase.h"
- SYDatabase::SYDatabase()
- {
- m_tds = NULL;
- m_login = NULL;
- m_context = NULL;
- }
- SYDatabase::~SYDatabase()
- {
- Disconnect();
- }
- int
- tsql_handle_message(TDSCONTEXT * context, TDSSOCKET * tds, TDSMSGINFO * msg)
- {
- if (msg->;msg_number == 0) {
- fprintf(stderr, "%s\n", msg->;message);
- return 0;
- }
- if (msg->;msg_number != 5701 && msg->;msg_number != 20018) {
- fprintf(stderr, "Msg %d, Level %d, State %d, Server %s, Line %d\n%s\n",
- msg->;msg_number, msg->;msg_level, msg->;msg_state, msg->;server, msg->;line_number, msg->;message);
- }
- return 0;
- }
- int SYDatabase::Initial(void)
- {
- TDSLOGIN *login = NULL;
- TDSCONTEXT *context = NULL;
-
- do {
- login = tds_alloc_login();
- if (!login)
- break;
- context = tds_alloc_context();
- if (!context)
- break;
- if (context->;locale && !context->;locale->;date_fmt){
- context->;locale->;date_fmt = strdup("%Y%m%d");
- }
- context->;msg_handler = tsql_handle_message;
- context->;err_handler = tsql_handle_message;
- m_login = (void*)login;
- m_context = (void*)context;
- return 0;
- }while(0);
- if (login)
- tds_free_login(login);
- if (context)
- tds_free_context(context);
- return -1;
- }
- int SYDatabase::Connect(const char *host, int port, const char *user, const char *passwd)
- {
- TDSLOGIN *login = (TDSLOGIN*)m_login;
- TDSCONTEXT *context = (TDSCONTEXT*)m_context;
- TDSSOCKET *tds = NULL;
- TDSCONNECTINFO *connect_info = NULL;
- do {
- if (!login || !context)
- break;
- tds_set_user(login,user);
- tds_set_app(login,"SYDATABASE_CLASS");
- tds_set_library(login,"TDS-Library");
- tds_set_server(login,host);
- tds_set_port(login,port);
- tds_set_charset(login,"roman8");
- tds_set_language(login,"us_english");
- tds_set_packet(login,512);
- tds_set_passwd(login,passwd);
- tds = tds_alloc_socket(context,512);
- if (!tds)
- break;
- tds_set_parent(tds,NULL);
- connect_info = tds_read_config_info(NULL,login,context->;locale);
- if (!connect_info || tds_connect(tds, connect_info) == TDS_FAIL)
- break;
- tds_free_connect(connect_info);
- m_tds = (void*)tds;
- return 0;
- }while(0);
- if (connect_info)
- tds_free_connect(connect_info);
- if (tds)
- tds_free_socket(tds);
- return -1;
- }
- void SYDatabase::Disconnect(void)
- {
- if (m_tds){
- tds_free_socket((TDSSOCKET*)m_tds);
- m_tds = NULL;
- }
- if (m_login){
- tds_free_login((TDSLOGIN*)m_login);
- m_login = NULL;
- }
- if (m_context){
- tds_free_context((TDSCONTEXT*)m_context);
- m_context = NULL;
- }
- }
复制代码 |
|