Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 统计数据库中的记录_.net_Asp.net_Sql_Vb.net_.net 3.5 - Fatal编程技术网

.net 统计数据库中的记录

.net 统计数据库中的记录,.net,asp.net,sql,vb.net,.net-3.5,.net,Asp.net,Sql,Vb.net,.net 3.5,我有下面的代码,但我需要添加更多的功能。我想添加的功能是我在下面的代码中注释的文本 Dim objSQLConnection As SqlConnection Dim objSQLCommand As SqlCommand Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intID As Integer = CType(Request.Form("ID"), In

我有下面的代码,但我需要添加更多的功能。我想添加的功能是我在下面的代码中注释的文本

Dim objSQLConnection As SqlConnection
Dim objSQLCommand As SqlCommand

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim intID As Integer = CType(Request.Form("ID"), Integer)
    Dim strHeading As String = CType(Request.Form("Heading"), String)
    Dim intState As Integer = CType(Request.Form("State"), Integer)
    Dim strUser As String = CType(Request.Form("User"), String)

    objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))

    ''#count if records with this user already exist in the database below

    If intState = 1 Then
        objSQLCommand = New SqlCommand("insert into table1 (id, heading, user) values (@intID, @strHeading, @strUser)", objSQLConnection)

        objSQLCommand.Parameters.Add("@intID", SqlDbType.Int, 4).Value = intID
        objSQLCommand.Parameters.Add("@strHeading", SqlDbType.VarChar, 255).Value = strHeading
        objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser
    ElseIf intState = 0 Then
        objSQLCommand = New SqlCommand("delete from table1 where id = @intID and user = @strUser", objSQLConnection)

        objSQLCommand.Parameters.Add("@intID", SqlDbType.Int, 4).Value = intID
        objSQLCommand.Parameters.Add("@strUser", SqlDbType.VarChar, 3).Value = strUser
    End If

    objSQLCommand.Connection.Open()
    objSQLCommand.ExecuteNonQuery()
    objSQLCommand.Connection.Close()
End

在if语句之前,我想知道数据库中是否已经有strUser变量中用户名为的记录。执行此操作的最佳方法是什么?

我不打算为您完成所有这些,但检查计数的一个简单方法是将存储过程(SQL Server)的结果存储到SqlDataReader中,并检查计数是否>0或HasRows=True

With (myObjectCommand)
    .Parameters.Add("@strUser", SqlDbType.Varchar, 3).Value = myUser
    myReader = .ExecuteReader
End With

if myReader.HasRows
    return true ?
end if

很简单。只需在数据库上执行ExecuteScalar查询,如“从username=@username的表中选择field1”,并将strUser传递到查询对象中。然后将标量结果保存到变量(result)中。然后,如果结果是“”,你可以这样做。请不要重复同一个问题:我认为问题不同,第一个问题,计算用户数,第二个问题,重复记录。