以infopath形式从vb.net调用Sql过程

时间:2013-04-24 13:31:30

标签: vb.net

我正在尝试使用vb.net执行以下代码从infopath表单调用sql过程,我调试并检查错误但代码正在执行并且面临问题以检索执行后收到的输出消息过程

请尽快帮我解决这些问题

感谢

Try
         Dim con As New SqlConnection("Data Source= XXXX;Initial Catalog= XXXXX;Integrated Security=true")
         Dim cmd As New SqlCommand
         Dim dr As SqlDataReader
         con.Open()
         cmd.CommandType = Data.CommandType.StoredProcedure
         cmd.CommandText = "Procedure Name"
         cmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar, 50)).Value = "XXX" 
         cmd.Parameters.Add(New SqlParameter("@CustID", SqlDbType.VarChar, 20)).Value = "XXX"  
         cmd.Connection = con
         dr = cmd.Executereader()
         dr.Read()
         If dr.HasRows Then
             MessageBox.Show(dr.GetString(0))
         End If
                con.Close()
Catch ex As Exception
         MessageBox.Show(ex.Message)
End Try

我的Sql程序:

enter code here

Create procedure Test_Infopath

@Name varchar(50),
@CustID Varchar(20)
      AS 

      Declare @Condition INT
      SELECT @Condition = CustID FROM temp_infopath WHERE CustID = @CustID
      IF @CustID= @Condition
      BEGIN 
                Print 'Customer ID already Exists'
      END
      Else 
                Print 'Submit the form to insert the data'

1 个答案:

答案 0 :(得分:0)

因此,您希望在存储过程中返回消息,而不是记录 但是,SqlDataReader和ADO.NET的其他方法被认为是返回记录而不是消息字符串 我建议你以这种方式改变你的程序

ALTER PROCEDURE Test_Infopath
    @Name varchar(50),
    @CustID Varchar(20)
    AS 

    Declare @Condition INT
    SELECT @Condition = CustID FROM temp_infopath WHERE CustID = @CustID

    IF @CustID= @Condition
        SELECT 'Customer ID already Exists'
    Else 
        SELECT 'Submit the form to insert the data'

现在,SqlDataReader将该消息视为SELECT
返回的记录 顺便说一句,当你返回由单行组成的单个记录时,编写这个场景的常用方法是通过SqlCommand的ExecuteScalar方法,而不涉及另一个对象实例,如SqlDataReader。

cmd.Connection = con
Dim message = cmd.ExecuteScalar().ToString()
MessageBox.Show(message)