例如在向数据库添加新数据时,需要检测是否有重复
本例介绍如何把这个检测的过程放在存储过程中,并用程序调用检测的结果做出反应。
存储过程如下:
在C#中调用本存储过程的代码如下:
本例介绍如何把这个检测的过程放在存储过程中,并用程序调用检测的结果做出反应。
存储过程如下:
CREATE
PROCEDURE
DInstitute_Insert
@InstituteNO nvarchar ( 6 ), @InstituteName nvarchar ( 40 )
AS
declare @return int , @count int
if ( ltrim ( rtrim ( @InstituteName )) = '' or ltrim ( rtrim ( @InstituteNO )) = '' )
select @return = 3 -- 返回3表示提交的数据有空值
else
begin
select @count = count ( 1 ) from DInstitute where InstituteNO = @InstituteNO
if ( @count > 0 )
select @return = 1 -- 返回1表示编号有重复
else
begin
insert into DInstitute (InstituteNO,InstituteName) values ( @InstituteNO , @InstituteName )
if ( @@error > 0 )
select @return = 2 -- 返回2表示数据操作错误
else
select @return = 0 -- 返回0表示数据操作成功
end
end
return @return
GO
其中DInstitute 是一个学院信息表。只有InstituteNO(学院编号)、InstituteName(学院名称)两个字段。
@InstituteNO nvarchar ( 6 ), @InstituteName nvarchar ( 40 )
AS
declare @return int , @count int
if ( ltrim ( rtrim ( @InstituteName )) = '' or ltrim ( rtrim ( @InstituteNO )) = '' )
select @return = 3 -- 返回3表示提交的数据有空值
else
begin
select @count = count ( 1 ) from DInstitute where InstituteNO = @InstituteNO
if ( @count > 0 )
select @return = 1 -- 返回1表示编号有重复
else
begin
insert into DInstitute (InstituteNO,InstituteName) values ( @InstituteNO , @InstituteName )
if ( @@error > 0 )
select @return = 2 -- 返回2表示数据操作错误
else
select @return = 0 -- 返回0表示数据操作成功
end
end
return @return
GO
在C#中调用本存储过程的代码如下:
//
执行插入操作
SqlCommand com1 = new SqlCommand( " DInstitute_Insert " , DBcon);
if (com1.Connection.State == ConnectionState.Closed)
com1.Connection.Open();
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add( new SqlParameter( " @InstituteNO " ,SqlDbType.NVarChar, 6 ));
com1.Parameters.Add( new SqlParameter( " @InstituteName " , SqlDbType.NVarChar, 40 ));
com1.Parameters.Add( new SqlParameter( " @return " , SqlDbType.Int));
com1.Parameters[ " @return " ].Direction = ParameterDirection.ReturnValue;
com1.Parameters[ " @InstituteNO " ].Value = t_NO.Text;
com1.Parameters[ " @InstituteName " ].Value = t_name.Text;
try
{
com1.ExecuteScalar();
}
catch (SqlException ee)
{
DB.msgbox( " 操作失败! " + ee.Message.ToString());
return ;
}
finally
{
com1.Connection.Close();
}
string temp = com1.Parameters[ " @return " ].Value.ToString();
// 返回0表示数据操作成功
// 返回1表示编号有重复
// 返回2表示数据操作错误
// 返回3表示提交的数据有空值
switch (temp)
{
case " 0 " :
DB.msgbox( " 添加成功! " );
break ;
case " 1 " :
DB.msgbox( " 编号有重复! " );
break ;
case " 2 " :
DB.msgbox( " 数据操作错误! " );
break ;
case " 3 " :
DB.msgbox( " 提交的数据有空值! " );
break ;
}
Binding(); // 刷新datagrid
SqlCommand com1 = new SqlCommand( " DInstitute_Insert " , DBcon);
if (com1.Connection.State == ConnectionState.Closed)
com1.Connection.Open();
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add( new SqlParameter( " @InstituteNO " ,SqlDbType.NVarChar, 6 ));
com1.Parameters.Add( new SqlParameter( " @InstituteName " , SqlDbType.NVarChar, 40 ));
com1.Parameters.Add( new SqlParameter( " @return " , SqlDbType.Int));
com1.Parameters[ " @return " ].Direction = ParameterDirection.ReturnValue;
com1.Parameters[ " @InstituteNO " ].Value = t_NO.Text;
com1.Parameters[ " @InstituteName " ].Value = t_name.Text;
try
{
com1.ExecuteScalar();
}
catch (SqlException ee)
{
DB.msgbox( " 操作失败! " + ee.Message.ToString());
return ;
}
finally
{
com1.Connection.Close();
}
string temp = com1.Parameters[ " @return " ].Value.ToString();
// 返回0表示数据操作成功
// 返回1表示编号有重复
// 返回2表示数据操作错误
// 返回3表示提交的数据有空值
switch (temp)
{
case " 0 " :
DB.msgbox( " 添加成功! " );
break ;
case " 1 " :
DB.msgbox( " 编号有重复! " );
break ;
case " 2 " :
DB.msgbox( " 数据操作错误! " );
break ;
case " 3 " :
DB.msgbox( " 提交的数据有空值! " );
break ;
}
Binding(); // 刷新datagrid
Feedback
#1楼 回复 引用
2007-01-10 14:22 by PASS [未注册用户]
return 只能返回int型
可以用output参数
可以用output参数
#2楼 回复 引用
2007-05-24 17:01 by 過客 [未注册用户]
不建議用return 建議用output