虐心87 2016-06-28 07:10 采纳率: 0%
浏览 3765

linux 连接远程sqlserver数据库?

linux 连接远程sqlserver数据库,具体如何操作,请不要把网上的那一堆复制给我,如果具体可以操作也ok?

  • 写回答

1条回答 默认 最新

  • devmiao 2016-06-28 07:15
    关注

    一、相关软件
    首先我们需要FreeTDS的安装包,现在的最新版是0.82其次就是大家需要自己搭建C++的开发环境了。
    二、软件安装、配置

    tar zxvf freetds-stable.tgz(解压)# ./configure --prefix=/usr/local/freetds \(指定FreeTDS安装路径)

    --with-tdsver=8.0 --enable-msdblib (设置TDS版本,支持SQL Server 2000)# make # make install 将freetds的库文件所在路径配置到LD_LIBRARY_PATH参数中:
    $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/:
    这么作的目的是为了避免加载FreeTds库文件加载不上的情况。
    三、程序开发
    不多说了,还是直接上代码:/*

    • SyBaseManager.h *
    • Created .: Feb 18, 2009
    • Author: Steven Wee / #ifndef SYBASEMANAGER_H_ #define SYBASEMANAGER_H_ #include \"../Common/CheckStringTools.h\" #include #include #include #include #include #include #include #include #include using namespace std; class SybaseManager { public: SybaseManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned int port); ~SybaseManager(); /
    • Init SQL Server
    • @param hosts: Host IP address
    • @param userName: Login UserName
    • @param password: Login Password
    • @param dbName: Database Name
    • @param port: Host listen port number / void initConnection(); /
    • Making query from database
    • @param mysql: MySQL Object
    • @param sql: Running SQL command / bool runSQLCommand(std::string sql); /*
    • Destroy MySQL object
    • @param mysql MySQL object / void destroyConnection(); bool getConnectionStatus(); vector > getResult(); protected: void setUserName(std::string userName); void setHosts(std::string hosts); void setPassword(std::string password); void setDBName(std::string dbName); void setPort(unsigned int port); private: bool IsConnected; DBPROCESS *dbProcess; vector< vector > resultList; unsigned int DEFAULTPORT; char * HOSTS; char * USERNAME; char * PASSWORD; char * DBNAME; }; #endif / SYBASEMANAGER_H_ / /
    • SyBaseManager.cpp *
    • Created .: Feb 18, 2009
    • Author: Steven Wee / #include \"SybaseManager.h\" SybaseManager::SybaseManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned int port) { IsConnected = false; this ->setHosts(hosts); this ->setUserName(userName); this ->setPassword(password); this ->setDBName(dbName); this ->setPort(port); } SybaseManager::~SybaseManager() { destroyConnection(); } void SybaseManager::setDBName(string dbName) { if ( dbName.empty() ) { std::cout << \"DBName is null! Used default value: master\" << std::endl; this ->DBNAME = new char; strcpy(this ->DBNAME, \"master\"); } else { this ->DBNAME = new char; strcpy(this ->DBNAME, dbName.c_str()); } } void SybaseManager::setHosts(string hosts) { if ( hosts.empty() ) { std::cout << \"Hosts is null! Used default value: localhost\" << std::endl; this ->HOSTS = new char; strcpy(this ->HOSTS, \"localhost\"); } else { this ->HOSTS = new char; strcpy(this ->HOSTS, hosts.c_str()); } } void SybaseManager::setPassword(string password) { if ( password.empty() ) { std::cout << \"Password is null! Used default value: \" << std::endl; this ->PASSWORD = new char; strcpy(this ->PASSWORD, \"\"); } else { this ->PASSWORD = new char; strcpy(this ->PASSWORD, password.c_str()); } } void SybaseManager::setPort(unsigned int port) { if ( port ) { std::cout << \"Port number is null! Used default value: 0\" << std::endl; this ->DEFAULTPORT = 0; } else { this ->DEFAULTPORT = port; } } void SybaseManager::setUserName(string userName) { if ( userName.empty() ) { std::cout << \"UserName is null! Used default value: sa\" << std::endl; this ->USERNAME = new char; strcpy(this ->USERNAME, \"sa\"); } else { this ->USERNAME = new char; strcpy(this ->USERNAME, userName.c_str()); } } void SybaseManager::initConnection() { string Charset = \"UTF-8\"; dbinit(); LOGINREC *loginREC = dblogin(); DBSETLUSER(loginREC, this ->USERNAME); DBSETLPWD(loginREC, this ->PASSWORD); DBSETLCHARSET(loginREC, Charset.c_str()); dbProcess = dbopen(loginREC, this ->HOSTS); if ( dbProcess == FAIL ) { std::cout << \"Connect to SQL Server failed!\" << std::endl; } if ( dbuse( dbProcess, this ->DBNAME ) == FAIL ) { std::cout << \"Use table failed!\" << std::endl; } } bool SybaseManager::runSQLCommand( string sql ) { dbcmd(dbProcess, sql.c_str()); if ( dbsqlexec(dbProcess) == FAIL ) { std::cout << \"Query from database failed!\" << std::endl; } DBINT result_code; vector objectValue; StringTools stringTools; sql = stringTools.filterString(sql); while ( (result_code = dbresults(dbProcess)) != NO_MORE_RESULTS ) { struct Column { char colName; char* colBuffer; int colType, colSize, colStatus; } columns, *pCol; int nColumns; int rowNo; if ( result_code == SUCCEED ) { nColumns = dbnumcols(dbProcess); if ( (columns = (Column)calloc(nColumns, sizeof(struct Column))) == NULL ) { std::cout << \"Error at bind data\" << std::endl; return false; } for ( pCol = columns; pCol - columns < nColumns; pCol++ ) { int colNo = pCol - columns + 1; pCol ->colName = dbcolname(dbProcess, colNo); pCol ->colType = dbcoltype(dbProcess, colNo); pCol ->colSize = dbcollen(dbProcess, colNo); if ( SYBCHAR != pCol ->colType ) { pCol ->colSize =
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?