【解決方法】aspx ページでストアド プロシージャの文字列出力パラメータを取得する方法


こんにちは。
出力パラメーターとして文字列を返すストアド プロシージャがあります。 その文字列は
print ‘レコードが正常に挿入されました’
このメッセージを aspx ページのラベルに印刷したいと考えています。
これを行う方法。 どんな体でも助けてください。

私のストアドプロシージャは

ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50)
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
print 'Records are inserted successfully'
end

この印刷ステートメントを aspx ページに印刷したいと考えています。
私のaspx.csページは以下です

{
       DataSet ds = new DataSet();
       SqlParameter[] oparam = new SqlParameter[3];
       oparam[0] = new SqlParameter("@eid", Txteid.Text);
       oparam[1] = new SqlParameter("@ename", Txtename.Text);


       FU.SaveAs("E:\\BackUpWebsites\\sri\\images\\" + FU.FileName);
       Server.MapPath("images\\" + FU.FileName);

       oparam[2] = new SqlParameter("@image", "E:\\BackUpWebsites\\sri\\images\\" + FU.FileName );


       ds = BusinessLogic.InsertDetails(oparam );
       SqlParameter paramoutput=new SqlParameter ("@print", SqlDbType .NVarChar );
       paramoutput .Direction =ParameterDirection .Output ;
       paramoutput .Value =10;
       LblResult.Visible = true;
       LblResult.Text = paramoutput.Value.ToString();
   }

ありがとうございます。

解決策 2

やあ、

SQL の print ‘text statement’ は出力パラメーターとは見なされません。.NET 開発環境の Console.Write(‘Text’) のようなものです。

だから、あなたのspを変更してください:

SQL
ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50),
@outString nvarchar(100) output
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
SET @outString = 'Records are inserted successfully'
end

aspx:

public void InsertSP()
    {
        SqlConnection cnn = new SqlConnection("your connection string here");
        try
        {            
            SqlCommand cmd = new SqlCommand("sp_insert", cnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@eid", SqlDbType.Int);
            cmd.Parameters.Add("@ename", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@image", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@outString", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;
            cnn.Open();
            cmd.ExecuteNonQuery();
            LblResult.Visible = true;
            LblResult.Text = cmd.Parameters["@outString"].Value.ToString();

        }
        catch (Exception ex)
        {
            //Process error
        }
        finally
        {
            if (cnn != null && cnn.State == ConnectionState.Open)
            {
                cnn.Close();
            }
        }
    }

解決策 1

手順の変更

SQL
ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50),
@print nvarchar(50) output 
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
 select @print = 'Records are inserted successfully'
end

@print パラメータの値を取得するコードを実行します

コメント

タイトルとURLをコピーしました