复制数据库中的行

时间:2011-01-14 12:43:26

标签: .net asp.net sql vb.net .net-3.5

出于某种原因,下面的if语句不起作用

''# count if records with this user already exist in the database below
objSQLCommand = New SqlCommand("select count(id) as record_count from table1 where user = @strUser", objSQLConnection)
objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser

objSQLCommand.Connection.Open()
intRecordCount = CType(objSQLCommand.ExecuteScalar(), Integer)
objSQLCommand.Connection.Close()

''# duplicate default rows in database if username not in database
If intRecordCount = 0 Then
    Response.Write(intRecordCount)

    objSQLCommand = New SqlCommand("insert into table1 (heading, user) select heading, @strUser as user from table1 where defaults = @intDefault", objSQLConnection)
    objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser
    objSQLCommand.Parameters.Add("@intDefault", SqlDbType.Int, 4).Value = 1
End If

如果找到0条记录,则Response.Write(intRecordCount)返回0,如果我在数据库中手动为用户插入2行,则返回2。

但由于某种原因,if语句不起作用。如果我手动运行“插入选择”查询,它将完美运行。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您没有执行第二个SQL插入命令,如下所示:

intRecordCount = CType(objSQLCommand.ExecuteScalar(), Integer)
相关问题