C# - using TableAdapter to return a single value from stored procedure returns null

十年热恋 提交于 2019-12-12 14:07:13

问题


I do not understand but my stored procedure which I added to table adapter only returns null value. It is supposed to return a simple integer value. In the preview I had with data set desinger, I could clearly get the integer value that I wanted. But for some reason I cannot get the value from my codes.

I followed the instruction of MSDN library: http://msdn.microsoft.com/en-us/library/37hwc7kt(VS.80).aspx

My code for c# is:

humansDataSetTableAdapters.ProfilesTableAdapter tableAdapter 
= new humansDataSetTableAdapters.ProfilesTableAdapter(); 

int returnValue = (int)tableAdapter.getSample();

Console.Write(returnValue);

My code for stored procedure getSample is:

DECLARE @r int
SET @r = 7
RETURN @r

Can anybody let me know how I can solve this problem?? Any help will be appreciated!


回答1:


Scalar expects a result, not a return. Be definition, it looks for the first column, first row. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

Try

DECLARE @r int
SET @r = 7
SELECT @r



回答2:


rather than going for this solution I would like to suggest you to use ExecuteScalar if stored procedure returning single value.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx




回答3:


Are you using a typed DataSet? If so, make sure your Stored Procedure is set to return a Scalar value.



来源:https://stackoverflow.com/questions/5409904/c-sharp-using-tableadapter-to-return-a-single-value-from-stored-procedure-retu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!