主题:请教问题,从socket到mssql数据库
请教大家一个问题,目前代码是捕获socket的包后发送到execl里面的,请问如何将其修改为保存到mssql数据库当中去呢?
代码如下
//---------------------------------------------------------------------------
#include <Unit1.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TFmMain *FmMain;
//---------------------------------------------------------------------------
__fastcall TFmMain::TFmMain(TComponent* Owner)
: TForm(Owner)
{
recordNum=1;
ExcelHandle=Variant::CreateObject("Excel.Application");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::listenButtonClick(TObject *Sender)
{
ServerSocket1->Port = StrToInt(Edit2->Text);
ServerSocket1->Active = true;
listenButton->Enabled=false;
disConnectButton->Enabled=true;
ServerSocket1->ThreadCacheSize=1;
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " has been accepted");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " connected");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " disconnected");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Listening...");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
//TFileStream类
int length;
length = Socket->ReceiveLength();
if ( length<= 0 )
return;
char *buf = new char[length + 1];
memset(buf, 0, length+ 1);
int readLen;
readLen =Socket->ReceiveBuf(buf, length);
RevDataInfo(buf,length,readLen);
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::disConnectButtonClick(TObject *Sender)
{
ServerSocket1->Active = false;
listenButton->Enabled= true;
disConnectButton->Enabled=false;
}
//---------------------------------------------------------------------------
bool TFmMain::WriteFile(String str)
{
String strFile =ExtractFilePath(Application->ExeName)+"log\\"+Date()+".xls";
String strFile2=ExtractFilePath(Application->ExeName)+"log\\"+Date()+".txt";
if(str.Length()<40) //time
return false;
int pos;
String tempStr=str;
TList *pList=new TList ;
pos= tempStr.Pos(" ");
char* temp;
while(tempStr.Length()>0)
{
if(pos<=0)
pos=tempStr.Length()+1;
temp=new char[100];
memset(temp,0,100);
temp[0]='\'';
memcpy(temp+1,tempStr.c_str(),pos-1);
temp[pos]=0;
//temp =(tempStr.SubString(1,pos-1));
pList->Add(temp);
int i=pos;
while(tempStr.SubString(i,1)==" ")
i++;
tempStr= (tempStr.SubString(i,tempStr.Length()));
pos=tempStr.Pos(" ");
}
try
{
if(FileExists(strFile)==0)
{
recordNum=1;
}
OutExcel(strFile,pList,recordNum);
delete pList;
recordNum++;
}catch(...)
{
return false;
}
TFileStream *file;
str+="\r\n";
try{
file= new TFileStream(strFile2.c_str(),fmOpenReadWrite);
}catch(...)
{
file= new TFileStream(strFile2.c_str(),fmCreate);
}
file->Seek(0,soFromEnd);
file->Write(str.c_str(),str.Length());
delete file;
return true;
}
//revLen接受到的字符串长度 readLen 读取的字符串长度
void TFmMain::RevDataInfo(String str,int revLen,int readLen)
{
AnsiString info;
info = "length:"+IntToStr(readLen)+"/r/n"; // NMUDP1->ReadBuffer(buf, len, len);
info += str;
//最多保持200条记录
if(ListBox1->Count>200)
ListBox1->Items->Delete(0);
ListBox1->Items->Add(info);
int temp;
temp=str.Pos("\r\n");
while(temp>0)
{
if(revLen ==readLen)//&&str.SubString(readLen-2,2)=="\r\n"
WriteFile(str.SubString(1,temp-1));
str=str.SubString(temp+2,str.Length());
temp=str.Pos("\r\n");
}
}
bool TFmMain::OutExcel(String strXlsFile,TList * list,int beginRow)//
{
Variant vExcelApp,vSheet;
HWND hPrevApp =NULL;
//创建了一个EXCEL文件
try
{
hPrevApp= FindWindow(NULL,"Microsoft Excel");
if(!hPrevApp)
{
vExcelApp=Variant::CreateObject("Excel.Application");
}
else
{
vExcelApp=Variant::GetActiveObject("Excel.Application");
}
}catch(...)
{
ShowMessage("打开Excel出错,请确认你已经正确安装了MS Office!");
return false;
}
//打开一个SHEET
vExcelApp.OlePropertySet("Visible",false);
//vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("ADD");
try{
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", strXlsFile.c_str());
}catch(...)
{
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("ADD");
vExcelApp.OlePropertyGet("ActiveWorkBook").OleProcedure("SaveAs",strXlsFile.c_str());
//vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", strXlsFile.c_str());
}
try
{
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet");
}catch(...)
{
return false;
}
//设置属性
// int nRowcount = 1;
int nColcount = list->Count;
int i,state;
//导入数据到xls文件里
char* strValue=new char[50];
for(int j=0;j<nColcount;j++)
{
strValue=((char*)(list->Items[j]));
vSheet.OlePropertyGet("Cells",beginRow,j+1).OlePropertySet("Value",strValue);
}
//保存
vExcelApp.OlePropertyGet("ActiveWorkbook").OleProcedure("Save");
//vExcelApp.OleFunction("Save");
vExcelApp.OlePropertyGet("workbooks").OleFunction("Close");
// vExcelApp.OleFunction("Close");
// vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
return true;
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
try{
ExcelHandle.OleFunction("Quit");
}catch(...)
{
return;
}
}
//---------------------------------------------------------------------------
代码如下
//---------------------------------------------------------------------------
#include <Unit1.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TFmMain *FmMain;
//---------------------------------------------------------------------------
__fastcall TFmMain::TFmMain(TComponent* Owner)
: TForm(Owner)
{
recordNum=1;
ExcelHandle=Variant::CreateObject("Excel.Application");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::listenButtonClick(TObject *Sender)
{
ServerSocket1->Port = StrToInt(Edit2->Text);
ServerSocket1->Active = true;
listenButton->Enabled=false;
disConnectButton->Enabled=true;
ServerSocket1->ThreadCacheSize=1;
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " has been accepted");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " connected");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Client " + Socket->RemoteAddress + ":" + IntToStr(Socket->RemotePort) + " disconnected");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1Listen(TObject *Sender,
TCustomWinSocket *Socket)
{
ListBox1->Items->Add("Listening...");
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
//TFileStream类
int length;
length = Socket->ReceiveLength();
if ( length<= 0 )
return;
char *buf = new char[length + 1];
memset(buf, 0, length+ 1);
int readLen;
readLen =Socket->ReceiveBuf(buf, length);
RevDataInfo(buf,length,readLen);
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::disConnectButtonClick(TObject *Sender)
{
ServerSocket1->Active = false;
listenButton->Enabled= true;
disConnectButton->Enabled=false;
}
//---------------------------------------------------------------------------
bool TFmMain::WriteFile(String str)
{
String strFile =ExtractFilePath(Application->ExeName)+"log\\"+Date()+".xls";
String strFile2=ExtractFilePath(Application->ExeName)+"log\\"+Date()+".txt";
if(str.Length()<40) //time
return false;
int pos;
String tempStr=str;
TList *pList=new TList ;
pos= tempStr.Pos(" ");
char* temp;
while(tempStr.Length()>0)
{
if(pos<=0)
pos=tempStr.Length()+1;
temp=new char[100];
memset(temp,0,100);
temp[0]='\'';
memcpy(temp+1,tempStr.c_str(),pos-1);
temp[pos]=0;
//temp =(tempStr.SubString(1,pos-1));
pList->Add(temp);
int i=pos;
while(tempStr.SubString(i,1)==" ")
i++;
tempStr= (tempStr.SubString(i,tempStr.Length()));
pos=tempStr.Pos(" ");
}
try
{
if(FileExists(strFile)==0)
{
recordNum=1;
}
OutExcel(strFile,pList,recordNum);
delete pList;
recordNum++;
}catch(...)
{
return false;
}
TFileStream *file;
str+="\r\n";
try{
file= new TFileStream(strFile2.c_str(),fmOpenReadWrite);
}catch(...)
{
file= new TFileStream(strFile2.c_str(),fmCreate);
}
file->Seek(0,soFromEnd);
file->Write(str.c_str(),str.Length());
delete file;
return true;
}
//revLen接受到的字符串长度 readLen 读取的字符串长度
void TFmMain::RevDataInfo(String str,int revLen,int readLen)
{
AnsiString info;
info = "length:"+IntToStr(readLen)+"/r/n"; // NMUDP1->ReadBuffer(buf, len, len);
info += str;
//最多保持200条记录
if(ListBox1->Count>200)
ListBox1->Items->Delete(0);
ListBox1->Items->Add(info);
int temp;
temp=str.Pos("\r\n");
while(temp>0)
{
if(revLen ==readLen)//&&str.SubString(readLen-2,2)=="\r\n"
WriteFile(str.SubString(1,temp-1));
str=str.SubString(temp+2,str.Length());
temp=str.Pos("\r\n");
}
}
bool TFmMain::OutExcel(String strXlsFile,TList * list,int beginRow)//
{
Variant vExcelApp,vSheet;
HWND hPrevApp =NULL;
//创建了一个EXCEL文件
try
{
hPrevApp= FindWindow(NULL,"Microsoft Excel");
if(!hPrevApp)
{
vExcelApp=Variant::CreateObject("Excel.Application");
}
else
{
vExcelApp=Variant::GetActiveObject("Excel.Application");
}
}catch(...)
{
ShowMessage("打开Excel出错,请确认你已经正确安装了MS Office!");
return false;
}
//打开一个SHEET
vExcelApp.OlePropertySet("Visible",false);
//vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("ADD");
try{
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", strXlsFile.c_str());
}catch(...)
{
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("ADD");
vExcelApp.OlePropertyGet("ActiveWorkBook").OleProcedure("SaveAs",strXlsFile.c_str());
//vExcelApp.OlePropertyGet("Workbooks").OleFunction("Open", strXlsFile.c_str());
}
try
{
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet");
}catch(...)
{
return false;
}
//设置属性
// int nRowcount = 1;
int nColcount = list->Count;
int i,state;
//导入数据到xls文件里
char* strValue=new char[50];
for(int j=0;j<nColcount;j++)
{
strValue=((char*)(list->Items[j]));
vSheet.OlePropertyGet("Cells",beginRow,j+1).OlePropertySet("Value",strValue);
}
//保存
vExcelApp.OlePropertyGet("ActiveWorkbook").OleProcedure("Save");
//vExcelApp.OleFunction("Save");
vExcelApp.OlePropertyGet("workbooks").OleFunction("Close");
// vExcelApp.OleFunction("Close");
// vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
return true;
}
//---------------------------------------------------------------------------
void __fastcall TFmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
try{
ExcelHandle.OleFunction("Quit");
}catch(...)
{
return;
}
}
//---------------------------------------------------------------------------