加一个问题:(这段SQL语句应该怎么写?)procedure TFormMain.Button1Click(Sender: TObject); var sql:string; begin sql:='select * from xiaoshoujilu where shijian between 2003-2-1 and 2003-4-30'; //错误的地方 dm.ADOQueryTJ.Close; dm.ADOQueryTJ.SQL.Add(sql); dm.ADOQueryTJ.Open; end; //如果sql:='select * from xiaoshoujilu where like %2003-3%'; 也错误 使用ADOQuery组件,ACCESS数据库。
Re:加一个问题:(这段SQL语句应该怎么写?)procedure TFormMain.Button1Click(Sender: TObject); var sql:string; begin sql:='select * from xiaoshoujilu where shijian between '''2003-2-1''' and '''2003-4-30''' '; //错误的地方 dm.ADOQueryTJ.Close; dm.ADOQueryTJ.SQL.Add(sql); dm.ADOQueryTJ.Open; end; //如果sql:='select * from xiaoshoujilu where like '''%2003-3%''' '; 也错误 使用ADOQuery组件,ACCESS数据库。
对了,刚才没看仔细,下面这句: //如果sql:='select * from xiaoshoujilu where like '''%2003-3%''' '; 也错误 使用ADOQuery组件,ACCESS数据库。在‘where’ 后应该加个字段名
procedure TFormMain.Button1Click(Sender: TObject); var sql,s1,s2:string; begin s1:='2003-2-1'; s2:='2003-4-30'; sql:='select * from xiaoshoujilu where shijian between '+''''+s1+''''+ ' and '+''''+s2+''''; //错误的地方 dm.ADOQueryTJ.Close; dm.ADOQueryTJ.SQL.Add(sql); dm.ADOQueryTJ.Open; end; 这样试一下,应该可以的.
// sql:='select * from xiaoshoujilu where shijian between ''2003-2-1'' and ''2003-4-30'' ';
between ……and 语句有结果了,那么谁讲一讲like语句呢?
我建议你还是看一看数据库的书。一般的书上都有关于LIKE和between子句的讲解。用ADoQuery控件就可以实现。一是用SQL语句,二是用filter 下面是用SQL的例子: ADODataset.Close; ADODataset.CommandText:='select * from message where content like "%'+edit.Text+'%" and state=0 order by id asc'; ADODataset.Open;%是通配符,就相当于DOS中的那个*号的作用。 你在DBGIRD中设置好了数据源后,再运行上面的代码。就会自动的把查询的结果返回到DBGIRD中。下面是一个按了查询按钮以后的具体例子: procedure TForm_find.BitBtn3Click(Sender: TObject); begin if edit.Text = '' then begin Application.MessageBox('查询关键字不能为空!','系统信息',MB_OK+MB_ICONERROR); edit.SetFocus; end else //---按内容查询模块--- begin ADODataset.Close; ADODataset.CommandText:='select * from message where content like "%'+edit.Text+'%" and state=0 order by id asc'; ADODataset.Open; if ADODataset.RecordCount < 1 then begin Application.MessageBox('没有找到符合条件的记录!','系统信息',MB_OK+MB_ICONINFORMATION); end else begin //Application.MessageBox('查找成功!共找到ADODataset.RecordCount条短信。','系统信息',MB_OK+MB_ICONINFORMATION); Application.MessageBox(pchar('查找成功!共找到'+IntToStr(ADODataset.RecordCount)+'条记录。'),'系统信息',MB_OK+MB_ICONINFORMATION); //或者可以这样写: //MessageBox(self.handle,pchar('查找成功!共找到'+IntToStr(ADODataset.RecordCount)+'条记录。'),'系统信息',MB_OK+MB_ICONINFORMATION); edit.Text:=''; edit.SetFocus; end; end; end;
DBGrid绑定到ADODataSet,对ADODataSet过滤可解决问题1;
对ADODataSet Updata 解决问题2;
但是是在客户端用TClientDataSet控件对数据库进行操作。
ClientDataSet.fieldByName('字段名').asInteger:=1;
..
..ClientDataSet.applyupdate(-1)
ClientDataSet.close;
这个标识不论出现什么异常,将数据提交到服务器端。
var
sql:string;
begin
sql:='select * from xiaoshoujilu where shijian between 2003-2-1 and 2003-4-30'; //错误的地方
dm.ADOQueryTJ.Close;
dm.ADOQueryTJ.SQL.Add(sql);
dm.ADOQueryTJ.Open;
end;
//如果sql:='select * from xiaoshoujilu where like %2003-3%'; 也错误
使用ADOQuery组件,ACCESS数据库。
var
sql:string;
begin
sql:='select * from xiaoshoujilu where shijian between '''2003-2-1''' and '''2003-4-30''' '; //错误的地方
dm.ADOQueryTJ.Close;
dm.ADOQueryTJ.SQL.Add(sql);
dm.ADOQueryTJ.Open;
end;
//如果sql:='select * from xiaoshoujilu where like '''%2003-3%''' '; 也错误
使用ADOQuery组件,ACCESS数据库。
//如果sql:='select * from xiaoshoujilu where like '''%2003-3%''' '; 也错误
使用ADOQuery组件,ACCESS数据库。在‘where’ 后应该加个字段名
var
sql,s1,s2:string;
begin
s1:='2003-2-1';
s2:='2003-4-30';
sql:='select * from xiaoshoujilu where shijian between '+''''+s1+''''+
' and '+''''+s2+''''; //错误的地方
dm.ADOQueryTJ.Close;
dm.ADOQueryTJ.SQL.Add(sql);
dm.ADOQueryTJ.Open;
end;
这样试一下,应该可以的.
下面是用SQL的例子:
ADODataset.Close;
ADODataset.CommandText:='select * from message where content like "%'+edit.Text+'%" and state=0 order by id asc';
ADODataset.Open;%是通配符,就相当于DOS中的那个*号的作用。
你在DBGIRD中设置好了数据源后,再运行上面的代码。就会自动的把查询的结果返回到DBGIRD中。下面是一个按了查询按钮以后的具体例子:
procedure TForm_find.BitBtn3Click(Sender: TObject);
begin
if edit.Text = '' then
begin
Application.MessageBox('查询关键字不能为空!','系统信息',MB_OK+MB_ICONERROR);
edit.SetFocus;
end
else
//---按内容查询模块---
begin
ADODataset.Close;
ADODataset.CommandText:='select * from message where content like "%'+edit.Text+'%" and state=0 order by id asc';
ADODataset.Open;
if ADODataset.RecordCount < 1 then
begin
Application.MessageBox('没有找到符合条件的记录!','系统信息',MB_OK+MB_ICONINFORMATION);
end
else
begin
//Application.MessageBox('查找成功!共找到ADODataset.RecordCount条短信。','系统信息',MB_OK+MB_ICONINFORMATION);
Application.MessageBox(pchar('查找成功!共找到'+IntToStr(ADODataset.RecordCount)+'条记录。'),'系统信息',MB_OK+MB_ICONINFORMATION);
//或者可以这样写:
//MessageBox(self.handle,pchar('查找成功!共找到'+IntToStr(ADODataset.RecordCount)+'条记录。'),'系统信息',MB_OK+MB_ICONINFORMATION);
edit.Text:='';
edit.SetFocus;
end;
end;
end;
ADOTable1.Filter:='用户名='''+ Edit1.Text+'''';
ADOTable1.Filtered:=true;
ADOTable1.Open;当然,你也可以使用ADOQuery控件来实现。只需要把ADOTable1改为ADOQuery即可。