大量数据插入SQL数据库问题,急急急!!!!!!
写了一个如下函数进行将数据插入到SQL数据库中:
bool UpdateToSQL(SQLInfo &info,char Item[][50],int nCount)
{
CString strSQL;
CString strItem,strValue,strTemp,SQLItem;
try
{
strItem.Format("PC_Name,SN,Result,Fail_Item,Fail_Value,End_Time,Test_Time");
//
CString str;
sprintf((LPSTR)(LPCSTR)strTemp,Item[6]);
if(strTemp=="")
str="0";
else
{
int n;
n=atoi(strTemp);
str.Format("%d",n);
}
/* sprintf((LPSTR)(LPCSTR)strTemp,Item[7]);
if(strTemp=="")
sprintf(Item[7],"N");*/
strValue.Format("'%s','%s','%s','%s','%s','%s','%s'",Item[0],Item[1],Item[2],Item[3],
Item[4],Item[5],str);
//
for(int i=8;i<=nCount;i++)
{
SQLItem.Format("%d",i-7);
if (SQLItem.GetLength()==1)
SQLItem="R00"+SQLItem;
else if (SQLItem.GetLength()==2)
SQLItem="R0"+SQLItem;
else
SQLItem="R"+SQLItem;
strItem=strItem+","+SQLItem;
strValue=strValue+","+"'"+Item[i]+"'";
}
//
strSQL.Format("insert into %s with (tablock) (%s) values (%s)",info.Tablename,strItem,strValue);
//m_pConnection->Execute((_bstr_t)strSQL,NULL,NULL); 2007-10-18
//m_pConnection->Execute((_bstr_t)strSQL,NULL,adCmdText);
//m_pConnection->CommandTimeout=30;
//
//
m_pConnection->Execute((_bstr_t)strSQL,NULL,NULL);
for(int k=0;k<nCount;k++)
{
for(int j=0;j<50;j++)
strcpy(&Item[k][j],"");
}
}
catch(_com_error e)
{
strcpy(ErrMsg,e.Description());
return false;
}
return true;
}
当数据量比较少时,程序能够正常运行,但是当数据量比较多,比如有上万条时,多台电脑同时运行该程序并且操作同一个表时,就会出错,只有一台能够正常运行,其他的都会出错。
不知道这个问题该如何解决?是表被锁住了么?还是怎么了?有没有什么办法可以优化么?
请大家帮帮忙~~~~