次のストアド プロシージャがあります。
ALTER PROCEDURE [dbo].[ProcedureName]
@date NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @result nvarchar(500) -- this one should return string.
DECLARE @variable1 NVARCHAR(50)
set @variable1 = (SELECT COUNT(*) FROM dbo.Table1 WHERE column1 not in (select column1 from dbo.Table2))
DECLARE @variable2 NVARCHAR(50)
update dbo.Table1 set columnX = 1 where column1 not in (select column1 from dbo.Table2)
set @variable2 = @@ROWCOUNT
など...少なくとも10〜12の変数を持つ200行のスクリプトのように続きます
その後、このような結果を得たい
'Hello,' +
'Some Text here' +
@date +': ' +
'Explaining text for variable1- ' + @variable1 +
'Updated rows from variable2 - ' + @variable2 +
'some other select count - ' + @variable3 +
'some other update rowcount - '+ @variable4
......
今まで PRINT ステートメントでこれを取得できましたが、次のように C# コードの変数に入れることはできません。
public void Execute_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to execute the 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;
}
}
}
}
結果として得られるのはすべて0またはNULLなどです。PRINT、RETURN、SET、OUTPUT .......を使用してSQLから値を返そうとしましたが、何も機能していないようです。ただし、C# から SQL に変数をフェッチするのは面倒な作業のようです。何か案は?