Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have the following stored procedure with parameters:
USE [DBRapBreaza]
   GO
   /****** Object:  StoredProcedure [dbo].[PS_SpauOPompa]    Script Date: 1/10/2019 10:01:13 ******/
   SET ANSI_NULLS ON
   GO
   SET QUOTED_IDENTIFIER ON
   GO

   ALTER PROCEDURE [dbo].[PS_SpauOPompa]

   @DataStart datetime,
   @DataStop datetime,
   @val int
   AS
   BEGIN
   SET NOCOUNT ON;

   declare @sql NVARCHAR(max)
   declare @col1 varchar (25)
   declare @col2 varchar(25)
   declare @col3 varchar (25)
   declare @col4 varchar (25)
   declare @col5 varchar (25)
   declare @col6 varchar (25)

   set @col1='ID'
   set @col2='BREAZA_SPAU'+CONVERT(VARCHAR, @val)+'_EA'
   set @col3='BREAZA_SPAU'+CONVERT(VARCHAR, @val)+'_EQ'
   set @col4='BREAZA_SPAU'+CONVERT(VARCHAR, @val)+'_ore1'
   set @col5='BREAZA_SPAU'+CONVERT(VARCHAR, @val)+'_nivel'
   set @col6='DateTime'

   set @sql= 'select [ID], ' + @col2 + ',' + @col3 + ',' + @col4 + ',' + @col5 + ',' + @col6 + ' FROM [DBRapBreaza].[dbo].[TblSPAU'+CONVERT(VARCHAR, @val)+ '] WHERE DateTime between '''+CONVERT(VARCHAR(25), @DataStart,121)+ ''' and '''+CONVERT(VARCHAR(25), @DataStop,121)+''';'

   print @sql;


   EXEC sp_executesql
       @sql,
       N'@DataStart datetime, @DataStop datetime, @val int',
       @DataStart, @DataStop, @val;
   END
 --execute [PS_SpauOPompa] '2018-12-13 15:58:46.940', '2018-12-13 16:31:39.243', 40

When I execute it in Microsoft SQL Server Management all is fine. The result of executing the stored procedure is:
Untitled — imgbb.com[^]
I want to get this results in C# by using DataSet item from Visual Studio. When I select the stored procedure, none of the columns is displayed in Data Column (see the below image). vs — imgbb.com[^]
What should I do so to get these columns in C#? Thanks in advance!

What I have tried:

See above the code I have done so far.
Posted
Updated 10-Jan-19 9:22am
Comments
F-ES Sitecore 10-Jan-19 8:14am    
You'll probably need to set the columns up manually if possible.
DrgIonuţ 10-Jan-19 8:18am    
What do you mean by "set the columns up manually"?
MadMyche 10-Jan-19 9:51am    
Can you please post the C# code calling this stored procedure?

See examples here: sql server - How to execute a stored procedure within C# program - Stack Overflow[^]
There is a DataSet example at the bottom.
 
Share this answer
 
v2
Comments
DrgIonuţ 11-Jan-19 3:06am    
Thank you for your response, but my problem is that I don't understand why I can't see the result of stored procedure in C#. If I use a normal select query, I can see the table's columns in DataSet. But when I use select in the @sql string and execute it with "EXEC sp_executesql @sql", I don't get any column in DataSet.
RickZeeland 11-Jan-19 4:25am    
Are you sure you are using cmd.CommandType = CommandType.StoredProcedure; ?
Otherwise please post your C# code.
If I understand your question correctly, you don't have any C# code yet. If that is the situation, then the easiest way is to use SqlDataAdapter Class (System.Data.SqlClient) | Microsoft Docs[^] to populate a DataTable with the results from the Stored procedure.

For examples about the code, have a look at c# - How to Populate a DataTable from a Stored Procedure - Stack Overflow[^]
 
Share this answer
 
Comments
DrgIonuţ 11-Jan-19 3:06am    
Thank you for your response, but my problem is that I don't understand why I can't see the result of stored procedure in C#. If I use a normal select query, I can see the table's columns in DataSet. But when I use select in the @sql string and execute it with "EXEC sp_executesql @sql", I don't get any column in DataSet.
Wendelius 11-Jan-19 13:38pm    
Please modify the question and include the code in C#. Without seeing it we're merely guessing what could be wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900