Dev Express 中的 dxDBGrid/cxGrid 均提供了将表格中 数据 导出 到 M$ Excel 等中的方法,但大多时候,却需将 数据 导出 至 M$ Access 中... 于是便有了本文。 uses ComObj, Gauges, ShellAPI; const ExportTabName_MDB = '营销 数据 '; MDBStr = 'Provider=
Dev Express 中的 dxDBGrid/cxGrid 均提供了将表格中数据导出到 M$ Excel 等中的方法,但大多时候,却需将数据导出至 M$ Access 中... ADelphiCoder
于是便有了本文。
uses
ComObj, Gauges, ShellAPI;
const
ExportTabName_MDB = '营销数据';
MDBStr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s';
var
ExportName: string;
ExportColumnLst: TStringList; //列名;列类型(长度)
begin
ExportName:= '导出结果.MDB'; //use a SaveDialog to select the save name here
ExportColumnLst:= TStringList.Create;
//(示例)导出列列表,注意 格式
ExportColumnLst.Add('Contact;联系人 varchar(30)');
ExportColumnLst.Add('Gender;性别 varchar(2)');
ExportColumnLst.Add('Address;地址 varchar(100)');
ExportColumnLst.Add('PostCode;邮编 varchar(6)');
try
ExportToMDB(ExportName, ExportColumnLst);
finally
FreeAndNil(ExportColumnLst);
end;
end;
procedure ExportToMDB(ExportMDBName: string; ExportColumnLst);
function CreateMDB(MDBFileName: string): Boolean;
var
vMDB: Variant;
begin
Result:= False;
vMDB:= CreateOleObject('ADOX.Catalog');
vMDB.Create(Format(MDBStr, [MDBFileName]));
vMDB:= UnAssigned;
Result:= True;
end;
function CreateTab(MDBAndTabName: string; ExportColumnLst: TStringList;
aqy_ExecSQL: TADOQuery): Boolean;
var
i: Integer;
StrTmp: string;
SQLTxt: string;
MDBName: string;
TabName: string;
begin
Result:= False;
SQLTxt:= '';
for i:= 0 to ExportColumnLst.Count - 1 do
begin
StrTmp:= ExportColumnLst.Strings
if SQLTxt = '' then
SQLTxt:= Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp));
else
SQLTxt:= SQLTxt + ',' +
Copy(StrTmp, Pos(';', StrTmp) + 1, Length(StrTmp));
end;
MDBName:= Copy(MDBAndTabName, 1, Pos(';', MDBAndTabName) - 1);
TabName:= Copy(
MDBAndTabName,
Pos(';', MDBAndTabName) + 1,
Length(MDBAndTabName)
);
with aqy_ExecSQL do
try
Close;
ConnectionString:=
'Provider=MSDataShape.1;Data Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=' + MDBName + ';Persist Security Info=false';
SQL.Text:=
'create table ' + TabName +
'(' +
SQLTxt +
')';
try
ExecSQL;
Close;
except
on E: Exception do
begin
MessageBox(
Handle,
PChar('创建表失败!' + #13 + '失败原因:' + E.Message),
'错误',
MB_OK + MB_ICONERROR
);
Close;
Exit;
end;
end;
finally
//Free;
end;
Result:= True;
end;
var
aqy_ExecSQL: TADOQuery;
SQLTxt: string;
i: Integer;
StrTmp: string;
ExportColumn: string;
ExportColumnParam: string;
ExportParamLst: TStringList;
GgTip: TGauge;
CurrRec: Integer;
begin
if CreateMDB(ExportMDBName) then
begin
aqy_ExecSQL:= TADOQuery.Create(Self);
try
if CreateTab(
ExportMDBName + ';' + ExportTabName_MDB,
ExportColumnLst,
aqy_ExecSQL
) then
begin
Screen.Cursor:= crHourGlass;
ExportColumn:= '';
ExportColumnParam:= '';
ExportParamLst:= TStringList.Create;
for i:= 0 to ExportColumnLst.Count - 1 do
begin
StrTmp:= ExportColumnLst.Strings
if ExportColumn = '' then
begin
ExportColumn:= Copy(StrTmp, 1, Pos(';', StrTmp) - 1);
ExportColumnParam:= ':' + ExportColumn;
ExportParamLst.Add(ExportColumn);
end
else
begin
ExportColumn:= ExportColumn + ',' +
Copy(StrTmp, 1, Pos(';', StrTmp) - 1);
ExportColumnParam:= ExportColumnParam + ',:' +
Copy(StrTmp, 1, Pos(';', StrTmp) - 1);
ExportParamLst.Add(Copy(StrTmp, 1, Pos(';', StrTmp) - 1));
end;
end;
SQLTxt:=
'select ' + ExportColumn + ' from TabName where ID=' +
aqy_Tmp1.FieldByName('ID').AsString;
try
with aqy_ExportData do //aqy_ExportData: TADOQuery;
begin
Close;
SQL.Text:= SQLTxt;
Open;
//pnl_ExportFile: TPanel;
GgTip:= TGauge.Create(pnl_ExportFile); //Gauge 进度提示
with GgTip do
begin
Parent:= pnl_ExportFile;
Left:= 0;
Height:= 21;
Width:= pnl_ExportFile.Width;
ForeColor:= clFuchsia;
MinValue:= 0;
MaxValue:= RecordCount;
Visible:= True;
Update;
end;
CurrRec:= 0;
while not Eof do
begin
Inc(CurrRec);
if CurrRec mod 20 = 0 then
begin
GgTip.Progress:= CurrRec;
Update;
Application.ProcessMessages;
end;
with aqy_ExecSQL do
begin
Close;
SQL.Text:=
'Insert Into ' + ExportTabName_MDB +
' Values(' + ExportColumnParam + ')';
for i:= 0 to ExportParamLst.Count - 1 do
Parameters.ParamByName(ExportParamLst.Strings
aqy_ExportData.FieldByName(
ExportParamLst.Strings
).AsString;
try
ExecSQL;
except
on E: Exception do
begin
Close;
GgTip.Visible:= False;
Update;
MessageBox(
Handle,
PChar('导出文件失败! ' + #13 + '失败原因:' +
E.Message + ' '
),
'错误',
MB_OK + MB_ICONERROR
);
Exit;
end;
end;
end; //End with
aqy_ExecSQL.Close;
Next;
end; //End while
Close; //aqy_ExportData
GgTip.Visible:= False;
if MessageBox(
Handle,
PChar('导出文件成功! ' + #13 +
'现在查看导出结果(' + ExportMDBName + '吗?'
),
'提示',
MB_YESNO + MB_ICONINFORMATION
) = IDYES then
begin
ShellExecute(0, 'Open', PChar(ExportMDBName), nil, nil, SW_SHOW);
end;
end;
except
on E: Exception do
begin
pnl_ExportFile.Caption:= '';
GgTip.Visible:= False;
Update;
MessageBox(
Handle,
PChar('导出文件过程中发生错误! ' + #13 +
'错误描述:' + E.Message + ' '
),
'导出失败',
MB_OK + MB_ICONERROR
);
end;
end;
end;
finally
FreeAndNil(aqy_ExecSQL);
FreeAndNil(ExportParamLst);
FreeAndNil(GgTip);
Screen.Cursor:= crDefault;
end;
end;
end;
OK,Done!

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

选择PostgreSQL而非MySQL的场景包括:1)需要复杂查询和高级SQL功能,2)要求严格的数据完整性和ACID遵从性,3)需要高级空间功能,4)处理大数据集时需要高性能。PostgreSQL在这些方面表现出色,适合需要复杂数据处理和高数据完整性的项目。

MySQL数据库的安全可以通过以下措施实现:1.用户权限管理:通过CREATEUSER和GRANT命令严格控制访问权限。2.加密传输:配置SSL/TLS确保数据传输安全。3.数据库备份和恢复:使用mysqldump或mysqlpump定期备份数据。4.高级安全策略:使用防火墙限制访问,并启用审计日志记录操作。5.性能优化与最佳实践:通过索引和查询优化以及定期维护兼顾安全和性能。

如何有效监控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。1.使用mysqladmin查看连接数。2.用SHOWGLOBALSTATUS查看查询数。3.PMM提供详细性能数据和图形化界面。4.MySQLEnterpriseMonitor提供丰富的监控功能和报警机制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显着差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高级安全性和良好集成性的企业级应用场景下,应选择SQLServer而不是MySQL。1)SQLServer提供企业级功能,如高可用性和高级安全性。2)它与微软生态系统如VisualStudio和PowerBI紧密集成。3)SQLServer在性能优化方面表现出色,支持内存优化表和列存储索引。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

记事本++7.3.1
好用且免费的代码编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),