请教ClientDataSet怎样编写SQL语句呢?200分(200分)

  • 主题发起人 主题发起人 copyyour
  • 开始时间 开始时间
C

copyyour

Unregistered / Unconfirmed
GUEST, unregistred user!
我做的客户端用SocketConnection连接服务器端,再用ClientDataSet连接
SocketConnection,但是ClientDataSet不能象Adoquery那样写SQl,请教各位大哥,怎样才能
让连接SocketConnection的组件支持SQl呢?谢谢了200分
 
这样
with ClientDataSet do
begin
Close;
CommandText := '...';
Open;
end;
 
下面是我实际用的:
procedure TfrmScAdmin.btbQueryPage2Click(Sender: TObject);
var
strBaseSql, strSql: string;
begin
inherited;
strBaseSql := 'SELECT U.userType,U.userNo FROM TscUsers U INNER JOIN ThrEmployees E ' +
' ON U.userNo = E.empNo WHERE U.status<>''D'' and U.userType=''U''';

strSql := '';

if trim(edtUserNo2.Text) <> '' then
strSql := strSql + ' and U.userNo=''' + edtUserNo2.Text + '''';
if trim(edtDeptNo2.Text) <> '' then
strSql := strSql + ' and E.deptNo=' + edtDeptNo2.Text;

DMSC.cdsUser2.Close; //DMSC.cdsUser2 是ClientDataSet
DMSC.cdsUser2.CommandText := strBaseSql + strSql;
DMSC.cdsUser2.Open;
end;
 
把服务器端的DataSetProvider1的options属性设置为包含poAllowCommandText即可
 
不太明白
有没有象ADOquery那样的呀。555~
谢谢了
 
procedure TFormContent.ButtonAdd_OKClick(Sender: TObject);
var
Login: TELNETANDFTPLOGIN;
Echo: PChar;
MiniSecond, MPEGFORMAT: String ;
B1, B2: Boolean;
bitrate, milliseconds: Integer;
StringList1: TStringList;
begin
Screen.Cursor := StartCursor;
B1 := False;
B2 := False;
if EditAdd_CONTENT_NAME.Text = '' then
begin
Application.MessageBox('节目名称不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_CONTENT_NAME.SetFocus;
end
else
if EditAdd_SALE_PRICE.Text = '' then
begin
Application.MessageBox('标准价格不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_SALE_PRICE.SetFocus;
end
else
if IsFloat(EditAdd_SALE_PRICE.Text) = False then
begin
Application.MessageBox('标准价格不是数值。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_SALE_PRICE.SetFocus;
end
else
if StrToFloat(EditAdd_SALE_PRICE.Text) < 0 then
begin
Application.MessageBox('标准价格必须大于等于零。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_SALE_PRICE.SetFocus;
end
else
if EditAdd_PERTIMES_PRICE.Text = '' then
begin
Application.MessageBox('每次价格不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_PERTIMES_PRICE.SetFocus;
end
else
if IsFloat(EditAdd_PERTIMES_PRICE.Text) = False then
begin
Application.MessageBox('每次价格不是数值。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_PERTIMES_PRICE.SetFocus;
end
else
if StrToFloat(EditAdd_PERTIMES_PRICE.Text) < 0 then
begin
Application.MessageBox('每次价格必须大于等于零。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_PERTIMES_PRICE.SetFocus;
end
else
if EditAdd_CONTENT_SUBCATEGORY_NAME.Text = '' then
begin
Application.MessageBox('节目子类不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_CONTENT_SUBCATEGORY_ID.SetFocus;
end
else
if EditAdd_RATING_NAME.Text = '' then
begin
Application.MessageBox('节目等级不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_RATING_ID.SetFocus;
end
else
if EditAdd_PROVIDER_NAME.Text = '' then
begin
Application.MessageBox('节目供应商不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_PROVIDER_ID.SetFocus;
end
else
if EditAdd_Location.Text = '' then
begin
Application.MessageBox('节目位置不能为空。', MessageTitle, WARNING_ICON + MB_OK);
EditAdd_Location.SetFocus;
end
else
begin
try
with DataModule1.Clientdataset1 do
begin
Close ;
CommandText := Format('Select MEDIA_FILENAME From CONTENT_CODE Where MEDIA_FILENAME = ''%s'' ',[FormContentLocation.ListView1.Selected.Caption]) ;
Open ;
if RecordCount > 0 then
B1 := True;
end;
if B1 = True then
Application.MessageBox('影片文件和其它节目的影片文件重名。', MessageTitle, WARNING_ICON + MB_OK)
else
begin
with DataModule1.ClientDataSet1 do
begin
Close ;
CommandText := Format('Select TELNETUSER_NAME, TELNET_PASSWORD, FTPUSER_NAME, FTP_PASSWORD from Device_Code where Device_ID = ''%s'' ', [SelectNcube]) ;
Open ;
Login.TelUsername := PChar(FieldByName('TELNETUSER_NAME').AsString) ;
Login.TelPassword := PChar(FieldByName('TELNET_PASSWORD').AsString) ;
Login.FTPUsername := PChar(FieldByName('FTPUSER_NAME').AsString) ;
Login.FTPPassword := PChar(FieldByName('FTP_PASSWORD').AsString) ;

Close;
CommandText := Format('Select IP_ADDRESS, PORT_NO from Device_Port where PORT_TYPE = ''%s'' and DEVICE_ID = ''%s'' ', ['TELNET',SelectNcube]);
Open;
if RecordCount > 0 then
begin
Login.IP := PChar(FieldByName('IP_ADDRESS').AsString) ;
Login.TelPort := FieldByName('PORT_NO').AsInteger;
GetMem(Echo, 2048);
StringList1 := TStringList.Create();
try
DllVstagPrint(@Login, PChar(SelectLPARTITION), PChar(Copy(FormContentLocation.ListView1.Selected.Caption, 1, Length(FormContentLocation.ListView1.Selected.Caption) - 4) + '.mpi'), Echo);
StringList1.SetText(Echo);
MPEGFORMAT := Trim(StringList1.Strings[0]);
bitrate := StrToInt(Trim(StringList1.Strings[1]));
milliseconds := StrToInt(Trim(StringList1.Strings[2]));
Except
B2 := True;
end;
StringList1.Free;
FreeMem(Echo, 2048);

if B2 = False then
begin
try
begin
with DataModule1.ADD_CONTENT_CODE85ClientDataSet do
begin
Close;
FetchParams;
Params.ParamByName('ARG_CONTENT_NAME').AsString := EditAdd_CONTENT_NAME.Text;
Params.ParamByName('ARG_CONTENT_TITLE').AsString := '';
Params.ParamByName('ARG_SPELLING_CODE').AsString := EditAdd_SPELLING_CODE.Text;
Params.ParamByName('ARG_ORIGINAL_NAME').AsString := EditAdd_ORIGINAL_NAME.Text;
Params.ParamByName('ARG_ALIAS').AsString := '';
Params.ParamByName('ARG_CAST_NAME').AsString := EditAdd_CAST_NAME.Text;
Params.ParamByName('ARG_METRO_UNIT').AsString := ComboBoxAdd_METRO_UNIT.Text;
Params.ParamByName('ARG_SALE_PRICE').AsString := EditAdd_SALE_PRICE.Text;
Params.ParamByName('ARG_LENGTH').AsString := FormContentLocation.ListView1.Selected.SubItems[0] ;
Params.ParamByName('ARG_MILLISECS').AsInteger := milliseconds;
with DataModule1.ClientDataSet1 do
begin
Close;
CommandText := Format('Select a.CATEGORY_ID From CONTENT_SUBCATEGORY a Where SUBCATEGORY_ID=''%S'' ',[EditAdd_CONTENT_SUBCATEGORY_ID.Text]);
Open;
end;
Params.ParamByName('ARG_CATEGORY_ID').AsString := DataModule1.ClientDataSet1.FieldByName('CATEGORY_ID').AsString;
Params.ParamByName('ARG_SUBCATEGORY_ID').AsString := EditAdd_CONTENT_SUBCATEGORY_ID.Text;
Params.ParamByName('ARG_RATING_ID').AsString := EditAdd_RATING_ID.Text;
Params.ParamByName('ARG_FILENAME').AsString := Copy(FormContentLocation.ListView1.Selected.Caption, 1, Length(FormContentLocation.ListView1.Selected.Caption) - 4) + '.mpi';
Params.ParamByName('ARG_MEDIA_FILENAME').AsString := FormContentLocation.ListView1.Selected.Caption;
MiniSecond := EditAdd_TIMESLICE.Text + '000';
Params.ParamByName('ARG_TIMESLICE').AsString := MiniSecond;
Params.ParamByName('ARG_TIMESLICE_PRICE').AsFloat := StrToFloat(EditAdd_SALE_PRICE.Text) * (StrToInt(MiniSecond)/milliseconds);
Params.ParamByName('ARG_CREATION_DATE').AsDateTime := Now();
Params.ParamByName('ARG_LASTMODIFIED_DATE').AsDateTime := Now();
Params.ParamByName('ARG_OBSOLETE_DATE').AsDateTime := Now();

//Reg := TRegistry.Create;
Reg.RootKey :=HKEY_CURRENT_USER;
Reg.OpenKey(CB_RegKey, True) ;
if Reg.ValueExists(CB_LoginUsername) then
Begin
Params.ParamByName('ARG_ADDED_BY').AsString := Reg.ReadString(CB_LoginUsername) ;
//Reg.Free ;
end
else
begin
Reg.WriteString(CB_LoginUsername, 'SUPPER MAN');
Params.ParamByName('ARG_ADDED_BY').AsString := Reg.ReadString(CB_LoginUsername) ;
//Reg.Free ;
end ;

Params.ParamByName('ARG_DESCRIPTION').AsString := '';
Params.ParamByName('ARG_ENCODE_TYPE').AsInteger := bitrate;
Params.ParamByName('ARG_VIDEO_FORMAT').AsString := MPEGFORMAT;
Params.ParamByName('ARG_CONTENT_SOURCE').AsString := '';
Params.ParamByName('ARG_PROVIDER_ID').AsString := EditAdd_PROVIDER_ID.Text;
Params.ParamByName('ARG_LINK_URL').AsString := EditAdd_LINK_URL.Text;
Params.ParamByName('ARG_VOD_FLAG').AsString := '10';
Params.ParamByName('ARG_PERTIMES_PRICE').AsString := EditAdd_PERTIMES_PRICE.Text;
Params.ParamByName('ARG_DIRECT_NAME').AsString := EditAdd_DIRECT_NAME.Text;
Params.ParamByName('ARG_PICTURE_URL').AsString := EditAdd_PICTURE_URL.Text;

Params.ParamByName('arg_VODSERVER_ID').AsString := SelectNcube;
with DataModule1.ClientDataSet1 do
begin
Close;
CommandText := Format('Select DEVICE_NAME From DEVICE_CODE Where DEVICE_ID=''%S'' ',[SelectNcube]);
Open;
end;
Params.ParamByName('arg_VODSERVER_NAME').AsString := DataModule1.ClientDataSet1.FieldByName('DEVICE_NAME').AsString;
Params.ParamByName('arg_PATH').AsString := SelectLPARTITION;
Params.ParamByName('arg_TOTAL_CLICKS').AsString := '0';
Params.ParamByName('arg_LOAD_DATE').AsDateTime := Now();
Params.ParamByName('arg_AVAILABLE_FLAG').AsString := 'AVAILABLE';
Execute;
end;
Application.MessageBox('点播节目增加成功。', MessageTitle, OK_ICON + MB_OK);
end;
except
Application.MessageBox('数据库异常。', MessageTitle, ERROR_ICON + MB_OK);
end;
end
else
Application.MessageBox('此视频文件不是完整文件。', MessageTitle, ERROR_ICON + MB_OK);
end
else
Application.MessageBox('没有TELNET端口数据。', MessageTitle, WARNING_ICON + MB_OK);
end;
end;
//SaveCookie(Direct_Name, EditAdd_DIRECT_NAME.Text, Direct_Name_Len);
except
Application.MessageBox('点播节目增加失败。', MessageTitle, ERROR_ICON + MB_OK);
end;
end;
Screen.Cursor := EndCursor;
end;

看看吧
留下你的 qq或 mail
 
其实是一样的
query是这样
with query do
begin
Close;
sql.text := '...';
Open;
end;

这和
with ClientDataSet do
begin
Close;
CommandText := '...';
Open;
end; 不是一样的吗?这样动态的写,比你在写query控件的sql属性里写死好多了
 
我试试先,晚上给分
 
其实最主要的是:你的Clientdataset对应的服务器端的
DataSetProvider1的options属性设置为包含poAllowCommandText.
重新编译服务器后,
sqlstr := '你的SQL语句';
Clientdataset.Close;
Clientdataset.CommandText :=sqlstr;
Clientdataset.open;
就可以啦!
 
procedure TLogin.TeThemeButton1Click(Sender: TObject);
begin
with CrmDm.ClientDataSet6 do
begin
CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'where 用户名=''' + TeThemeEdit1.Text + ''' and ';
CrmDm.ClientDataSet6.CommandText := '密码 = ''' + TeThemeEdit2.Text + '''';
CrmDm.ClientDataSet6.Open;
if CrmDm.ClientDataSet6.Eof then
begin
showmessage('请注意!你的用户名或者是密码出错!请核对后输入') ;
//dm.ADOQuery2.close;
Application.Terminate;
end
else
begin
CrmDm.ClientDataSet6.close;
end;
end;{with}
close;
end;

编译通过但是运行出错,怎么回事呢?555~~
 
>>>>> if CrmDm.ClientDataSet6.Eof then
改为 if CrmDm.ClientDataSet6.IsEmpty then 试试。 这个判断是否查找到记录。
 
哪能这样啊
CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'where 用户名=''' + TeThemeEdit1.Text + ''' and ';
CrmDm.ClientDataSet6.CommandText := '密码 = ''' + TeThemeEdit2.Text + '''';
CrmDm.ClientDataSet6.Open;
应该这样
CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'select * from Tablename where 用户名=''' + TeThemeEdit1.Text + ''' and '...';
CrmDm.ClientDataSet6.Open;
 
谢谢王建伟大哥,我回家试试就给分给大家
 
只要看李维写的那本书就可以[8D]
 
可不可以写成这样的

CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'select * from Tablename ';
CrmDm.ClientDataSet6.CommandText := 'where 用户名=''' + TeThemeEdit1.Text + '''';
CrmDm.ClientDataSet6.CommandText := 'and 密码= ''' + TeThemeEdit2.Text + '''';
CrmDm.ClientDataSet6.Open;

另外我写成这样

procedure TLogin.TeThemeButton1Click(Sender: TObject);
begin
with CrmDm.ClientDataSet6 do
begin
CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'select * from 用户账号 where 用户名=''' + TeThemeEdit1.Text + ''' and 密码 = ''' + TeThemeEdit2.Text + '''';
CrmDm.ClientDataSet6.Open;
if CrmDm.ClientDataSet6.Eof then
begin
showmessage('请注意!你的用户名或者是密码出错!请核对后输入') ;
//dm.ADOQuery2.close;
Application.Terminate;
end
else
begin
showmessage('测试成功');
//CrmDm.ClientDataSet6.close;
end;
end;{with}
close;
end;

出错了,当我密码输入正确的时候,不能谈出测试成功的对话框,不知道怎么回事,
希望得到大哥指点
 
要写成:
CrmDm.ClientDataSet6.Close;
CrmDm.ClientDataSet6.CommandText := 'select * from Tablename ';
CrmDm.ClientDataSet6.CommandText :=CrmDm.ClientDataSet6.CommandText+ 'where 用户名=''' + TeThemeEdit1.Text + '''';
CrmDm.ClientDataSet6.CommandText := CrmDm.ClientDataSet6.CommandText+'and 密码= ''' + TeThemeEdit2.Text + '''';
CrmDm.ClientDataSet6.Open;

if CrmDm.ClientDataSet6.Eof then改成
if CrmDm.ClientDataSet6.Bof and CrmDm.ClientDataSet6.Eof then
试试
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部