delphi12 sqlserver 客户-服务简单连接设置
服务端组件:TSQLConnection,TSQLDataSet,TSQLQuery,TDataSetProvider
客户端组件:TSQLConnection,TDSProviderConnection,TClientDataSet,TDataSource
连接
服务端
TSQLConnection
添加组件SQLConnection1,属性Drive下拉框选择 MSSQL
在属性Params中,点击右侧的三个点,在弹出页面中修改数据库连接设置,修改红框内的内容,127.0.0.1默认为本地ip
HostName:数据库所在ip
DataBasr:连接数据库的库名
User_Name:连接数据库的用户名
Password:用户密码
设置完后,将Connected设为true判断是否能够连接数据库,如果报错请检查信息是否有错误
TSQLDataSet
添加组件SQLDataSet1,将属性SQLConnection下拉框选择刚刚添加的组件名称SQLConnection1
TSQLQuery
添加SQLQuery1组件,设置SQLConnection属性下拉框选择为SQLConnection1
将SQL右侧三个点点开,在里面写入sqlserver语句
如果语句中存在不确定的参数,可以在函数中写语句,如下
1 查看本单元的类名,TServerMethods1
2 添加函数到代码中,函数名前面加单元类名.,语句中的参数 aid 前需要加冒号,然后在语句后加Params.ParamByName('aid').asString := id,其中 aid 是语句中的参数,id是函数中传入的参数,语句为 select 则使用 open 打开,如果是修改语句 update ,则用 ExecSQL 打开。
如果函数需要返回参数,可以在函数的括号内加var idCode: string,在open打开后加语句Params.ParamByName('aid').asString := id;
procedure TServerMethods1.getbyID(id:string);
beginwith SQLQuery1 dobeginClose;SQL.Text := 'select * from DataBase1 where id = :aid';Params.ParamByName('aid').asString := id;tryopen;excepton E: Exception dobeginshowmessage( E.Message);end;end;end;
end;
//根据条件查出最新的idCard
procedure TServerMethods1.getbyID(var idcard:string;id:string);
beginwith SQLQuery1 dobeginClose;SQL.Text := 'select * from DataBase1 where id = :aid order by id desc';Params.ParamByName('aid').asString := id;tryopen;firstidcard:= FieldByname('idcard').asStringexcepton E: Exception dobeginshowmessage( E.Message);end;end;end;
end;
//根据条件修改对应值
procedure TServerMethods1.getbyID(id,idcard:string);
beginwith SQLQuery1 dobeginClose;SQL.Text := 'update DataBase1 set idcard=:aidcard where id = :aid';Params.ParamByName('aid').asString := id;Params.ParamByName('aidcard').asString := idcard;tryExecSQL;excepton E: Exception dobeginshowmessage( E.Message);end;end;end;
end;
TDataSetProvider
如果需要将select出的结果显示到客户端或是供客户端调用,即客户端需要Clientdataset组件接收数据,则服务端添加组件DataSetProvider1,如果只是修改并不需要将结果显示,则不需要这个组件
使用方法:添加组件DataSetProvider1到页面上,dataset属性设为刚刚添加的SQLQuery1即可
客户端
TSQLConnection
放置组件SQLConnection1(同一个ip放置一个即可),设置属性 Driver 为 Datasnap,将LoginPromt改为false(即每次不需要输入账号密码直接按照上次连接记录连接),然后将Params的HostName设为服务端的ip地址,确认后,将Connected连接,如果不报错则没问题,报错则检查以下输入是否有其他问题
TDSProviderConnection
放置组件DSProviderConnection1(几个TSQLConnection组件放几个,可以由多个TClientDataSet组件连接),设置属性SQLConnection下拉框选择对应IP的SQLConnection1,Connected连接True,成功则和SQLConnection1连接成功
TClientDataSet
添加组件ClientDataSet1,设置属性RemoteServer为DSProviderConnection1,并且ProvideName下拉框选择服务端对应语句的DataSetProvider1-如果没有出现可以查看DataSetProvider1和SQLConnection1的Connected师傅为true,如果已经为true就将SQLConnection1的Connected重新变为false-> true
TDataSource
如果对于ClientDataSet1中的数据只需要使用不需要显示到表(unidbgrid等组件)中,则不需要这个组件
放置组件DataSource1,将Dataset下拉框选择ClientDataSet1即可
使用
客户端中右键SQLConnection1选择弹出的第三个选项generate Datasnap client classes
将新建出来的单元保存,自己设置个名字如UServer
如果报错:[dcc32 Fatal Error] UServer.pas(715): F2069 Line too long (more than 1023 characters)则可以按快捷键(ctrl+D或者ctrl+W)自动整理代码
使用中,将UServer引入到需要使用的页面代码中
uses Userver
如果在unibutton3的点击事件中需要使用三层代码时,代码如下:
procedure TfQuaLookBoardNew.UniButton3Click(Sender: TObject);
varAServer: TServerMethods1Client; //声明参数id, idcard: string;
beginAServer := TServerMethods1Client.Create(UniMainModule.SQLConnection1.DBXConnection);id := '0';tryAServer.getbyID(id); //调用三层函数ClientDataSet1.Active := false;
//三层函数到客户端的路径->SQLQuery1->DataSetProvider1->ClientDataSet1ClientDataSet1.Active := true;
//其他函数代码
// ClientDataSet1.first;
// idcard := ClientDataSet1.FieldByName('idcard').AsString;excepton E: Exception dobeginShowMessage('获取信息错误: ' + E.Message);end;end;
end;