简体   繁体   中英

Duplicating rows in database

For some reason, the if statement below is not working

''# 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) returns a 0 if 0 records are found and it returns 2 if i manually insert 2 rows for the user in the database.

But for some reason, the if statement does not work. If I run the "insert select" query manually, it works perfectly.

What am I doing wrong?

您没有像在以下步骤中那样执行第二条SQL插入命令:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM