更新上一行

我有一个Excel文件,其中包含一些我想要导出到Access数据库的数据。 在C列中,我有一个名为“Description”的字段。 通常这个领域只占用一个单元格,但可能会更长。

在这里输入图像说明

在这种情况下,例如,AP.01有5行说明。 如何更新下一行的第一行?

Public Sub updateDB(ByVal PathDB As String, str As String, id As Integer) Dim db As New cDB Dim v As New cVoce Dim rs As ADODB.Recordset = db.RecordSet v.Description = str db.connetti_DB(PathDB) db.get_rs("UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'") End Sub Public Function get_rs(ByVal query As String) As ADODB.Recordset If db Is Nothing Then rs = Nothing : Return rs rs = New ADODB.Recordset rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic rs.LockType = ADODB.LockTypeEnum.adLockOptimistic rs.Open(query, db) Return rs End Function 

此代码不起作用,因为我更新我的当前行,由于这个原因没有用的UPDATE指令。 我如何修复我的代码?

编辑我张贴在这里For循环

 For r = 2 To grid.RowCount - 1 vett = Split(grid(r, 1).Text) total = UBound(Split(grid(r, 1).Text, ".")) If grid(r, 1).Text <> "" Then Select Case total Case 0 Dim chapter As New cChapter flag = 1 id = id + 1 chapter.Cod = grid(r, 1).Text.Substring(0, 1) chapter.Description = grid(r, 3).Text If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then chapter.Cod = Left(vett(0), 1) oldChap = chap.Cod If chapter.Cod <> oldCap Then chapters.Add(chapter) End If End If chapters.Add(chapter) stringChap = chap.Description Dim par As New cParagraph If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then par.Cod = Left(vett(0), 2) par.Cod_Chapter = Left(vett(0), 1) oldPar = par.Cod If par.Cod <> oldPar Then paragraphs.Add(par) End If End If If grid(r, 3).Text.Length > 255 Then par.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..." Else par.Description = grid(r, 3).Text.ToString End If paragraphs.Add(par) stringPar = par.Description Case 1 flag = 2 id = id + 1 c_Voc = voc.Cod_Chapter p_Voc = voc.Cod_Paragraph voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1) voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0) voc.Cod_Voice = Right(vett(0), 2) If grid(r, 3).Text.Length > 255 Then voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..." Else voc.Description = grid(r, 3).Text.ToString If voc.Description.EndsWith("-") Then a = Replace(voc.Description, "-", "") voc.Description = a End If End If stringVoice = voc.Description voices.Add(voc) voices.Save_DB(dbDest) Case 2 flag = 3 id = id + 1 sVoice = New cVoice oldSvoice = voice.Cod_SVoice sVoice.Cod_SVoice = Left(vett(0), 2) If sVoice.Cod_SVoce <> oldSvoice Then voices.Add(sVoice) voices.Save_DB(dbDest) End If If grid(r, 3).Text.Length > 255 Then sVoice.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..." Else sVoice.Description = grid(r, 3).Text End If stringSvoice = sVoice.Description sVoice.Cod_Voce = Left(vett(0), 5) sVoice.Price1 = grid(r, 12).Text sVoice.Price2 = sVoice.Price1 sVoice.UniMi = grid(r, 11).Text sVoce.Sep = "." voices.Add(sVoce) voices.Save_DB(dbDest) End Select Else If flag = 1 Then stringChap = grid(r, 3).Text chap.Description = stringChap & grid(r, 3).Text stringPar = grid(r, 3).Text paragraph.Description = stringPar & grid(r, 3).Text End If If flag = 2 Then stringVoice = grid(r, 3).Text voc.Description = voc.Description & stringVoice voices.updateDB(dbDest, stringVoice, id) voices.Add(voc) End If If flag = 3 Then stringSvoice = grid(r, 3).Text sVoice.Description = stringSvoice & grid(r, 3).Text voices.Add(sVoice) End If chapter.Save_DB(dbDest) paragraph.Save_DB(dbDest) voice.Save_DB(dbDest) End If Next 

EDIT2我宣布id为整数,当Code列有一个值,然后id = id + 1。 这样我总是知道我要修改哪一行。 我也修改了updateDB(现在我使用3个参数),并在查询中添加了一个WHERE条件。 尽pipe更新,没有任何改变

在数据库中,不能存储没有PrimaryKey的logging(实际上你可以,但这是个坏主意)。 因为在你的解决scheme中,id实际上是Excel行号(对不起,如果我不正确,但看起来像代码),将来可能很难维护它(以防有人添加或删除描述行)。 将id列更改为文本并使用代码作为PK会更好。 存储描述然后可以用两种方式解决:

1)将包含描述的所有行连接成1个variables,在其间添加vbNewLine并将其存储在描述字段中。

2)更多的关系,但也更复杂 – 创build第二个表的描述与PK作为即自动编号,ForeignKey代码参考主表。 维护将会非常复杂。 不值得。

代码中的变化量是相当大的,所以对不起,我不会提供固定的代码,但我希望想法是明确的。

顺便说一句:为什么描述不更新描述在您的文章。 只有当代码存在时,才会增加id,所以第一组中的每个描述字段的id = 1。代码中最简单的修复就是创build2条更新语句 – 一条代码行

 UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"' 

第二个没有代码的行:

 UPDATE Voice SET Description = Description + CHAR(13) + CHAR(10) + '" + v.Description + "' WHERE id= '"+id+"'