在vb.net windows应用程序中,cmd.executenonquery返回-1

在vb.net windows应用程序中,cmd.executenonquery返回-1,vb.net,Vb.net,我只是使用存储过程在sqlserver数据库中插入一条记录。 在这段代码中,Binddata()是我将gridview与记录组合在一起的方法 代码如下: Private Sub btnadd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnadd.Click sqlSP = "INSERT_E_CLIENTMASTER_REC" strConnection =

我只是使用存储过程在sqlserver数据库中插入一条记录。 在这段代码中,Binddata()是我将gridview与记录组合在一起的方法

代码如下:

Private Sub btnadd_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnadd.Click

    sqlSP = "INSERT_E_CLIENTMASTER_REC"
    strConnection = _
        ConfigurationManager.ConnectionStrings("connectstring").ConnectionString
    Dim conn As New SqlConnection(strConnection)

    conn.Open()

    Dim cmd As New SqlCommand(sqlSP, conn)

    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add(
        New SqlParameter("@CLIENTCODE", SqlDbType.VarChar, 100, _
            ParameterDirection.Input, False, 0, 0, "", _
            DataRowVersion.Proposed, txtclientcode.Text))
    'cmd.Parameters.Add( _
    '    New SqlParameter("@CLIENT_WEBXID", SqlDbType.NVarChar, _
    '        Convert.ToString(txtwebxid.Text))) _
    cmd.Parameters.Add( _
        New SqlParameter("@CLIENT_WEBXID", SqlDbType.VarChar, 100, _
            ParameterDirection.Input, False, 0, 0, "", _
            DataRowVersion.Proposed, txtwebxid.Text))

    cmd.Parameters.Add( _
        New SqlParameter("@ToEmail", SqlDbType.VarChar, 100, _
            ParameterDirection.Input, False, 0, 0, "", _
            DataRowVersion.Proposed, txttoemail.Text))

    cmd.Parameters.Add( _
        New SqlParameter("@ClientName", SqlDbType.VarChar, 100, _
        ParameterDirection.Input, _
        False, 0, 0, "", DataRowVersion.Proposed, txtclientname.Text))

    cmd.Parameters.Add(_
        New SqlParameter("@CcEmail", SqlDbType.VarChar, 100, _
        ParameterDirection.Input, False, 0, 0, "", _
        DataRowVersion.Proposed, txtccname.Text))

    Dim i As Int32 = cmd.ExecuteNonQuery()
    If (i < 0) Then
        BindData()
        MsgBox("Record Inserted successfully ", MsgBoxStyle.OkOnly)
        txtclientcode.Text = ""
        txtwebxid.Text = ""
        txttoemail.Text = ""
        txtclientname.Text = ""
        txtccname.Text = ""

    Else
        MsgBox("Record Not Inserted successfully ", MsgBoxStyle.OkOnly)
    End If
Private Sub btnadd_Click(ByVal sender作为System.Object_
ByVal e作为System.EventArgs)处理btnadd。单击
sqlSP=“插入\u E\u客户端主机\u REC”
strConnection=_
ConfigurationManager.ConnectionString(“connectstring”).ConnectionString
Dim conn作为新的SqlConnection(strConnection)
康涅狄格州公开赛
Dim cmd作为新的SqlCommand(sqlSP,conn)
cmd.CommandType=CommandType.storedProcess
cmd.Parameters.Add(
新的SqlParameter(“@CLIENTCODE”,SqlDbType.VarChar,100_
ParameterDirection.Input,False,0,0,“_
DataRowVersion.Proposed,txtclientcode.Text)
'cmd.Parameters.Add(_
'新的SqlParameter(“@CLIENT_WEBXID”),SqlDbType.NVarChar_
'Convert.ToString(txtwebxid.Text)))_
cmd.Parameters.Add(_
新的SqlParameter(“@CLIENT_WEBXID”),SqlDbType.VarChar,100_
ParameterDirection.Input,False,0,0,“_
DataRowVersion.Proposed,txtwebxid.Text)
cmd.Parameters.Add(_
新的SqlParameter(“@ToEmail”,SqlDbType.VarChar,100_
ParameterDirection.Input,False,0,0,“_
DataRowVersion.Proposed,txttoemail.Text)
cmd.Parameters.Add(_
新的SqlParameter(“@ClientName”,SqlDbType.VarChar,100_
参数方向。输入_
False,0,0,“,DataRowVersion.Proposed,txtclientname.Text))
cmd.Parameters.Add(_
新的SqlParameter(“@CcEmail”,SqlDbType.VarChar,100_
ParameterDirection.Input,False,0,0,“_
DataRowVersion.Proposed,txtcName.Text)
Dim i As Int32=cmd.ExecuteNonQuery()
如果(i<0),则
BindData()
MsgBox(“成功插入记录”,MsgBoxStyle.OkOnly)
txtclientcode.Text=“”
txtwebxid.Text=“”
txttoemail.Text=“”
txtclientname.Text=“”
txtcname.Text=“”
其他的
MsgBox(“未成功插入记录”,MsgBoxStyle.OkOnly)
如果结束
端接头


在这段代码中,cmd.ExecuteNonQuery()返回-1,因此我给出了(i记录是否保存到数据库?如果是,那是因为存储过程没有返回值

用于更新、插入和删除 语句,返回值为 受影响的行数 当触发器存在于 正在插入或更新的表 返回值包括 受insert或insert命令影响的行 更新操作和 受触发器影响的行或 触发器。对于所有其他类型的 语句,返回值为-1。如果 发生回滚时,返回值为 还有-1

如果需要从存储过程返回一个值,请查看以下内容

在旁注中-查看如何使用“”进行连接

从存储过程中删除行“SET NOCOUNT On;”

设置NOCOUNT(Transact-SQL):

您好。我没有从存储过程返回任何值。但是ExecuteNonQuery()方法返回受影响的行数,这是一个正数。但是我得到的-1值是错误的,因此我的代码是正确的。感谢您回答ExecuteNonQuery()在执行插入、更新或删除时将返回受影响的行。您的存储过程没有返回值,这就是为什么您要将-1作为返回值。我已编写了用于插入、更新或删除的存储过程。正在插入/更新/删除记录,但如果我给出if(I>0)条件然后进入else块,而不是if块,因为调试时的i值是-1。我不知道出了什么问题。