如何使用asp.net vb代码调用存储过程

时间:2012-10-26 09:50:29

标签: asp.net

我有一个名为dissumbio的存储过程,这将返回一些值

现在我想在我的asp.net程序中调用它,返回的值应该显示在我的文本框中

我的代码如下

Dim admdate As String

admdate = Mid(txtadmdate.Text, 4, 2) + "/" + Left(txtadmdate.Text, 2) + "/20" + Right(txtadmdate.Text, 2)

Dim cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@strId", txthospno.Text)
cmd.Parameters.AddWithValue("@strfromdate", admdate)

cmd.Connection = sqlConn2
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()

这是arjent,请帮助

提前致谢

舒卜哈

2 个答案:

答案 0 :(得分:1)

ExecuteNonQuery只返回受影响的行数...所以在你的情况下,如果你的存储过程返回一个值,你必须使用`

Dim cmd As New SqlCommand
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@strId", txthospno.Text)
cmd.Parameters.AddWithValue("@strfromdate", admdate)

cmd.Connection = sqlConn2
sqlConn2.Open()
value= Convert.ToInt32(cmd.ExecuteScalar())

`

答案 1 :(得分:0)

如果您需要检索数据,则应使用ExecuteReader代替ExecuteNonQuery

请查看有关Execute a Stored Procedure that Returns Rows

的这篇文章
Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader

cmd.CommandText = "StoredProcedureName"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = sqlConnection1

sqlConnection1.Open()

reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.

sqlConnection1.Close()