I have a SQL table with 3 columns: ID (bigint), Tag1 (int), DateTime (datetime). I've made a stored procedure which selects from my table the values of Tag1 from DataStart to DataStop. It works fine. The code:
ALTER PROCEDURE [dbo].[PS_TagLogging]
-- Add the parameters for the stored procedure here
@Col varchar(30) Output,
@DataStart varchar(50) ,
@DataStop varchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql nvarchar(1000)
SET @sql = N'SELECT ' + @Col + ', DateTime ' + 'FROM [TRTF_TagLogging].[dbo].[tbl_TagLogging] WHERE (DateTime BETWEEN ' +''''+ @DataStart +'''' + ' AND ' +''''+ @DataStop+'''' +')'
exec (@sql)
PRINT @sql
END
execute [PS_TagLogging] '[Tag1]', '2020-02-05 13:06:30.697','2020-02-05 13:06:50.700'
The execution of the stored proc returns, correctly, 5 values.
I want to get those 5 values in C#. What I've tried:
private void DB_ProcStoc_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(@"Data Source=DESKTOP-JQSJAF8\SQLEXPRESS;Initial Catalog=TRTF_TagLogging;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("PS_TagLogging", connection))
{
connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Col", SqlDbType.VarChar, 30).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@DataStart", SqlDbType.VarChar, 30).Value = "2020-02-05 13:06:30.697";
cmd.Parameters.Add("@DataStop", SqlDbType.VarChar, 30).Value = "2020-02-05 13:06:50.700";
cmd.ExecuteNonQuery();
strCol = Convert.ToString(cmd.Parameters["@Col"].Value); //strCol is a string globally declared
connection.Close();
}
}
MessageBox.Show("Proc: " + strCol + " values");
}
I get no error when I press the button, but no value is displayed in MessageBox. I'm not sure if I should have used the @Col parameter as Output. What should I do in order to get those values?