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 R语言中安装bibliometrix 后运行biblioshiny出现问题
  • ¥20 关于#android#的问题:用开发助手发现找不到控件(autojs)
  • ¥15 dir815漏洞反弹shell失败
  • ¥15 支付宝小程序云函数登录获取user_id失败
  • ¥50 python for 循环速度慢
  • ¥15 CubeMX生成的代码用keil编译有报错
  • ¥15 Stata链式中介效应代码修改
  • ¥15 pip安装PyAV报错
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?