Delphi combination of TDataSetProvider, TDataSet, TAdoQuery, TDataSource control use
As usual on the first control layout diagram:
1, File-New-VCL Forms Application, fill in one by one to find the controls
Use controls: Button, TDataSetProvider, TDataSet, TAdoQuery, TDataSource, DbgridEh
Set point to the relationship between the controls:
1> ds1 ds2 point of DataSet
2> dtsprvdr1 DataSet point of qry1
3> ds2 of ProviderName point dtsprvdr1
4> DbGrideh the DataSource point ds1
2、File-New-Other-DataModule,确定
Add a Adoconnection control for even connect to the database
For convenience, simply double-click the control, configure the connection string
save document
3, this time back to the main form pas file, the new reference DataModual upper unit,
Qry1 found in the main window, set his DataSource, pointing DataModule5.ADOConnection1
4, to increase the SQL property qry1 statement to query, for example: select * from testTable
5, the main form pas file increasing function:
1 procedure GetConnected(Conn: TADOConnection); 2 begin 3 if Conn.Connected = False then 4 begin 5 Conn.Connected := True; 6 end; 7 end; 8 9 function QuerySql(Sql: string; Conn: TADOConnection): boolean; 10 var 11 AdoQuery:TADOQuery; 12 begin 13 Result := False; 14 try 15 try 16 GetConnected(Conn); 17 An AdoQuery:. = TADOQuery the Create ( nil ); // must create an instance of the class 18 is AdoQuery.Close; . 19 AdoQuery.Connection: = Conn; 20 is AdoQuery.SQL.Clear; 21 is AdoQuery.SQL.Add (the Sql); 22 is an AdoQuery .Open; 23 is the Result: = AdoQuery.RecordCount> 0 ; 24 the except 25 ON E: Exception do 26 is the begin 27 the ShowMessage ( ' statement: ' + the Sql + # 13 is # 10 + 'Exception class name: ' + E.ClassName + # 13 # 10 + ' exception message: ' + e.Message); 28 29 Application.MessageBox ( ' ! Querying the database fails, please refer to ' , ' tips ' , MB_OK + MB_ICONINFORMATION ); 30 the Result: = False; 31 is End ; 32 End ; 33 is the finally 34 is End ; 35 End ;
6, double-click the Button button:
1 QuerySql('select * from testTable',DataModule5.ADOConnection1); 2 ds2.Active := False; 3 ds2.Active := True;
7, ok, save, compile and run
Reproduced in: https: //www.cnblogs.com/OSKnown/p/8624761.html