使用这些原装的组件,访问数据库,操作简单,使用方便,超级好用!
一、查询数据
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) ;
本文详细介绍了如何使用Delphi的TADOQuery进行数据库查询,并展示了如何执行SQL命令,包括设置连接参数、执行SQL语句及记录影响。通过实例代码,读者能快速掌握基本的数据库操作技巧。
1081

被折叠的 条评论
为什么被折叠?



