问题
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