當前位置:首頁 » 文件傳輸 » wex5文件上傳
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

wex5文件上傳

發布時間: 2023-08-04 11:02:25

㈠ wex5怎麼連接資料庫

//初始化資料庫連接
::CoInitialize(NULL);
連接資料庫
[cpp] view plainprint?

BOOL CLogin::ConnectDB(void)
{
HRESULT hr = NULL;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///創建 Connection 對象

if(SUCCEEDED(hr))
{
//登錄資料庫並連接資料庫DBCourse
//這里的server不能寫成127.0.0.1否則會出現未指定的錯誤,應該寫成Sql server登錄界面的伺服器名稱
m_pConnection->ConnectionString = "driver={SQL Server};server=LOGO-PC\\LOGO;uid=DBCourse;pwd=DBCourse;";
m_pConnection->Open("","","",adConnectUnspecified);
m_pConnection->DefaultDatabase = "DBCourse";
}
else
{
AfxMessageBox(TEXT("創建 Connection 對象失敗"));///顯示錯誤信息
return FALSE;
}
}
catch(_com_error e)///捕捉異常
{
CString errormessage;
errormessage.Format( TEXT("連接資料庫失敗 !\r\n 錯誤信息 :%s(%ld)"),e.ErrorMessage(),e.Error() );
AfxMessageBox(errormessage);///顯示錯誤信息
return FALSE;
}
return TRUE;
}
操作資料庫
[cpp] view plainprint?

void CLogin::OnBnClickedButton1()
{
if( m_user.GetWindowTextLength()<=0 || m_password.GetWindowTextLength()<=0 )
{
MessageBox(TEXT("請輸入用戶名或密碼!"), TEXT("錯誤:用戶名或密碼為空"), MB_OK | MB_ICONWARNING );
return;
}
if( FALSE==this->ConnectDB() ) return;

TCHAR szUserName[20];
TCHAR szPassword[50];
TCHAR szSql[MAX_PATH];

//獲取用戶輸入的用戶名和密碼
m_user.GetWindowText( szUserName, 20 );
m_password.GetWindowText( szPassword, 50 );

//創建_RecordsetPtr用來執行資料庫操作
_RecordsetPtr pRecordset;
pRecordset.CreateInstance("ADODB.Recordset");

_tcscpy_s( szSql, TEXT("select * from usr where uname='") );
_tcscat_s( szSql, szUserName );
_tcscat_s( szSql, TEXT("' and passwd='") );
_tcscat_s( szSql, szPassword );
_tcscat_s( szSql, TEXT("';") );
pRecordset->Open(_variant_t(szSql), _variant_t ((IDispatch*) m_pConnection,true), adOpenStatic, adLockOptimistic, adCmdText);

int nResult = DLG_RESULT_OK;
if( pRecordset->RecordCount>0 )
{
//MessageBox(TEXT("登錄成功!"), szUserName, MB_OK | MB_ICONINFORMATION );
}
else
{
MessageBox(TEXT("用戶名或密碼錯誤!"), TEXT("登錄失敗!"), MB_OK | MB_ICONINFORMATION );
nResult = DLG_RESULT_ERROR;
}
pRecordset->Close();
if( DLG_RESULT_OK==nResult ) Exit( DLG_RESULT_OK );
}
哦忘了一件事了,還需要在stafx.h文件中引入dll文件的,如此
[cpp] view plainprint?
//添加dll庫
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
//添加dll庫

//添加宏定義
#define DLG_RESULT_OK 1
#define DLG_RESULT_ERROR 2
#define DLG_RESULT_CANCEL 3
//添加宏定義