2022-07-06 14:06:57 +08:00
/*
* 数 据 库 连 接 工 具 函 数 接 口 实 现
*/
2022-07-03 18:22:02 +08:00
# include"DBCUtils.h"
bool getConnection ( std : : string & pwd , _ConnectionPtr & connection ) {
bool isPwdTrue = false ;
_bstr_t strConnect ;
2022-07-06 14:06:57 +08:00
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 ) ;
2022-07-06 14:06:57 +08:00
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 ( ) ) ;
}
2022-07-06 14:06:57 +08:00
if ( ! isPwdTrue ) { //密码错误, 连接失败( 打开连接失败, 跳出try块执行)
wprintf ( L " 密码错误! \n " ) ;
2022-07-03 18:22:02 +08:00
return false ;
}
}
2022-07-06 14:06:57 +08:00
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 ) ;
2022-07-06 14:06:57 +08:00
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 ( ) ) ;
}
2022-07-06 14:06:57 +08:00
if ( ! isPwdTrue ) { //验证失败, 连接失败( 打开连接失败, 跳出try块执行)
wprintf ( L " 验证失败! \n " ) ;
2022-07-03 18:22:02 +08:00
return false ;
}
}
2022-07-04 22:17:38 +08:00
return false ;
2022-07-03 18:22:02 +08:00
}
void getSqlType ( std : : string & sql , std : : string & comType ) {
2022-07-04 22:17:38 +08:00
size_t len = sql . length ( ) ;
2022-07-06 14:06:57 +08:00
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 ;
}
2022-07-06 14:06:57 +08:00
}