我根据上一个问题编写了一个SQL Server存储过程,我问哪个回答是为什么我要创建一个新问题。
存储过程
ALTER Procedure [dbo].[usp_validatecard](
@CLUBCARD1 nvarchar(50),
@STATUS nvarchar(50))
As
BEGIN
IF EXISTS(Select CLUBCARD1, STATUS
from dbo.clubmembers
where CLUBCARD1 = @CLUBCARD1 and STATUS = 'ACTIVE')
Print 'Card is GOOD'
ELSE
IF EXISTS(SELECT CLUBCARD1, STATUS
FROM dbo.clubmembers
where CLUBCARD1 = @CLUBCARD1 and STATUS = 'INACTIVE')
PRINT 'Card is good but not active'
ELSE
PRINT 'CARD NOT IN SYSTEM'
END
我遇到的麻烦不太清楚如何在VB中调用它。我到目前为止的代码就是这个
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub cmdclick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdclick.Click
Dim strConnString As String = WebConfigurationManager.ConnectionStrings("orca").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("usp_validatecard", con)
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Parameters.Add("@CLUBCARD1", Data.SqlDbType.NVarChar).Value = txtvalidate.Text
Try
con.Open()
Dim Result As Integer = cmd.ExecuteNonQuery()
Catch ex As SqlException
lblmessage.Text = "Error"
Finally
con.Close()
End Try
End Sub
End Class
我想要做的是,如果找到了clubcard参数并且状态是有效的,我希望标签上写着'卡是好的'如果找到了clubcard参数但是不活动那么标签应该是'card is好的但不活跃的'最后如果所有那些失败只是标签上写着'卡不在系统中'
到目前为止,这段代码不起作用我以为我可以调用存储过程并执行它,它会根据传递的内容打印语句,但它会出错。
当我调试它时,我得到的错误是在cmd.parameters.add
下项目为了评估索引 物业,财产必须 必须有资格和参数 由用户明确提供。
答案 0 :(得分:1)
您需要从存储过程中获取结果。
打印不返回任何内容,它只是在SQL Server中打印(在管理工作室中运行存储过程时很有用)。
您需要在代码中将@STATUS参数定义为输出参数(使用空字符串定义参数,然后设置parameter.Direction = ParameterDirection.Output)。
然后在存储过程中,而不是打印,你需要说SET @ STATUS = string。
答案 1 :(得分:1)
您可以将SP更改为
BEGIN
IF EXISTS(Select CLUBCARD1, STATUS
from dbo.clubmembers
where CLUBCARD1 = @CLUBCARD1 and STATUS = 'ACTIVE')
Print 'Card is GOOD'
Select 'Card is GOOD';
ELSE
IF EXISTS(SELECT CLUBCARD1, STATUS
FROM dbo.clubmembers
where CLUBCARD1 = @CLUBCARD1 and STATUS = 'INACTIVE')
PRINT 'Card is good but not active'
Select 'Card is good but not active';
ELSE
PRINT 'CARD NOT IN SYSTEM'
Select 'CARD NOT IN SYSTEM';
END
并在vb.net方面,
Dim reader as SqlDataReader = command.ExecuteReader()
If reader.Read() Then
lblmessage.Text = Convert.ToString(reader(0))
End If
答案 2 :(得分:0)
在您的数据库中尝试此sp:
ALTER Procedure [dbo].[usp_validatecard](
@strCLUBCARD1 nvarchar(50)
)
As
SELECT
CASE
WHEN EXISTS(SELECT CLUBCARD1, [STATUS] FROM dbo.clubmembers WHERE CLUBCARD1 = @strCLUBCARD1 AND [STATUS] = 'ACTIVE') THEN 'CARD IS GOOD'
WHEN EXISTS(SELECT CLUBCARD1, [STATUS] FROM dbo.clubmembers WHERE CLUBCARD1 = @strCLUBCARD1 AND [STATUS] = 'INACTIVE') THEN 'CARD IS GOOD BUT NOT ACTIVE'
ELSE 'CARD NOT IN SYSTEM'
END
...然后在你的数据工厂中:
''' <summary>
''' Validates a card in the database
''' </summary>
''' <param name="clubCard">The card to validate</param>
Public Shared Function ValidateCard(ByVal clubCard As String) As String
'create connection
Using cnSQL As SqlConnection = New SqlConnection("MyConnectionString")
Using cmdSP As New SqlCommand("usp_validatecard", cnSQL)
cmdSP.CommandType = System.Data.CommandType.StoredProcedure
'declare and add parameter
cmdSP.Parameters.Add("@strCLUBCARD1", System.Data.SqlDbType.NVarChar, 50).Value = clubCard
Dim dtTable As New DataTable
'execute command
Try
cnSQL.Open()
dtTable.Load(cmdSP.ExecuteReader())
cnSQL.Close()
Catch ex As SqlException
'do something here
End Try
'return string
Return dtTable.Rows(0)(0)
End Using
End Using
End Function