将数据插入表时出错 [英] Error while inserting data into table

查看:84
本文介绍了将数据插入表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个抓取逗号分隔文件并将其插入数据库的应用程序。但插入它时会给我一个错误。代码如下。


不做EOF(iDrop)

线路输入#iDrop,iLine

iArray = Split( iLine,;"

iArray(5)=回合(iArray(5)/ 21.21,0)

带rs

.AddNew

.Fields(" ID")= crID

.Fields(" NO")= iArray(0)

。字段(" SID")= iArray(1)

.Fields(" CID")= iArray(2)

.Fields(" CClass")= iArray(3)

.Fields(" Cfier")= iArray(4)

.Fields(" DB")= iArray(5)

。更新

结束

循环

关闭#iDate


错误:

子程序dosomething中的意外错误-2147217873。

[microsoft] [odbc sql server driver] [sql server]违反主键约束PK _DBand。不能在对象DB中插入重复的密钥


这里Dband是我的主键ID表,DB是表Dband中的一个列。


i我不知道为什么我收到此错误。它适用于一些记录,直到我得到这个消息。


我有大约1000条记录,它只有70-80条记录并给我这个错误。


任何线索为什么会发生这种情况。我不确定解释是否有帮助请向我提问以了解更多。

谢谢

I am developing an application which grab a comma delimited file and inserts them into a database. but while inserting it gives me an error. the code is below.

Do While Not EOF(iDrop)
Line Input #iDrop, iLine
iArray = Split(iLine, ";")
iArray(5) = Round(iArray(5) / 21.21, 0)
With rs
.AddNew
.Fields("ID") = crID
.Fields("NO") = iArray(0)
.Fields("SID") = iArray(1)
.Fields("CID") = iArray(2)
.Fields("CClass") = iArray(3)
.Fields("Cfier") = iArray(4)
.Fields("DB") = iArray(5)
.Update
End With
Loop
Close #iDate

Error:
unexpected error -2147217873 in subroutine dosomething.
[microsoft][odbc sql server driver][sql server] violation of primary key constrain PK_DBand. cannot insert duplicate key in object DB


here Dband is my table with primary key ID and DB is one of my column in the table Dband.

i am not sure why i am getting this error. It works for some records until i get this message.

in all i have about 1000 records and it does only 70-80 records and gives me this error.

any clues why this is happening. i am not sure the explaination helps or not please ask me question to understand more.
thanks

推荐答案

你正在拆分分号( ; )但你说文件是用逗号分隔的。


哦,还有一个问题 - crID在哪里得到它的值?
You are splitting on a semicolon (;) but you said the file is comma-delimited.

Oh, and a question - where is crID getting its value?


抱歉它的分号分隔...而且crID是在另一个表上查询完成的,并且是表中的statis值。 crID我得到的正确并且不是问题。


sorry its semicolon delimited...and also crID is query done on the another table and is a statis value from a table. crID i get it correctly and is not an issue.



你正在拆分分号(; )但是你说文件是以逗号分隔的。


哦,还有一个问题 - crID在哪里获得它的价值?
You are splitting on a semicolon (;) but you said the file is comma-delimited.

Oh, and a question - where is crID getting its value?



抱歉它的分号分隔...而且crID是在另一个表上查询完成的表中的statis值。我正确地得到它并不是问题。
sorry its semicolon delimited...and also crID is query done on the another table and is a statis value from a table. crID i get it correctly and is not an issue.



那么,这是否意味着我们正在寻找代码的简化版本?就目前而言,由于 .Fields(" ID")= crID ,它看起来好像会在第二条记录的主键上产生重复值错误。

So, does that mean we''re looking at a cut-down version of the code? As it stands, it looks as though it should produce a duplicate-value error on your primary key for the second record, because of .Fields("ID") = crID.


这篇关于将数据插入表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆