Как отобразить @@ROWCOUNT
в методе после выполнения хранимой процедуры? Я хочу подсчитать, сколько строк затронуто после выполнения операторов INSERT
и UPDATE
:
CREATE PROCEDURE [dbo].[ADProcTemp]
@Username varchar(250),
@DisplayName varchar(70),
@isEnabled tinyint,
@PassNevExp tinyint,
@rowcount int OUT
AS
--SET NOCOUNT ON
BEGIN
IF NOT EXISTS (SELECT TOP 1 PrezimeIme FROM [dbo].[tblZaposleni_AD] WITH (NOLOCK)
WHERE NetworkLogin = @Username)
BEGIN
IF(@isEnabled = 1 OR @PassNevExp = 1)
INSERT INTO [dbo].[tblZaposleni_AD](NetworkLogin, PrezimeIme, Status, PassNevExp)
VALUES (@Username, @DisplayName, @isEnabled, @PassNevExp)
SET @rowcount = @@ROWCOUNT;
RETURN @@ROWCOUNT;
END
ELSE
BEGIN
UPDATE [dbo].[tblZaposleni_AD]
SET Status = @isEnabled,
PassNevExp = @PassNevExp
WHERE NetworkLogin = @Username
AND (Status <> @isEnabled OR [PassNevExp] <>@PassNevExp)
END
END
C# код:
public void ExcStrPrc(string Username, string DisplayName, bool isEnable, bool PassNevExp)
{
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("ADProcTemp", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", Username.ToString().Trim());
cmd.Parameters.AddWithValue("@DisplayName", DisplayName.ToString().Trim());
cmd.Parameters.AddWithValue("@isEnabled", Convert.ToInt32(isEnable));
cmd.Parameters.AddWithValue("@PassNevExp", Convert.ToInt32(PassNevExp));
//cmd.Parameters.AddWithValue("@rowcount", SqlDbType.Int);
SqlParameter rowsAffected = new SqlParameter("@rowcount", SqlDbType.Int);
rowsAffected.Direction = ParameterDirection.Output;
cmd.Parameters.Add(rowsAffected);
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
Console.WriteLine("Uspjesno insertovano ! ");
}
int result = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
}