c# - Function to display single returned value from SQL Server stored procedure -
i have sql server stored procedure takes no parameters. goes through database find commonly recorded screen resolution , outputs said resolution sole homecoming value.
the stored procedure takes 2 columns, screenwidth
, screenheight
(both int
) , concatenates them x
in between homecoming single text value screen resolution
here's stored procedure:
alter procedure [dbo].[uspretmostcommonscreenres] begin -- set nocount on added prevent result sets -- interfering select statements. set nocount on; -- insert statements procedure here select top 1 cast(screenwidth nvarchar(max)) + 'x' + cast(screenheight nvarchar(max)) resolutions browserinfoes grouping cast(screenwidth nvarchar(max)) + 'x' + cast(screenheight nvarchar(max)) order count(*) desc end
this executes absolutely fine , returns value 1600x1024
i want create code in c# sole value , store string value.
the experience have interfacing between c# , sql server inputting info through entity framework, have no knowledge on how i'd go task.
the pseudocode trying following
using(sqlconnection cnn = new sqlconnection(....here connection string ....)) using(sqlcommand cmd = new sqlcommand("uspretmostcommonscreenres", cnn)) { cnn.open(); cmd.commandtype = commandtype.storedprocedure; string result = cmd.executescalar().tostring(); }
this code requires connection string allows c# programme connect database. define sqlcommand pointing stored procedure , connection open connection, flag commandtext storedprocedure name , execute executescalar method.
the using statement takes care close , dispose connection , command
c# sql sql-server-2008 stored-procedures
No comments:
Post a Comment