使用这些原装的组件,访问数据库,操作简单,使用方便,超级好用!
一、查询数据
TADOQuery* pQuery = new TADOQuery(this);
pQuery->ConnectionString ="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1" ;
pQuery->CursorType =ctStatic;
pQuery->LockType = ltOptimistic ;
pQuery->SQL->Text = "select * From table where id = '1'" ;pQuery->Open() ;
Edit2->Text = pQuery->FieldByName("deptname")->AsString ;
Edit1->Text = pQuery->Fields->FieldByNumber(3)->AsString;
Edit3->Text = IntToStr(pQuery->RecordCount) +"行" ;
pQuery->Close() ;
二、执行SQL命令
//执行命令
int RecordsAffected ;
OleVariant Parameters = 1 ;
TADOCommand* pCmd = new TADOCommand(this) ;
pCmd->Connection = ADOConnection1 ;
pCmd->CommandText = "update table1 set name = 'test2' where id = '1' " ;
ADOConnection1->BeginTrans() ;
pCmd->Execute(RecordsAffected,Parameters) ;
ADOConnection1->CommitTrans() ;Edit3->Text = "影响行数:" + IntToStr(RecordsAffected) ;