我用的是VC和Oracle 9i,我通过测量得到一些数据下面是测量数据的代码
void CAllConnectDlg::OnPingall() 
{
// TODO: Add your control notification handler code here m_pinglist.DeleteAllItems();
m_pingaverlist.DeleteAllItems();
CNodeListDlg cnd;
int n=cnd.InitdmIPTab();
len=cnd.InitdmIPTab();
WSADATA wsaData;
WSAStartup(WINSOCK_VERSION,&wsaData); UpdateData(TRUE);
EnableControls(FALSE);
ptimes = m_pingspin.GetPos();
spacks = m_packspin.GetPos();
m_pingprogress.SetRange(0,m_pingspin.GetPos());
m_pingprogress.SetPos(0);
double sum=0;
CString ipstr;
for(int j=0;j<n;j++)
{
sum=0,averttall=0,ipstr="测量";
ipstr += cnd.m_dmIPtab[j];
m_pinglist.DeleteColumn(0);
m_pinglist.InsertColumn(0,ipstr,LVCFMT_CENTER,ColumnW);
for(int i=0;i<ptimes;i++)
{ str=AllPing(cnd.m_dmIPtab[j]);
m_pinglist.InsertItem(i,str);
_ltoa(TripTime,str,10);
if(TripTime<9999)
sum+=TripTime;
Sleep(1000);
UpdateData(FALSE);
Invalidate();
UpdateWindow();
m_pingprogress.StepIt();
SetDlgItemText(IDC_PING_STATIC,ipstr);
}
averttall=sum/ptimes;
avertt[j]=averttall;
starttime=CTime::GetCurrentTime();
timestr=starttime.Format("%Y年%m月%d日");
timestr.operator +=(starttime.Format("\t%H点%M分%S秒"));
curtime[j]=timestr.GetBuffer(timestr.GetLength());
IPstr[j]=cnd.m_dmIPtab[j];
m_averttall.Format("%s 's RTT is %.2f millseconds",cnd.m_dmIPtab[j],averttall);
m_pingaverlist.InsertItem(j,m_averttall);
UpdateData(FALSE);
SetDlgItemText(IDC_PING_STATIC,ipstr);
}
WSACleanup(); SetDlgItemText(IDC_PING_STATIC,"测量完成!");
EnableControls(TRUE);
}
这个代码运行正常,但是运行完它之后,再调用对数据库表操作就显示连接无法用于此操作。在此上下文中它可能已被关闭或无效。但是如果不运行上面的代码创建表操作就有效,创建表代码如下:
void CAllConnectDlg::OnCreate() 
{
// TODO: Add your control notification handler code here
try{
// CString strSql="create table Connectivity(IPAddr char(10), MeasureTime char(10), SendTimes int, SendPacks int, ConnectRtt int, Connectivity char(10))";
CString strSql="create table Connectivity(IPAddr varchar2(20), MeasureTime varchar2(30), SendTimes int, SendPacks int, ConnectRtt int, Connectivity varchar2(10))";
BSTR bstrSQL = strSql.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
m_pRecordset->Close();
}
catch (_com_error e) {
AfxMessageBox(e.Description());
}
}
插入数据代码如下:
void CAllConnectDlg::OnSave() 
{
// TODO: Add your control notification handler code here
try{
CString strSql="select * from Connectivity";
BSTR bstrSQL = strSql.AllocSysString();
m_pRecordset2->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
for(int i=0;i<len;i++)
{
//添加记录
m_pRecordset2->AddNew();
m_pRecordset2->Fields->GetItem("IPADDR")->Value=_bstr_t(IPstr[i]);
//修改字段的值的第一种方式
m_pRecordset2->Fields->GetItem("MEASURETIME")->Value=_bstr_t(curtime[i]);
//修改字段值的第二种方式
m_pRecordset2->Fields->GetItem(_variant_t("CONNECTRTT"))->PutValue((short)avertt[i]);
m_pRecordset2->Fields->GetItem("SENDTIMES")->Value=(short)m_pingspin.GetPos();
m_pRecordset2->Fields->GetItem("SENDPACKS")->Value=(short)m_packspin.GetPos();
if(averttall>0&&averttall<9999)
m_pRecordset2->Fields->GetItem("CONNECTIVITY")->Value=_bstr_t("connect!");
else
m_pRecordset2->Fields->GetItem("CONNECTIVITY")->Value=_bstr_t("disconnect!");
m_pRecordset2->MoveNext();
}
//更新数据库
m_pRecordset2->Update();
m_pRecordset2->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
插入的数据都是测量函数得到的。研究好几天了问了好多人都不知道!