【问题标题】:Duplicating rows in database复制数据库中的行
【发布时间】:2011-06-09 03:16:20
【问题描述】:

由于某种原因,下面的 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

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

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

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    您没有像在中那样执行第二个 SQL 插入命令:

    intRecordCount = CType(objSQLCommand.ExecuteScalar(), Integer)
    

    【讨论】:

    • 如果用户不存在应该只复制行
    • 就是这样。谢谢!不能再接受几分钟的回答。
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2016-06-22
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多