SAS/DBCUtils.cpp

55 lines
2.1 KiB
C++
Raw Normal View History

/*
*
*/
2022-07-03 18:22:02 +08:00
#include"DBCUtils.h"
bool getConnection(std::string& pwd, _ConnectionPtr& connection) {
bool isPwdTrue = false;
_bstr_t strConnect;
if (!pwd.empty()) {//密码非空使用SQL Server身份验证
2022-07-03 18:22:02 +08:00
try {
std::string con = "Provider=SQLOLEDB.1;Password=" + pwd + "; Persist Security Info = True; User ID = sa; Initial Catalog = stuAdminSystem; Data Source = LAPTOP-4DMOD6O5";
strConnect = con.c_str();
connection->Open(strConnect, "", "", NULL);
isPwdTrue = true;//密码正确,连接成功
wprintf(L"登录成功!\n");
2022-07-03 18:22:02 +08:00
return true;
}
catch (_com_error& err) {
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
}
if (!isPwdTrue) {//密码错误连接失败打开连接失败跳出try块执行
wprintf(L"密码错误!\n");
2022-07-03 18:22:02 +08:00
return false;
}
}
else {//密码为空使用Windows 身份验证
2022-07-03 18:22:02 +08:00
try {
strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=stuAdminSystem;Data Source=LAPTOP-4DMOD6O5";
connection->Open(strConnect, "", "", NULL);
isPwdTrue = true;//验证成功,连接成功
wprintf(L"登录成功!\n");
2022-07-03 18:22:02 +08:00
return true;
}
catch (_com_error& err) {
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
}
if (!isPwdTrue) {//验证失败连接失败打开连接失败跳出try块执行
wprintf(L"验证失败!\n");
2022-07-03 18:22:02 +08:00
return false;
}
}
return false;
2022-07-03 18:22:02 +08:00
}
void getSqlType(std::string& sql, std::string& comType) {
size_t len = sql.length();
for (size_t i = 0; i < len; i++) {//获取SQL语句的类别
2022-07-03 18:22:02 +08:00
if (sql[i] != ' ')
comType += isupper(sql[i]) ? (sql[i] - 32) : sql[i];
else return;
}
}