| |

VerySource

 Forgot password?
 Register
Search
View: 790|Reply: 5

SqlHelper does not get output when executing ExecuteReader

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-9 22:30:01
| Show all posts |Read mode
The database operation classes used by my program all use SqlHelper

My stored procedure stores an output parameter that returns the number of rows in the query, but after executing ExecuteReader, the value is null. I put
The Reader object is closed before it can be retrieved and served. What do I do with output after I close it? How do I get the output value before the Reader object is closed? Is it a bug or does it let you fetch it?

---------------------------------- The stored procedure is as follows ------------- --------------
---------------------------------- Tested stored procedures no problem ---------- ---------

CREATE PROCEDURE [dbo]. [SelectInitCar]
@ShowType bit, @Rowss smallint output
AS

If @ShowType = 1
Begin
     select Car.CarID, Car.CarName, Car.CarSim, Car.LogTime,
            OrienSet.Stake, OrienSet.MapStake, OrienSet.LonOrOffset, OrienSet.LatOrOffset
            From Car JOIN OrienSet ON Car.CarID = OrienSet.CarID
select @Rowss = @@ rowcount

End

Else
Begin
     select Car.CarID, Car.CarName, Car.CarSim, Car.LogTime,
            OrienSet.Stake, OrienSet.MapStake, OrienSet.LonOrOffset, OrienSet.LatOrOffset,
            CarGroup.GroupID, CarGroup.GroupName
            From CarGroup
        LEFT JOIN Car ON CarGroup.GroupID = Car.GroupID
            JOIN OrienSet ON Car.CarID = OrienSet.CarID
select @Rowss = @@ rowcount
End
-------------------------------------------------- -------------------------------------------------- ----------------------------------------------

----------------------code show as below-------------------------- -------------------------------------------------- --------------------------------------
 SqlParameter [] sps = new SqlParameter [2];
                sps [0] = new SqlParameter ("@ ShowType", SqlDbType.Bit);
                sps [0] .Direction = ParameterDirection.Input;

                sps [1] = new SqlParameter ("@ Rowss", SqlDbType.SmallInt);
                sps [1] .Direction = ParameterDirection.Output;

                SqlDataReader dr = (SqlDataReader) Communal.Ahelper.ExecuteReader (Communal.SqlStr, CommandType.StoredProcedure, "SelectInitCar", sps);
                
                //dr.Close ();
                    string xxxx = Convert.ToString (sps [1] .Value);
-------------------------------------------------- -----------------------

Who knows sqlhelper better, tell me to change there too! !! Please enlighten me why the value is not working when dr is on? How to solve it, thank you
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-12 12:45:01
| Show all posts
The return value must be in
It can only be obtained after dr.close(). . .
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-12 13:00:01
| Show all posts
Whether you use sqlhelper or yourself
Directly write statements that manipulate the database

Must be after dr is closed to get the return value
This is a mechanism problem
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-12 13:15:02
| Show all posts
Your parameter type may be wrong, this is my example, you try

/// <summary>
/// Add pusifang and return a new AttachID
/// </summary>
/// <param name="m_Attachmemt"></param>
/// <returns></returns>

internal int AttachmemtInsertPre(SMS.Attachmemt.Framework.Components.Attachmemt m_Attachmemt)
{
int result=0;

AttachmemtInsert(m_Attachmemt);

SqlParameter[] p1=
{
Agronet.API.Framework.Data.SqlHelper.MakeParam("@AttachID",SqlDbType.Int, 4,System.Data.ParameterDirection.ReturnValue,m_Attachmemt.AttachID)
//Note that the type is ReturnValue!!!!!!!!!
To
};
To
result=ReturnMaxPKID("CPP_SMS_getAttachMaxID",p1);
if(result<=0)
{
result=0;
}
To
return result;
//return DateHandleResult("SMS_AttachmemtInsert",p,false);

}

#region Back to the primary key pusifang
internal protected static int ReturnMaxPKID(string commandText, SqlParameter[] p)
{
To
object result = Agronet.API.Framework.Data.SqlHelper.ExecuteScalar(ConnectionString,System.Data.CommandType.StoredProcedure,commandText);
To
return int.Parse(result.ToString());
//return Agronet.API.Framework.Data.SqlHelper.ExecuteNonQuery(ConnectionString,CommandType.StoredProcedure,commandText,p);
To
}

The stored procedure is as follows:

----CPP_SMS_getAttachMaxID 0
ALTER Procedure [dbo].[CPP_SMS_getAttachMaxID]
- (
- --@MaxID int output
- ----@test int
-)

as
--set @MaxID= IDENT_CURRENT('Attachmemt')
----exec sp_executesql
--print @MaxID

select IDENT_CURRENT('Attachmemt')
return





GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-8-12 13:30:02
| Show all posts
Use the ExecuteNonQuery method
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-8-12 18:00:01
| Show all posts
ExecuteNonQuery? ? ? If you can channel it, I have to read it out. I don’t like using datasets, which is too slow. A little more data will not work. It's okay after Bangding!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list