当我们在使用存储过程时,如何去判断存储过程是否执行成功呢?
对于插入、修改、删除这几类对于数据库有影响的操作,当调用ExecuteNoQuery()方法时,我们可以通过它的返回值(返回数据库受影响的行数),即
Int EffectRows=aCommand.ExecuteNoQuery()是否不等于-1来做判断。
create proc InsertPerson
(
@Name varchar(20),
@Age int
)
as
Insert into Person ([Name],Age) values (@Name,@Age)
C#:
public void InsertPerson(string name,int age)
{
SqlCommand aCommand = new SqlCommand("InsertPerson", conn); //建立SQL命令对象
aCommand.CommandType = CommandType.StoredProcedure; //选择命令类型为存储过程
aCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar,20,"Name")); //添加参数
aCommand.Parameters.Add(new SqlParameter("@Age",SqlDbType.Int,0,"Age"));
aCommand.Parameters[0].Value = name; //给参数进行赋值
aCommand.Parameters[1].Value = age;
try
{
conn.Open();
int EffectRows=aCommand.ExecuteNonQuery(); //若要判断插入是否成功,可以在此写int EffectRows=aCommand.ExecuteNonQuery(),会返回影响的条数至EffectRows
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return EffectRows;
}
ReturnValue:参数表示存储过程的返回值。SQL Server 的存储过程参数列表中不显示该参数。它只与存储过程的 RETURN 语句中的值相关联。
create proc DeletePerson
(
@Name varchar(20)
)
as
delete from person where [Name]=@Name
return 100
用C#添加该参数:
public int DeletePerson(string name)
{
SqlCommand aCommand = new SqlCommand("DeletePerson", conn); //建立SQL命令对象
aCommand.CommandType = CommandType.StoredProcedure; //选择命令类型为存储过程
aCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 20, "Name")); //添加参数
aCommand.Parameters.Add(new SqlParameter("@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); //ReturnValue
aCommand.Parameters[0].Value = name;
try
{
conn.Open();
//int EffectedRows=aCommand.ExecuteNonQuery();
aCommand.ExecuteNonQuery();
int ReturnValue = Convert.ToInt32(aCommand.Parameters["@ReturnValue"].Value); //此时ReturnValue的值为存储过程中return 100中的100
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return ReturnValue;
}
Output:参数表示存储过程的输出参数。
存储过程:
create procedure InsertPersonWithOutput(@Name varchar(20),@Age int,@ID int output)
as
insert into person ([Name],Age) values (@Name,@Age)
select @ID=count(*) from person
C#:
public int InsertPersonWithOutput(string name, int age)
{
SqlCommand aCommand = new SqlCommand("InsertPersonWithOutput", conn); //建立SQL命令对象
aCommand.CommandType = CommandType.StoredProcedure; //选择命令类型为存储过程
aCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 20, "Name")); //添加参数
aCommand.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int, 0, "Age"));
aCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "ID", DataRowVersion.Default, null)); //添加一个输出参数
aCommand.Parameters[0].Value = name;
aCommand.Parameters[1].Value = age;
try
{
conn.Open();
aCommand.ExecuteNonQuery();
int newID = (int)aCommand.Parameters["@ID"].Value;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return newID;
}
ReturnValue与Output参数的区别:
output 类型的参数需要在存储过程的参数列表中添加。output类型的参数传入存储过程后还会返回其运行后的值。
returnvalue类型的参数不需要在存储过程的参数列表中添加。returnvalue类型的参数就是存储过程最后return的值。
当存储过程执行的结果是返回多条记录时,使用SqlDataReader或SalDataAdapter。
存储过程:
create procedure GetPerson(@Name varchar(20))
as
select * from person where [Name]=@Name
C#:
public List<Person> GetPerson(string name)
{
SqlDataReader sdr;
List<Person> list = new List<Person>(); //返回泛型
SqlCommand aCommand = new SqlCommand("GetPerson", conn);
aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 20, "Name"));
try
{
aCommand.Parameters[0].Value = name;
conn.Open();
sdr = aCommand.ExecuteReader(CommandBehavior.CloseConnection); //在关闭DataReader后自动关闭其对应的数据库连接
while (sdr.Read())
{
list.Add(new Person(sdr["Name"].ToString(), Convert.ToInt32(sdr["Age"].ToString())));
}
sdr.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return list;
}
C#:
public DataSet GetPersons(string name)
{
DataSet ds = new DataSet();
SqlDataAdapter aDataAdapter = new SqlDataAdapter("GetPerson", conn);
aDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
aDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 20, "Name"));
aDataAdapter.SelectCommand.Parameters[0].Value = name;
try
{
conn.Open();
aDataAdapter.Fill(ds);
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return ds;
}