2015-01-05 44 views
1

我在Visual Studio中连接到SQL Server,我想执行存储过程并检索消息框中的返回值,但我不知道如何。从MsgBox中的存储过程中检索返回值

我刚刚得到这个:

enter image description here

的SqlConnection的工作正常,问题显然是在最后几行:

Dim myConnNAC As New SqlConnection("Initial Catalog=Northwind;Data Source=SERVERNAC\KANTAR;Initial Catalog=NACDroid;Persist Security Info=True;User ID=xxxxxx;Password=xxxxxx;") 

Dim cmd As New SqlCommand 
cmd.CommandText = "NACDRoid_actualiza_domicilios" 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Connection = myConnNAC 

myConnNAC.Open() 

Dim returnValue As Object 
returnValue = cmd.ExecuteReader.ToString() 

MsgBox(returnValue) 

直接在SQL Server时,我执行存储过程显示0作为返回值如果是好的,这就是我需要在MsgBox中。

enter image description here

+2

改为使用'ExecuteScalar()'。 – abatishchev

+1

为什么你将Object的对象变暗为ReturnValue?在你的SQL代码中,它是一个整数。 –

+0

哎呀谢谢你们,我做了你们的改变,现在可以工作了! 'Dim returnValue As Integer' and'returnValue = cmd.ExecuteScalar' 非常感谢。 – user3810795

回答

3
cmd.ExecuteReader.ToString 

这是干什么的?好了,该语句上半年执行

cmd.ExecuteReader 

返回此类型

derp

的一个实例(读取图像中的文字),然后调用ToString在这种情况下。 ToString的默认实现返回类型名称。如在,“System.Data.SqlClient.SqlDataReader”

您需要使用读取器从数据库中获取数据。 Here's the MSDN docs for this simple operation.

通常,如果使用ADO从数据库中检索单个值,您需要在命令对象上查找一个名为ExecuteScalar的漂亮小方法。