#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的问题还是别的。。我想改成通过传参也能实现正常的新增数据源