首页 > 代码库 > [转载] 学习TClientDataset的使用

[转载] 学习TClientDataset的使用

[转载] 学习TClientDataset的使用 http://blog.csdn.net/onebigday/article/details/6275550

 

这个控件好像很好用,以前都是用TSimpledataset,现在突然想用一下它。要用它,好像比TSimpleDataset多几个控件,我是加了以下几个控件,TSQLConnection,TSQLQuery,TDataSetProvider,TClientDataset,TDataSource,TDBGrid。这几个控件中,TDBGrid的DataSource属性设为TDataSource,TDataSource的DataSet属性设为TClientDataset,TClientDataset的ProviderName属性设为TDataSetProvider,TDataSetProvider的Dataset属性设为TSQLQuery,TSQLQuery的SQLConnection属性设为TSQLConnection。

其中还要对TDataSetProvider设置一个属性,就是TDataSetProvider中的Options->poAllowCommandText这个属性设置为True,如果不设置为True的话,调用TClientDataset的Open时,会出现" commandtext changes are not allowed"的错误。以上设置完后,就可以用ClientDataSet1.CommantText := ‘SELECT * FROM MYTABLE‘(注意:SQL语句一定要大写,不然在调用ClientDataset1.ApplyUpdates时会出现“table or view does not exist”的错误)之类的语句设置SQL语句,然后用ClientDataSet1.Open来取得数据。在TDBGrid中修改了数据后就可以用ClientDataset1.ApplyUpdates(0)来把数据更新到数据库中了。

对于有二进制字段的,可以把TDataSetProvider->Options->poFetchBlobOnDemand设为True。这样程序一开始并不加载Blob字段的数据,如果用到了,TClientDataset会自动加载,对于我们程序员来说,是透明的,不需做额外的事,如正常一样访问TClientDataSet中的Blob字段就行了,要注意的是TClientDataSet的CommantText一定要有Select到Blob的SQL语句,而且,SQL语句一定要是大写的,不然也会出现“table or view does not exist”的错误。

完整的测试代码如下:

pas代码:

[delphi] view plaincopyprint?

unit Unit1;

 

interface

 

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, DBXpress, FMTBcd, SqlExpr, StdCtrls, DB, DBClient, Provider,

Grids, DBGrids;

 

type

TForm1 = class(TForm)

DBGrid1: TDBGrid;

DataSource1: TDataSource;

DataSetProvider1: TDataSetProvider;

ClientDataSet1: TClientDataSet;

SQLConnection1: TSQLConnection;

Button1: TButton;

Button2: TButton;

SQLQuery1: TSQLQuery;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

 

var

Form1: TForm1;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.Button1Click(Sender: TObject);

begin

ClientDataSet1.Close;

ClientDataSet1.CommandText := ‘SELECT * FROM MYTABLE1‘; //SQL语句一定要大写,不然在调用ClientDataset1.ApplyUpdates(0)时会出现“table or view does not exist”的错误

ClientDataSet1.Open;

end;

 

procedure TForm1.Button2Click(Sender: TObject);

begin

ClientDataSet1.ApplyUpdates(0);

end;

 

end.

 

dfm代码如下:

[delphi] view plaincopyprint?

object Form1: TForm1

Left = 245

Top = 238

Width = 1142

Height = 656

Caption = ‘Form1‘

Color = clBtnFace

Font.Charset = DEFAULT_CHARSET

Font.Color = clWindowText

Font.Height = -11

Font.Name = ‘MS Sans Serif‘

Font.Style = []

OldCreateOrder = False

PixelsPerInch = 96

TextHeight = 13

object DBGrid1: TDBGrid

Left = 152

Top = 88

Width = 529

Height = 233

DataSource = DataSource1

TabOrder = 0

TitleFont.Charset = DEFAULT_CHARSET

TitleFont.Color = clWindowText

TitleFont.Height = -11

TitleFont.Name = ‘MS Sans Serif‘

TitleFont.Style = []

end

object Button1: TButton

Left = 528

Top = 32

Width = 75

Height = 25

Caption = ‘Button1‘

TabOrder = 1

OnClick = Button1Click

end

object Button2: TButton

Left = 704

Top = 40

Width = 75

Height = 25

Caption = ‘Button2‘

TabOrder = 2

OnClick = Button2Click

end

object DataSource1: TDataSource

DataSet = ClientDataSet1

Left = 160

Top = 24

end

object DataSetProvider1: TDataSetProvider

DataSet = SQLQuery1

Options = [poAllowCommandText]

Left = 288

Top = 24

end

object ClientDataSet1: TClientDataSet

Aggregates = <>

Params = <>

ProviderName = ‘DataSetProvider1‘

Left = 224

Top = 16

end

object SQLConnection1: TSQLConnection

ConnectionName = ‘OracleConnection‘

DriverName = ‘Oracle‘

GetDriverFunc = ‘getSQLDriverORACLE‘

LibraryName = ‘dbexpora.dll‘

LoginPrompt = False

Params.Strings = (

‘DriverName=Oracle‘

‘DataBase=192.168.1.100‘

‘User_Name=qgtg‘

‘Password=qgtg‘

‘RowsetSize=20‘

‘BlobSize=-1‘

‘ErrorResourceFile=‘

‘LocaleCode=0000‘

‘Oracle TransIsolation=ReadCommited‘

‘OS Authentication=False‘

‘Multiple Transaction=False‘

‘Trim Char=False‘)

VendorLib = ‘oci.dll‘

Left = 376

Top = 24

end

object SQLQuery1: TSQLQuery

MaxBlobSize = -1

Params = <>

SQLConnection = SQLConnection1

Left = 336

Top = 24

end

end

 

[转载] 学习TClientDataset的使用