首页 \ 问答 \ 比较受影响的行数(Compare Number of Rows affected)

比较受影响的行数(Compare Number of Rows affected)

我正在尝试比较一个字符串(网络ID)和使用SQL Like命令,返回受影响的行数(如果找到或不找到用户名)但是在我的代码中我总是得到“-1”,我找不到原因,用户名是正确的,并通过在SQL Server管理中运行查询在SQL表中找到。

Try
        Dim Con As New SqlConnection
        Con.ConnectionString = "Data Source=WCRDUSMJEMPR9\SQLEXPRESS;Initial Catalog=MicroDB;Integrated Security=True"
        Con.Open()
        Dim SQL2 As String
        SQL2 = "SELECT * from MicroDB_Users WHERE Users LIKE '+@Usercheck+'"
        Dim cmd2 As New SqlCommand(SQL2, Con)
        cmd2.Parameters.AddWithValue("@Usercheck", TextBox1.Text)
        Dim obj2 = cmd2.ExecuteNonQuery
        Con.Close()
        If obj2 > 0 Then
            MsgBox(obj2)
            Response.Redirect("~\ControlCharts\AddData_Control.aspx")
            Label7_Control.Visible = False
        Else
            MsgBox(obj2)
            Label7_Control.Text = ("You are not authorized to Add Data")
            Label7_Control.Visible = True
        End If
    Catch ex As Exception
        MsgBox(Err.Description)

如您所见,我使用IF来比较用户是否被发现(1行受影响)或未找到(受影响的0行)。


I'm trying to compare a string (network ID) and using a SQL Like command, return the numbers of rows affected (if the user name was found or not) however in my code I'm always getting " -1 " which i can not find why, the username is correct and found in the SQL table by running a query in SQL Server Mgt.

Try
        Dim Con As New SqlConnection
        Con.ConnectionString = "Data Source=WCRDUSMJEMPR9\SQLEXPRESS;Initial Catalog=MicroDB;Integrated Security=True"
        Con.Open()
        Dim SQL2 As String
        SQL2 = "SELECT * from MicroDB_Users WHERE Users LIKE '+@Usercheck+'"
        Dim cmd2 As New SqlCommand(SQL2, Con)
        cmd2.Parameters.AddWithValue("@Usercheck", TextBox1.Text)
        Dim obj2 = cmd2.ExecuteNonQuery
        Con.Close()
        If obj2 > 0 Then
            MsgBox(obj2)
            Response.Redirect("~\ControlCharts\AddData_Control.aspx")
            Label7_Control.Visible = False
        Else
            MsgBox(obj2)
            Label7_Control.Text = ("You are not authorized to Add Data")
            Label7_Control.Visible = True
        End If
    Catch ex As Exception
        MsgBox(Err.Description)

As you can see, i;m using a IF to compare if the user was found ( 1 row affected) or if it was not found ( 0 rows affected).


原文:https://stackoverflow.com/questions/14469564
更新时间:2023-06-28 11:06

最满意答案

在查看您在评论中提供的网站后,我注意到您的提交表单处理程序仅提交了4个字段( nameemailphonecomments )。 这需要修改为包括g-recaptcha-response

来自第1141行的http://zacktarrcreations.com/AFK/js/plugins.js

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

改成:

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                    "g-recaptcha-response": $('#g-recaptcha-response').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

经过测试,它对我有用。


After looking at your website that you provided in the comments, I noticed your submit form handler is only submitting 4 fields (name, email, phone, and comments). This needs to be modified to include g-recaptcha-response.

From http://zacktarrcreations.com/AFK/js/plugins.js on line 1141:

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

Change to:

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                    "g-recaptcha-response": $('#g-recaptcha-response').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

Tested and it worked for me.

相关问答

更多