-1

我现在有这个代码。但它在三个步骤中失败了。你能帮我解决这个问题吗??

我列出了它失败的三点。

如果我做得对,也请为我验证?

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
4

2 回答 2

0

您需要在 SQL 语句中的字符串周围加上单引号:

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

应该:

strsql = "SELECT COUNT(*) FROM MetaSearchTags WHERE SearchTag = '" & itm & "'"
于 2013-07-17T00:01:21.423 回答
0

OleDb 命令。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 语句的引号和撇号。

于 2013-07-17T00:41:42.327 回答