客户端如何通过咏南中间件调用存储过程和数据分页查询和文件传输的演示
客戶端如何通過詠南中間件調用存儲過程和數據分頁查詢和文件傳輸的演示
演示使用MSSQL 2000的存儲過程,其它類型的數據庫的存儲過程的語法是有所不同的。
1)MSSQL2000通用數據分頁存儲過程,僅作演示用,在此不探討性能問題
ALTER proc [dbo].[sp_page]
@RecordCount int output, --查詢到的記錄總數,輸出參數
@QueryStr nvarchar(1000)='table1', --表名、視圖名、查詢語句
@PageSize int=20, --每頁的大小(行數)
@PageCurrent int=0, --要顯示的頁 從0開始
@FdShow nvarchar (2000)='*', --要顯示的字段列表
@IdentityStr nvarchar (100)='id', --主鍵
@WhereStr nvarchar (2000)='1=1',
@FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
as
set nocount on
declare
@sql nvarchar(2000)
if @WhereStr = '' begin
set @WhereStr = '1=1'
end
declare @tsql nvarchar(200)
set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
if @PageCurrent = 0 begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
end
else begin
if upper(@FdOrder) = 'DESC' begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
end
else begin
set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
end
end
--print @sql
execute(@sql)
2)客戶端調用代碼
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, DBClient, StdCtrls, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
btnQry: TButton;
DataSource1: TDataSource;
ClientDataSet1: TClientDataSet;
btnSave: TButton;
downFile: TButton;
uploadFile: TButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure btnQryClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure downFileClick(Sender: TObject);
procedure uploadFileClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FPageCurrent: Integer;
function qryPage(tableName, fieldList, keyField, whereStr, orderType: string; pageSize, pageCurrent: Integer): OleVariant;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
untDllMethods;
{$R *.dfm}
procedure TForm1.btnQryClick(Sender: TObject);
begin
ClientDataSet1.Data := untDllMethods.QuerySql('0', 'select c1,c2,字段3 as c3 from t1');
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
untDllMethods.SaveData('0', 't1', ClientDataSet1.Delta);
end;
procedure TForm1.downFileClick(Sender: TObject);
var
v: OleVariant;
LStream: TMemoryStream;
begin
v := untDllMethods.DownLoadFile2('down.txt');
if v = null then
Exit;
LStream := TMemoryStream.Create;
untDllMethods.VariantToStream(v, LStream);
LStream.SaveToFile('c:\down.txt');
LStream.Free;
ShowMessage('成功下載文件 c:\down.txt');
end;
procedure TForm1.uploadFileClick(Sender: TObject);
var
LStream: TFileStream;
v: OleVariant;
begin
if not FileExists('c:\down.txt') then
begin
ShowMessage('請準備一個要上傳的文件 c:\down.txt');
Exit;
end;
LStream := TFileStream.Create('c:\down.txt', fmOpenRead);
untDllMethods.StreamToVariant(LStream, v);
if untDllMethods.UploadFile2(v, 'd:\down.txt') then
ShowMessage('上傳文件成功')
else
ShowMessage('上傳文件失敗');
LStream.Free;
end;
function TForm1.qryPage(tableName, fieldList, keyField, whereStr, orderType: string; pageSize, pageCurrent: Integer): OleVariant;
var
p: TParams;
begin
p := TParams.Create(nil);
p.Clear;
p.CreateParam(ftInteger, '@RecordCount', ptOutput); // 查詢到的記錄總數,輸出參數
p.CreateParam(ftString, '@QueryStr', ptInput).AsString := tableName; // 表名、視圖名、查詢語句
p.CreateParam(ftInteger, '@PageSize', ptInput).AsInteger := pageSize; // 每頁的大小(行數)
p.CreateParam(ftInteger, '@PageCurrent', ptInput).AsInteger := PageCurrent; // 要顯示的頁 從0開始
p.CreateParam(ftString, '@FdShow', ptInput).AsString := fieldList; // 要顯示的字段列表
p.CreateParam(ftString, '@IdentityStr', ptInput).AsString := keyField; // 主鍵
p.CreateParam(ftString, '@WhereStr', ptInput).AsString := whereStr; // where條件
p.CreateParam(ftString, '@FdOrder', ptInput).AsString := orderType; // 排序 只能取desc或者asc
result := untDllMethods.spOpenOut('0', 'sp_page', PackageParams(p));
p.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
r: OleVariant;
p: TParams;
LRecordCount: Integer;
begin
FPageCurrent := 0;
r := Self.qryPage('t1', '*', 'c1', '1=1', 'asc', 10, FPageCurrent);
ClientDataSet1.Data := r[0];
p := tparams.Create(nil);
UnpackParams(r[1], p);
LRecordCount := p.ParamByName('@RecordCount').AsInteger;
p.Free;
Caption := '記錄總數:' + IntToStr(LRecordCount);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
r: OleVariant;
p: TParams;
LRecordCount: Integer;
begin
if FPageCurrent = 0 then
begin
ShowMessage('當前是第一頁');
Exit;
end;
Dec(FPageCurrent);
r := Self.qryPage('t1', '*', 'c1', '1=1', 'asc', 10, FPageCurrent);
ClientDataSet1.Data := r[0];
p := tparams.Create(nil);
UnpackParams(r[1], p);
LRecordCount := p.ParamByName('@RecordCount').AsInteger;
p.Free;
Caption := '記錄總數:' + IntToStr(LRecordCount);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
r: OleVariant;
p: TParams;
LRecordCount: Integer;
begin
inc(FPageCurrent);
r := Self.qryPage('t1', '*', 'c1', '1=1', 'asc', 10, FPageCurrent);
ClientDataSet1.Data := r[0];
p := tparams.Create(nil);
UnpackParams(r[1], p);
LRecordCount := p.ParamByName('@RecordCount').AsInteger;
p.Free;
Caption := '記錄總數:' + IntToStr(LRecordCount);
end;
end.
?
轉載于:https://www.cnblogs.com/hnxxcxg/p/5976465.html
總結
以上是生活随笔為你收集整理的客户端如何通过咏南中间件调用存储过程和数据分页查询和文件传输的演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EClipse开发NDK流程
- 下一篇: 2016 CCPC 杭州站 小结