2013-07-16 83 views
-1

我現在有這個代碼。但它失敗了三個步驟。你能幫我弄清楚嗎?VB.net表迭代

我列出了失敗的三點。

也請爲我驗證如果我做對了嗎?

Retrieve the string from the tb_metatags textbox 
Dim s As String 
s = Me!tb_metaatags 

parse the string into substrings by looking for the commasDim arrLines() As String 
Dim arrLines() As String 
arrLines = Split(s, ",") 
For each substring, check if the substring is in the MetaSearchTags table 
Dim itm As Variant 
For Each itm In arrLines 
    Dim strsql As String 
    Dim numrows As Integer 
    strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = " & itm & "" 
    Dim objcmd As New OleDbCommand(strsql, conn) "I get an error here 
    numrows = objcmd.ExecuteScalar 

    If numrows > 0 Then 
     MsgBox("Record Exists", vbInformation, "Add") "I get an error here 
    Else 
    Dim myadapter1 As New OleDbDataAdapter("INSERT INTO MetaSearchTags (SearchTag) "VALUES ('" & itm & "')", conn) "I get an error here 
    Dim mytable1 As New DataTable 
    myadapter1.Fill (mytable1) 
    End If 

if it is not already in the MetaSearchTags table, then add it to the table 
get the primary key (ID) for the substring from the MetaSearchTags table 
Add an row in the MetaSearchTagAssignments table for this search tag 
using the projectID, and the substring ID from the MetaSearchTags table 
Repeat this process for each substring entered in the field 
+3

如果你用撇號評論你的筆記會更好,所以有人可以複製並粘貼你的代碼。您還需要確認收到的完整錯誤消息。 –

+3

完全刪除了最後一段的代碼格式,因爲它是文本段落,並且與代碼無關。請[編輯]它,以便閱讀(和複製/粘貼)更容易。此外,當你在這裏時,發佈關於「我得到一個錯誤」的細節的含義,因爲「我得到一個錯誤」沒有關於「錯誤」的細節沒有意義。每個人都有錯誤信息;請分享它,因爲我們無法從這裏看到它們。 :-) –

+0

這條線上的錯誤是什麼? 'Dim myadapter1 As New OleDbDataAdapter(「INSERT INTO MetaSearchTags ...' –

回答

0

你需要把周圍的字符串單引號中的SQL語句:

strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = " & itm & "" 

應該是:

strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = '" & itm & "'" 
+2

那些不是」括號「(括號是'[]'),它們是單引號(''')。另外,最後一個問題應該是對問題本身的評論,而不是你答案的一部分;它要求澄清問題,並且應該將信息作爲編輯添加到問題中,而不是作爲對答案的評論(不降低你的答案;我只是給你一個改正的機會。) –

0

OleDbCommand的。 ExecuteScalar返回

第一行的在結果集中的第一列,或者如果結果集爲空空 參考。

您需要處理這個null引用(在VB.NET這相當於Nothing)時,沒有記錄返回。要做到這一點

一種方法是:

Dim numrows as String = String.Empty 

numrows = objcmd.ExecuteScalar() 
If numrows Is Nothing Then 
    'Do something with the error condition 
Else 
    'Do something with numrows which contains a valid result. 
End If 

(我會重新命名numrows

您還試圖插入一條記錄,即使沒有返回結果表。這不會是一個錯誤,但是您已經指出SearchTag是主鍵,但在這種情況下,嘗試插入重複項時會出錯(儘管解釋有點困難)。

而且,如上所述,您需要更正INSERT語句的引號和撇號。