我在 asp.net 和 SQL Server 2005 中工作。
我想知道如何从存储过程中返回一个值,或者我们如何知道查询是否已在存储过程中成功执行。例如,我将一些数据插入到 SQL Server 数据库中,我想通过使用返回值来检查它是否正确插入。这里是代码
存储过程..
ALTER PROCEDURE [dbo].[practice]
-- Add the parameters for the stored procedure here
@thename varchar(50),
@thedate datetime,
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into tblpractice (name,date) values (@thename,@thedate)
END
代码隐藏..
string name=txtname.Text.Trim();
DateTime date = Convert.ToDateTime(txtdate.Text);
try
{
SqlCommand cmd = new SqlCommand("practice", lcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@thename", name);
cmd.Parameters.AddWithValue("@thedate", date);
lcon.Open();
cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
if (lcon != null)
{
lcon.Close();
}
}