cmd.executenonquery 在 vb.net Windows 应用程序中返回 -1 [英] cmd.executenonquery is returning -1 in vb.net windows application

查看:21
本文介绍了cmd.executenonquery 在 vb.net Windows 应用程序中返回 -1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am just inserting a record in sqlserver database using stored procedure. In this code Binddata() is the method where i have binbed the gridview with records.

代码如下:

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

结束子

在这段代码中,cmd.ExecuteNonQuery() 返回 -1,所以我给出了 (i<0) 条件,当行受到影响时它应该是正值.

In this code cmd.ExecuteNonQuery() is returning -1 so i have given (i<0) condition actually it should be positive value when rows are affected.

提前致谢.

推荐答案

记录是否保存到数据库?如果是这样,那是因为存储过程没有返回值.

Is the record saved to the database? If so, then that's because the stored procedure is not returning a value.

ExecuteNonQuery()

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

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

如果您需要从存储过程中返回一个值,请查看此StackOverFlow 问题

If you need to return a value from the stored procedure, look at this StackOverFlow Question

附带说明 - 考虑使用USING Block" 用于您的连接

On a side note - Look into using the "USING Block" for your connections

这篇关于cmd.executenonquery 在 vb.net Windows 应用程序中返回 -1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆