写一段用delphi 开发安卓app的代码,连sqlserver 登记个人表单
时间: 2023-09-27 21:05:22 浏览: 160
### 回答1:
您可以使用以下 Delphi 代码来连接 SQL Server 并登记个人表单:// 建立数据库连接var
SQLConnection: TSQLConnection;
begin
SQLConnection := TSQLConnection.Create(nil);
try
SQLConnection.DriverName := 'SQL Server';
SQLConnection.Params.Add('Database=YourDatabaseName');
SQLConnection.Params.Add('User_Name=YourUserName');
SQLConnection.Params.Add('Password=YourPassword');
SQLConnection.Params.Add('Server=YourServerName');
SQLConnection.Params.Add('Port=1433');
SQLConnection.Open; // 执行 SQL 语句
SQLConnection.ExecuteDirect('INSERT INTO personal_table (name, age, address) VALUES (''John'', 25, ''London'');'); finally
SQLConnection.Free;
end;
end;
### 回答2:
在Delphi中开发安卓应用程序时,可以使用FireDAC组件连接SQL Server并注册个人表单。以下是一段简单的代码示例:
首先,将一个TButton组件和一个TListView组件放置在主窗体上。
```delphi
unit MainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base, FMX.StdCtrls,
FireDAC.Phys.ODBCDef, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Phys, FireDAC.Phys.ODBCBase, FireDAC.UI.Intf, FireDAC.FMXUI.Wait, FireDAC.Comp.UI,
FireDAC.Comp.Client, FMX.ListView, Data.DB, FireDAC.Stan.Intf, FireDAC.DatS, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.Comp.DataSet;
type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
FDConnection1: TFDConnection;
FDQuery1: TFDQuery;
FDPhysODBCDriverLink1: TFDPhysODBCDriverLink;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
FDGUIxWaitCursor2: TFDGUIxWaitCursor;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
var
FirstName, LastName: String;
SQLQuery: String;
begin
// 获取用户输入的名字
FirstName := InputBox('Enter your first name', 'First Name:', '');
LastName := InputBox('Enter your last name', 'Last Name:', '');
try
// 连接到SQL Server数据库
FDConnection1.Params.Clear;
FDConnection1.DriverName := 'ODBC';
FDConnection1.Params.Add('DriverID=SQL Server');
FDConnection1.Params.Add('Server=YourServerName');
FDConnection1.Params.Add('Database=YourDatabaseName');
FDConnection1.Params.Add('User_Name=YourUserName');
FDConnection1.Params.Add('Password=YourPassword');
FDConnection1.Connected := True;
// 构建插入语句并执行
SQLQuery := 'INSERT INTO PersonalInfo(FirstName, LastName) VALUES (:FirstName, :LastName)';
FDQuery1.SQL.Text := SQLQuery;
FDQuery1.Params.ParamByName('FirstName').Value := FirstName;
FDQuery1.Params.ParamByName('LastName').Value := LastName;
FDQuery1.ExecSQL;
ShowMessage('Registration successful!');
except
on E: Exception do
ShowMessage('Error: ' + E.Message);
end;
end;
end.
```
以上代码通过FDConnection1连接到SQL Server数据库,并通过FDQuery1将用户的FirstName和LastName插入到PersonalInfo表中。代码通过InputBox获取用户输入,然后构建并执行SQL插入语句。在插入过程中,如果出现异常则显示错误消息,插入成功后显示注册成功提示。
请注意,根据您的实际情况,您需要替换代码中的服务器名称、数据库名称、用户名和密码,以及PersonalInfo表的字段名称。另外,还需要确保您的Delphi中FireDAC组件和ODBC驱动程序正确安装和配置。
阅读全文