我在SQL数据库中创建了一个只有两列UniqueIdentifier
列的表
现在我需要删除具有相同UserId的某些记录。代码是:
If aspProvider.SQLConn.State = ConnectionState.Closed Then aspProvider.SQLConn.Open()
QueryString = "DELETE FROM aspnet_UsersInRoles WHERE UserId = @UserNewId"
dbHandler.SQLComm = New SqlCommand(QueryString, aspProvider.SQLConn)
srvHandler.SQLComm.CommandType = CommandType.TableDirect
dbHandler.SQLComm.Parameters.Add("@UserNewId", SqlDbType.UniqueIdentifier).Value = UsrId
srvHandler.SQLComm.ExecuteNonQuery()
当指针到达行srvHandler.SQLComm.CommandType
时,它会抛出一个错误:
Object reference not set to an instance of an object.
当我删除这一行时,当指针到达该行时,会发生同样的情况:
srvHandler.SQLComm.ExecuteNonQuery()
UsrId
是GUID的值,当我调试代码时它就在那里
有人告诉我我做错了什么吗?
提前谢谢。
答案 0 :(得分:0)
我通过更改代码来解决问题:
If aspProvider.SQLConn.State = ConnectionState.Closed Then aspProvider.SQLConn.Open()
QueryString = "DELETE FROM aspnet_UsersInRoles WHERE UserId = @UserNewId"
Using SQLComm As New SqlCommand(QueryString, aspProvider.SQLConn)
SQLComm.Parameters.Add("@UserNewId", SqlDbType.UniqueIdentifier).Value = UsrId
SQLComm.ExecuteNonQuery()
End Using