我现在有了这个代码。但它在三个步骤失败了。你能帮我搞清楚吗?
我列出了失败的三点。
另外请为我验证如果我做得对吗?
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
答案 0 :(得分:0)
您需要在SQL语句中围绕字符串放置单引号:
strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = " & itm & ""
应该是:
strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = '" & itm & "'"
答案 1 :(得分:0)
OleDbCommand。ExecuteScalar返回
结果集中第一行的第一列,或null 如果结果集为空,则引用。
当没有返回任何记录时,你需要处理这个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语句的引号和撇号。