使用的ado控件
//判断是否存在ZZF_price.bcp文件
bool s1=FileExists("\\ZZF_price.bcp");
//bool s2=FileExists("train_dir.bcp");
if( !s1 )
{
Application->MessageBoxA("不存在相应的文件!请检测你的设备!","提示框",MB_OK);
Application->Terminate();
return;
}
else
{
StatusBar1->Panels->Items[0]->Text="开始连接...";
Refresh();
//读取ini文件
TIniFile *readFile = new TIniFile(ExtractFilePath(Application->ExeName)+"config.ini");
AnsiString uName = readFile->ReadString("database","userName","").Trim();
AnsiString uPwd = readFile->ReadString("database","userPwd","").Trim();
AnsiString server = readFile->ReadString("database","dataSource","").Trim();
//连接数据库
try{
Form1->ADOConnection1->Connected=false;
Form1->ADOConnection1->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID="+uName+"; Password="+uPwd+";Initial Catalog=master;Data Source="+server;
Form1->ADOConnection1->Connected=true;
}catch(...)
{
ShowMessage("数据库连接失败或缺失ini文件,请开启数据库或完善ini文件!");
StatusBar1->Panels->Items[0]->Text="数据库连接失败或缺失ini文件,请开启数据库或完善ini文件!";
Refresh();
return;
}
StatusBar1->Panels->Items[0]->Text="开始导入数据...";
Refresh();
if(Form1->ADOConnection1->InTransaction)
Form1->ADOConnection1->BeginTrans();
//Refresh();
StatusBar1->Panels->Items[0]->Text="正在导入数据...";
Refresh();
try{
//创建临时表temp_price
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("create table zzcz.dbo.temp_price(intime varchar(14),train_no char(12),train_code char(8),station_train_code char(8),station_no char(2),station_name char(10),distance smallint,route_name char(2),arrive_time char(5),start_time char(5),seat_type_code char(2),seat_type_name char(12),ticket_price money)");
ADOQuery1->ExecSQL();
/*导入从ftp上get的数据文件*/
// try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("EXEC master..xp_cmdshell 'bcp zzcz.dbo.temp_price in "+GetCurrentDir().SubString(1,3)+"ZZF_price.bcp -c -T'");
ADOQuery1->ExecSQL();
//}catch(...)
//{
// ShowMessage("Error");
//}
//获取系统时间
//FormatDateTime("yyyy-mm-dd hh:mm:ss",TDateTime(Now()));
//TDateTime dateNow=FormatDateTime("yyyy-mm-dd hh:mm:ss",TDateTime(Now()));
String datetime=Now().FormatString("yyyy-MM-dd hh:mm:ss");
String year,month,day,hour,miunte,second;
year=datetime.SubString(1,4);
month=datetime.SubString(6,2);
day=datetime.SubString(9,2);
hour=datetime.SubString(12,2);
miunte=datetime.SubString(15,2);
second=datetime.SubString(18,2);
String intimenow = year+month+day+hour+miunte+second;
//把临时表temp_price中的intime更改为当前插入时间
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("update zzcz.dbo.temp_price set intime="+intimenow);
ADOQuery1->ExecSQL();
//把临时表temp_price中的数据插入到sto_price表中
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("insert into zzcz.dbo.sto_price select * from zzcz.dbo.temp_price");
ADOQuery1->ExecSQL();
/*创建和cg30_p_p_price相似结构的表*/
//try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("create table zzcz.dbo.zz_price(start_date char(8) not null default '',stop_date char(8) not null default'',train_no char(12) not null,from_station_name char(10) not null default'郑州',to_station_name char(10) not null,ticket_type int not null default 0,belong_line tinyint not null default 0,seat_type_code char(1) not null default'',bed_level char(1) not null default'',price smallint not null,service_cost smallint not null default 0,origin char(2) not null default'',replicate_range int)");
ADOQuery1->ExecSQL();
//}catch(...)
//{
//ShowMessage("Error");
//}
/*插入相应的数据*/
//try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("insert into zzcz.dbo.zz_price(train_no,to_station_name,seat_type_code,price) select train_no,station_name,convert(char(1),seat_type_code),convert(smallint,ticket_price) from zzcz.dbo.sto_price where intime="+intimenow);
ADOQuery1->ExecSQL();
//}catch(...)
//{
// ShowMessage("Error");
// }
//删除cg30_p_price表中from_station_name为郑州的数据
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("delete from zzcz.dbo.cg30_p_p_price where from_station_name='郑州'");
ADOQuery1->ExecSQL();
/*把zz_price中的数据插入到cg30_p_p_price中去*/
// try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("insert into zzcz.dbo.cg30_p_p_price select * from zzcz.dbo.zz_price");
ADOQuery1->ExecSQL();
//}catch(...)
//{
//ShowMessage("Error");
//}
/*删除临时表sto_price*/
//try{
/* ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("drop table zzcz.dbo.sto_price");
ADOQuery1->ExecSQL(); */
//}catch(...)
//{
//ShowMessage("Error");
//}
//删除临时表temp_price
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("drop table zzcz.dbo.temp_price");
ADOQuery1->ExecSQL();
/*删除临时表zz_price*/
//try{
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("drop table zzcz.dbo.zz_price");
ADOQuery1->ExecSQL();
//删除表sto_price中的旧数据
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("delete from zzcz.dbo.sto_price where datediff(ss,substring(intime,1,4)+'-'+substring(intime,5,2)+'-'+substring(intime,7,2)+' '+substring(intime,9,2)+':'+substring(intime,11,2)+':'+substring(intime,13,2),'"+datetime+"')>0");
ADOQuery1->ExecSQL();
Application->MessageBox("导入数据完成","提示框",MB_OK);
//ShowMessage("导入数据完成");
//Refresh();
StatusBar1->Panels->Items[0]->Text="导入数据完成";
Refresh();
//}catch(...)
//{
// ShowMessage("Error");
//}
//if(Form1->ADOConnection1->InTransaction)
//Form1->ADOConnection1->CommitTrans();
}catch(Exception &e)
{
Form1->ADOConnection1->RollbackTrans();
ShowMessage("执行时出错");
Application->Terminate();
return;
}
}
FTP下载文件代码使用的idftp控件 需要对host,user,password,transfertype进行相应的修改
try
{
StatusBar1->Panels->Items[0]->Text="开始连接...";
Refresh();
Form1->IdFTP1->Connect();
if(Form1->IdFTP1->Connected()) {
Refresh();
StatusBar1->Panels->Items[0]->Text="连接成功,正在读取数据...";
Refresh();
Form1->IdFTP1->Get("/backup/ZZF_train/stop_time.bcp","/stop_time.bcp",true); //这是取linux系统根目录下的数据需要在前面加'/'
Form1->IdFTP1->Get("/backup/ZZF_train/train_dir.bcp","/train_dir.bcp",true); //这是取linux系统根目录下的数据需要在前面加'/'
Form1->IdFTP1->Get("ZZF/ZZF_price.bcp","/ZZF_price.bcp",true);//这不是根目录下,不需要加'/'
Application->MessageBox("数据读取完成","提示框",MB_OK);
StatusBar1->Panels->Items[0]->Text="读取数据完成";
Refresh();
Form1->IdFTP1->Disconnect();
}
}
catch(...)
{
ShowMessage("连接FTP失败!请检测连接,确保连接成功后,再次运行程序!");
StatusBar1->Panels->Items[0]->Text="连接FTP失败!请检测连接,确保连接成功后,再次运行程序!";
Refresh();
}
主要是遇到bcp的难题解决,以及在表之间数据的利用。
在sql语句中进行日期比较时,使用了daydiff函数,但这个函数要求数据的格式必须为yyyy-mm-dd hh:mm:ss,否则报错。同时在c++builder中使用now()函数如果在;上午10点前显示的格式为2011-07-21 9:50:12,所以需要进行时间格式转换,有两种方法,一:FormatDateTime("yyyy-mm-dd hh:mm:ss",TDateTime(Now()));二:Now().FormatString("yyyy-MM-dd hh:mm:ss"); ftp连接linux时,根目录前不需要加‘/’.还有ini文件的使用,需要加#include "inifiles.hpp"。