sql server - How to get SQL String Result from Stored Procedure and save it in C# Windows Application string variable -
i have following stored procedure :
alter procedure [dbo].[procedurename] @date nvarchar(50) begin set nocount on; declare @result nvarchar(500) -- 1 should return string. declare @variable1 nvarchar(50) set @variable1 = (select count(*) dbo.table1 column1 not in (select column1 dbo.table2)) declare @variable2 nvarchar(50) update dbo.table1 set columnx = 1 column1 not in (select column1 dbo.table2) set @variable2 = @@rowcount and on... continues 200 rows of script @ least 10-12 variables
after want result
'hello,' + 'some text here' + @date +': ' + 'explaining text variable1- ' + @variable1 + 'updated rows variable2 - ' + @variable2 + 'some other select count - ' + @variable3 + 'some other update rowcount - '+ @variable4 ...... till able print statement, can't take variable in c# code goes this:
public void execute_click(object sender, eventargs e) { if (messagebox.show("are sure want execute program?", "confirm start", messageboxbuttons.yesno, messageboxicon.question) != dialogresult.no) { string connectionstring = getconnectionstring(usernamepicker.text, passwordpicker.text); using (sqlconnection connection = new sqlconnection(connectionstring)) { using (sqlcommand cmd = new sqlcommand("dbo.procedurename", connection)) { connection.open(); cmd.commandtext = "dbo.procedurename"; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@date", sqldbtype.varchar).value = datetimepicker1.text; sqlparameter result = cmd.parameters.add("@result", sqldbtype.varchar); result.direction = parameterdirection.returnvalue; cmd.executescalar(); var resultout = (string)cmd.parameters["@result"].value; connection.close(); textmessage.text = datetimepicker1.text; } } } } all result 0 or null or etc. tried return value sql print, return, set, output ....... nothing seems work. fetching variable c# sql seems child-work. ideas?
if want return concatenate string output @ end of procedure write select @result. make sure have concatenated before statement.
this return string can use in c# code string.
Comments
Post a Comment