weixin_46027762 2024-02-23 17:52 采纳率: 64.7%
浏览 5
已结题

odbc代码新增sqlserver数据源


#include <iostream>
#include <vector>
#include <Windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
#include <string>

#include <locale>
#include <codecvt>

#include <comutil.h>  
#pragma comment(lib, "comsuppw.lib")

using namespace std;
#pragma comment(lib, "legacy_stdio_definitions.lib")

inline std::wstring to_wide_string(const std::string& input)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    return converter.from_bytes(input);
}


wstring string2wstring(string str)
{
    wstring result;
    //获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    TCHAR* buffer = new TCHAR[len + 1];
    //多字节编码转换成宽字节编码  
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = '\0';             //添加字符串结尾  
    //删除缓冲区并返回值  
    result.append(buffer);
    delete[] buffer;
    return result;
}
int add_odbc()
{
    LPCWSTR lpszDriver = L"SQL Server";
    std::wstring aa = string2wstring("DSN=192.168.10.10\0SERVER=192.168.10.10\0");
    std::vector<wchar_t> data;

    for (wchar_t c : aa) {
        data.push_back(c);
    }

    // Add an extra null terminator at the end
    data.push_back(L'\0');

    LPCWSTR lpszAttributes = data.data();

    std::wcout << lpszAttributes << std::endl;

    BOOL result = SQLConfigDataSource(NULL, ODBC_ADD_DSN, lpszDriver, lpszAttributes);

    if (result) {
        std::wcout << L"Data source configured successfully." << std::endl;
    }
    else {
        std::wcout << L"Error configuring data source." << std::endl;
    }

    return 0;
}
int main() {

    add_odbc();
    return 0;
}

我通过上面的代码新增odbc数据源时,数据源的名称和服务器都为192.168.10.10,但是我通过string的字符串拼接后

#include <iostream>
#include <vector>
#include <Windows.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
#include <string>

#include <locale>
#include <codecvt>

#include <comutil.h>  
#pragma comment(lib, "comsuppw.lib")

using namespace std;
#pragma comment(lib, "legacy_stdio_definitions.lib")

inline std::wstring to_wide_string(const std::string& input)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    return converter.from_bytes(input);
}


wstring string2wstring(string str)
{
    wstring result;
    //获取缓冲区大小,并申请空间,缓冲区大小按字符计算  
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    TCHAR* buffer = new TCHAR[len + 1];
    //多字节编码转换成宽字节编码  
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = '\0';             //添加字符串结尾  
    //删除缓冲区并返回值  
    result.append(buffer);
    delete[] buffer;
    return result;
}
int aabba(string ip)
{
    LPCWSTR lpszDriver = L"SQL Server";
    std::wstring aa = string2wstring("DSN="+ip+"\0SERVER="+ip+"\0");
    std::vector<wchar_t> data;

    for (wchar_t c : aa) {
        data.push_back(c);
    }

    // Add an extra null terminator at the end
    data.push_back(L'\0');

    LPCWSTR lpszAttributes = data.data();

    std::wcout << lpszAttributes << std::endl;

    BOOL result = SQLConfigDataSource(NULL, ODBC_ADD_DSN, lpszDriver, lpszAttributes);

    if (result) {
        std::wcout << L"Data source configured successfully." << std::endl;
    }
    else {
        std::wcout << L"Error configuring data source." << std::endl;
    }

    return 0;
}
int main() {

    aabba("192.168.10.10");
    return 0;
}

这时候新增的数据源的名称和服务器就变成192.168.10.10192.168.10.10,两个连续的ip值了,我不知道是因为string转化为wstring的问题还是别的。。我想改成通过传参也能实现正常的新增数据源

  • 写回答

17条回答 默认 最新

  • 急速光粒 2024-02-26 11:29
    关注

    这个需要拼接二进制字符串,完成\0分割字符串的拼接才可以正确连接。有帮助请采纳,谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 已采纳回答 2月26日
  • 创建了问题 2月23日

悬赏问题

  • ¥15 安霸cv22 + rtl8211f 千兆,udp传输丢包
  • ¥15 关于区块链和边缘环境搭建的相关问题
  • ¥15 windows远程桌面断卡重连软件卡顿问题
  • ¥30 Unity 实现扫描效果
  • ¥15 HbuilderX检测不到安卓模拟器
  • ¥15 这个main已经在filename.obj中定义是什么错 C语言
  • ¥15 关于#linux#的问题:exsi8.0系统 怎么更改web访问端口,不用80、443
  • ¥15 使用elementor设计样式
  • ¥15 谁能提供一个中文版的推销咨询网站连接?
  • ¥15 springboot项目程序启动报错