读书人

哪位DX能帮小弟我把这个语句改一下原

发布时间: 2012-05-27 05:42:30 作者: rapoo

哪位DX能帮我把这个语句改一下,原来是连ORACLE的,现在想连SQL SERVER,万分感谢!!
unit Ut_dump;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids, DBGrids,ComObj,IniFiles;

type
TForm1 = class(TForm)
Query1: TQuery;
Database1: TDatabase;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
dbname,user,pass,dir,code,sqlstr,filename,date,time:string;
myinifile:Tinifile;
section,key:TStrings;
i,j:integer;
sheet:variant;
excel:variant;
l,k:integer;
begin
myinifile:=Tinifile.create('datadump_tx_ylm.ini');
dbname:=myinifile.Readstring('config','dbname','a');
user :=myinifile.Readstring('config','user','a');
pass :=myinifile.Readstring('config','pass','a');

Database1.Close;
Database1.Params.Clear;
Database1.AliasName:= dbname;
Database1.Params.Add('USER NAME='+user);
Database1.Params.Add('PASSWORD='+pass);
Database1.Open;

section := TStringList.Create;
myinifile.readsections(section);
for i := 0 to section.Count-1 do
begin
if (section[i]<>'config') and (section[i]<>'path') and (section[i]<>'etmscode') then
begin
//showmessage(section[i]);
key := TStringList.Create;
myinifile.readsection(section[i],key);
excel:=createoleobject('excel.application');
excel.workbooks.add;
for j:=0 to key.Count-1 do
begin
//showmessage(key[j]);
sqlstr:=myinifile.Readstring(section[i],key[j],'a');
//ShowMessage(sqlstr);
excel.Workbooks[1].WorkSheets[j+1].Name:= key[j];
with Query1 do
begin
close;
sql.Clear;
sql.Add(sqlstr);
open;
sheet:=excel.workbooks[1].worksheets[j+1];
sheet.name:=key[j];
disablecontrols;
for l:=1 to fields.Count do
if fields[l-1].visible then
begin
sheet.cells[1,l]:=fields[l-1].DisplayLabel; //列标题
//ShowMessage(fields[l-1].DisplayLabel);
end;
first;
k:=2;
while not eof do
begin
for l:=1 to fields.Count do
if fields[l-1].visible then
begin
sheet.cells[k,l]:=fields[l-1].DisplayText; //列内容
//Sheet.rows[1].font.color:=clblue;)

end;
next;
inc(k);
end;
enablecontrols;

end;
end;

dir:=myinifile.Readstring('path','dir','a');
code:= myinifile.Readstring('etmscode','code','a');
filename:= dir+'\';
if section[i]= '销售' then filename:= filename+'SM_'+code+'_' else
begin
if section[i]='采购' then filename:= filename+'PM_'+code+'_' else
begin
if section[i]='库存' then filename:= filename+'IM_'+code+'_';
end;
end;
DateTimeToString(time,'yyyymmdd',now()-1);


filename:= filename+'_'+time+'.xls';
//showmessage(filename);
excel.workbooks[1].Saveas(filename);
excel.workbooks[1].close;
excel.quit;
key.Clear;
end;
end;

section.Clear;
myinifile.Destroy;
Form1.Free;
end;

end.

[解决办法]
我看了上述代码,我觉得代码本身并没有需要改动的地方,你需要注意如下方面
1:你使用的是BDE控件,当你切换数据库连接的时候,需要修改Alias。当前的Alias你是通过INI文件读取出写入到dbname变量的,如果你需要切换数据库,可以在你的BDE文件中重新配置一个新的Alias,将Type选择正确的SQL Driver。

这步,在你从控制面板中打开BDE administrator,在左半窗口选择databases页,单击Databases节点右键选择New,在弹出窗体的下拉框中选择SQLServer的驱动,可以是MSSQL或SQL Server或SQL Native Client等,然后在工具栏点击Apply按钮。

进入ODBC管理器(BDE administrator菜单object-ODBC administrator),选择user DSN 或 System DSN页,点击Add,选择数据库驱动,注意,这里选择的驱动名要和你刚才在BDE中新建Alias时选择的驱动要相同,接下来配置你需要连接的服务器地址和数据库名,保存。

回到BDE administrator,你刚才新建的Alias,选中,在窗体的右半部分找到ODBC DSN行,选中单元格在下拉框中找到你刚才在ODBC administrator中新建的DSN,然后点击工具栏上的Apply按钮,保存关闭BDE。

2:打开你配置数据库连接信息的INI文件datadump_tx_ylm.ini,将[config]下的dbname=换成你刚才在BDE中新建的Alias的名字,user pass 换成SQL server的用户名和密码

3:将SQL语句换成符合SQL Server的语法

读书人网 >.NET

热点推荐