的ExecuteNonQuery返回值 [英] Executenonquery return value

查看:169
本文介绍了的ExecuteNonQuery返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个表上的搜索,看是否存在记录。我不想以后执行插入或更新。我已经这样做了,但不知何故,我不能得到这个工作。在我的asp.net页面,我似乎无法得到任何返回值。错误是输入字符串格式不正确我是个相信这是显而易见的,但我似乎现在不能看呢!

这是我的code:

 昏暗CON作为新的SqlConnection(CONNSTRING)
DIM CMD作为新的SqlCommand(检查名CON)
cmd.CommandType = CommandType.StoredProcedurecmd.Parameters.Add(新的SqlParameter(@ D,SqlDbType.Int))
cmd.Parameters(@ ID)。值= TextBox1.Text昏暗段作为新的SqlParameter
para.Direction = ParameterDirection.ReturnValue
para.ParameterName =返回值
cmd.Parameters.Add(对)con.Open()
cmd.ExecuteNonQuery()
昏暗的存在作为整数
存在= Convert.ToInt32(cmd.Parameters(返回值)。值)
如果不存在= 1,则
    Label1.Text =你......
         elseif的存在= 0。然后
    Label1.Text =你.....万一
con.Close()

存储过程:

  CREATE PROCEDURE检查名
     - 添加参数的存储过程在这里
    @id INT

   - 这意味着它的存在,它返回ASP告诉我们
  - 选择'已经存在IF EXISTS(SELECT * FROM WHERE与会者ID = @id)
开始
返回1
结束
其他
开始
   返回0
结束


解决方案

您需要确保您传递一个整数。

  INT的intValue;
如果(!int.TryParse(TextBox1.Text,出来的intValue))
{
     //更新您的网页以指示错误     返回;}cmd.Parameters.Add(新的SqlParameter(ID,SqlDbType.Int));
。cmd.Parameters(ID)值=的intValue;

(从技术上讲不需要的@字符时,
   定义.NET程序的参数
   code)

I want to perform a search on a table to see if record exists. I do not want to perform insert or update after. I have done this already but somehow I cannot get this to work. On my asp.net page I cannot seem to get any value returned. The error is "input string not in correct format" I ma sure it is obvious but I cannot seem to see it now!

here is my code:

Dim con As New SqlConnection("connstring")
Dim cmd As New SqlCommand("checkname", con)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(New SqlParameter("@d", SqlDbType.Int))
cmd.Parameters("@id").Value = TextBox1.Text

Dim para As New SqlParameter
para.Direction = ParameterDirection.ReturnValue
para.ParameterName = "returnvalue"
cmd.Parameters.Add(para)

con.Open()


cmd.ExecuteNonQuery()
Dim exists As Integer
exists = Convert.ToInt32(cmd.Parameters("returnvalue").Value)
If exists = 1 Then
    Label1.Text = "You......"
         ElseIf exists = 0 Then
    Label1.Text = "You....."

End If
con.Close()

stored procedure:

CREATE PROCEDURE checkname 
    -- Add the parameters for the stored procedure here
    @id int
AS
  --This means it exists, return it to ASP and tell us
 -- SELECT 'already exists'

IF EXISTS(SELECT * FROM attendees WHERE id = @id)
BEGIN
RETURN 1
END
ELSE
BEGIN
   RETURN 0
END

解决方案

You need to ensure that you are passing an integer.

int intValue;
if(!int.TryParse(TextBox1.Text, out intValue))
{
     // Update your page to indicate an error

     return;

}

cmd.Parameters.Add(New SqlParameter("id", SqlDbType.Int));
cmd.Parameters("id").Value = intValue; 

(Technically you don't need the "@" character when defining the parameters in the .NET code.)

这篇关于的ExecuteNonQuery返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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