R
Robert Phillips
I'm using a SqlDataReader to call a proc that reads data. My call to
raiseerror has no effect. Below is the code. if i comment out the select
statement, my code behaves as expected.
CREATE PROCEDURE [dbo].[usp_Table1Read]
@id int
AS
--select * from table1 where IId = @Id
if (@@rowcount = 0)
begin
raiserror('an error occured', 10, 1)
return 50000
end
return 0
GO
using System;
using System.Data;
using System.Data.SqlClient;
namespace ReaderTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.DoWork();
}
public void DoWork()
{
try
{
//connection
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Persist Security Info=False;Integrated
Security=SSPI;database=MyDataBase;server=(local)";
connection.Open();
connection.InfoMessage += new
SqlInfoMessageEventHandler(this.MySqlInfoHandler);
//command
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "usp_Table1Read";
command.Connection = connection;
//create return parameter
SqlParameter p = new SqlParameter();
p.ParameterName = "@RetVal";
p.Direction = ParameterDirection.ReturnValue;
p.SqlDbType = SqlDbType.Int;
command.Parameters.Add(p);
p = new SqlParameter();
p.ParameterName = "@Id";
p.Direction = ParameterDirection.Input;
p.SqlDbType = SqlDbType.Int;
p.Value = "3";
command.Parameters.Add(p);
//get reader
SqlDataReader reader = command.ExecuteReader();
//look at return parameter (1)
Console.Write("Check 1: ");
Console.WriteLine(command.Parameters["@RetVal"].Value);
while(reader.Read()){}
reader.Close();
connection.Close();
//look at return parameter (2)
Console.Write("Check 2: ");
Console.WriteLine(command.Parameters["@RetVal"].Value);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void MySqlInfoHandler(object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine("************************");
Console.WriteLine(e.Message);
Console.WriteLine("************************");
}
}
}
raiseerror has no effect. Below is the code. if i comment out the select
statement, my code behaves as expected.
CREATE PROCEDURE [dbo].[usp_Table1Read]
@id int
AS
--select * from table1 where IId = @Id
if (@@rowcount = 0)
begin
raiserror('an error occured', 10, 1)
return 50000
end
return 0
GO
using System;
using System.Data;
using System.Data.SqlClient;
namespace ReaderTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.DoWork();
}
public void DoWork()
{
try
{
//connection
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Persist Security Info=False;Integrated
Security=SSPI;database=MyDataBase;server=(local)";
connection.Open();
connection.InfoMessage += new
SqlInfoMessageEventHandler(this.MySqlInfoHandler);
//command
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "usp_Table1Read";
command.Connection = connection;
//create return parameter
SqlParameter p = new SqlParameter();
p.ParameterName = "@RetVal";
p.Direction = ParameterDirection.ReturnValue;
p.SqlDbType = SqlDbType.Int;
command.Parameters.Add(p);
p = new SqlParameter();
p.ParameterName = "@Id";
p.Direction = ParameterDirection.Input;
p.SqlDbType = SqlDbType.Int;
p.Value = "3";
command.Parameters.Add(p);
//get reader
SqlDataReader reader = command.ExecuteReader();
//look at return parameter (1)
Console.Write("Check 1: ");
Console.WriteLine(command.Parameters["@RetVal"].Value);
while(reader.Read()){}
reader.Close();
connection.Close();
//look at return parameter (2)
Console.Write("Check 2: ");
Console.WriteLine(command.Parameters["@RetVal"].Value);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void MySqlInfoHandler(object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine("************************");
Console.WriteLine(e.Message);
Console.WriteLine("************************");
}
}
}