我想对表执行搜索以查看记录是否存在。我不想在之后执行插入或更新。我已经这样做了,但不知何故我无法让它发挥作用。在我的 asp.net 页面上,我似乎无法返回任何值。错误是“输入字符串格式不正确”我确信这很明显,但我现在似乎看不到它!
这是我的代码:
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()
存储过程:
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