我有一个包含3列的SQL表:ID(bigint),Tag1(int),DateTime(datetime)。我已经创建了一个存储过程,该存储过程从表中选择从DataStart到DataStop的Tag1的值。它工作正常。代码:
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'
存储的proc的执行正确返回5个值。
我想在C#中获得这5个值。我尝试过的:
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");
}
当我按下按钮时没有错误,但是MessageBox中没有显示任何值。我不确定是否应该将@Col参数用作输出。我该怎么做才能获得这些值?
[如果有人遇到与我相同的问题,则可以通过以下方式解决:
我的存储过程:
ALTER PROCEDURE [dbo].[PS_TagLogging]
-- Add the parameters for the stored procedure here
@DataStart datetime=null,
@DataStop datetime=null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT [ID]
,[Tag1]
,[DateTime]
FROM [TRTF_TagLogging].[dbo].[tbl_TagLogging]
WHERE DateTime between @DataStart and @DataStop
END
在C#中,单击按钮时的操作:
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("@DataStart", SqlDbType.DateTime).Value = new DateTime(2020, 02, 05, 13, 06, 30, 697);
cmd.Parameters.Add("@DataStop", SqlDbType.DateTime).Value = new DateTime(2020, 02, 05, 13, 12, 25, 703);//2020, 02, 05, 13, 12, 25, 703 //2020, 02, 05, 13, 06, 50, 700
var values = new List<int>();
var valData = new List<DateTime>();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
values.Add(reader.GetInt64(0));//reads the value of my first column (ID) from DB
values.Add(reader.GetInt32(1));//reads the value of my second column (Tag1) from DB
valData.Add(reader.GetDateTime(2));//reads the date from my third column of DB
}
strCol = String.Join(" ", values);
strDate = String.Join(" ", valData);
connection.Close();
}
}
//MessageBox.Show("Proc: " + strCol + " values");
MessageBox.Show("Date: "+ strDate);
}