日期:2014-05-19  浏览次数:20492 次

请教一下为什么往表中添加(INSERT)数据时会出现行数据(记录)有遗漏的情况呢?
我用MFC程序往一个SQL   SERVER数据库中添加2种变量数据:NumId   和   out1。
但是每次添加时总会随机出现某些行数据的丢失,或者说某行记录丢失。比如往数据库中添加的数据应该为:
NumId     out1
0             0
1             1
2             2
3             3
4             4
5             5
6             6
.
.
.
998         998
999         999
1000       1000
但实际操作结果却为:
NumId     out1
0             0
1             1
2             2
3             3
4             4
6             6           //缺5     5
.
.
.
1000       1000
有时候结果可能为:
NumId     out1
4             4       //缺   1~3
5             5
6             6
.
.
.
998         998
999         999
1000       1000
而遗漏的行记录序号居然随机出现!!

我的插入数据部分代码为:(以前居然没有注意到)
NumberId=0;
Out1=0;
for(int   i=0;i <=1000;i++)
{
    //设置INSERT语句
          CString   strID;
          strID.Format( "%d ",NumberId++);
          CString   strOut;
          strOut.Format( "%f ",Out1++);
          _bstr_t   vSQL;
          vSQL= "INSERT   INTO   Simulator(NumId,   out1)   VALUES     ( "+strID+ ",   "+strOut+ ") ";
          //执行INSERT语句
          try
          {
//是否已经连接数据库
m_pConnection-> Execute(vSQL,NULL,adCmdText);
          }
          catch(_com_error   e)
          {
//显示错误
CString   errormessage;
errormessage.Format( "Execute失败!\r\n错误信息:%s ",e.ErrorMessage());
AfxMessageBox(errormessage);
return;
          }

}
难道是我的代码有问题?


------解决方案--------------------
不会吧,怎么会这样,刚才写了一个过程,应该不是数据库的问题。楼主检查下自己的程序吧。或者把要insert的行数,计算一下,是否满足?


// java code below
public void randomInsert(){
ResultSet rs = null;
PreparedStatement ps = null;
int affectNum = 0;
try{
conn = DriverManager.getConnection(dbURL,System.getProperties());
System.out.println( "insert rows...... ");
for(int i=0;i <100;i++){
ps = conn.prepareStatement(
"insert into b(c1,c2) values(?,?) ");
ps.setInt(1, i);
ps.setString(2, String.valueOf(i));
affe