c# 저장 프로세스 이름과 저장 프로세스 매개 변수 그룹에 따라 대응하는 저장 프로세스를 실행하는 방법을 정의합니다
14821 단어 저장 프로세스
저장 프로세스 이름과 저장 프로세스 매개 변수 그룹에 따라 대응하는 저장 프로세스를 실행하는 방법을 정의합니다.저장 프로세스에 필요한 매개변수를 SqlParameter[]로 대체합니다.이렇게 하면 모든 저장 프로세스에 대해 방법을 쓸 필요가 없다
1. 먼저 ExcuteProcedure() 방법을 정의하여 저장 프로세스를 실행하고 첫 줄의 첫 번째 열의 결과를 되돌려줍니다
public static object ExcuteProcedure(string proName, SqlParameter[] paramt)
{
using (SqlCommand command = new SqlCommand())
{
object returnInfo = string.Empty;
command.Connection = conn; // SqlConnention
command.CommandText = proName;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(paramt);
command.Connection.Open();
try
{
returnInfo = command.ExecuteScalar();
}
catch
{
//return "False";
}
finally
{
command.Connection.Close();
}
return returnInfo;
}
}
2. 방법을 하나 더 정의하여 저장 프로세스를 실행하고 결과 집합을 되돌려줍니다
public static DataSet ExcuteProcedureDS(string proName, SqlParameter[] paramt)
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = conn;
command.CommandText = proName;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(paramt);
command.Connection.Open();
using (SqlDataAdapter sda = new SqlDataAdapter())
{
DataSet ds = new DataSet();
sda.SelectCommand = command;
try
{
sda.Fill(ds);
return ds;
}
catch
{
return null;
}
finally
{
conn.Close();
ds.Dispose();
}
}
}
}
3. 그 다음에 저장 프로세스 이름과 SqlParameter[] 그룹을 전달하고 이 두 가지 방법을 호출하여 저장 프로세스를 실행할 수 있다.
예를 들어 이런 저장 프로세스가 있다. 이 저장 프로세스는 다섯 개의 매개 변수가 있다.
USE [mydb]
GO
/****** Object: StoredProcedure [dbo].[ProUpUserList] Script Date: 03/30/2014 12:30:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- : < >
-- : <tracine>
-- : <2014/3/30>
-- : < >
-- =============================================
ALTER proc [dbo].[ProUpUserList]
(
@Name nvarchar(50),
@UserName nvarchar(50),
@UserPassword nvarchar(50),
@DeptName nvarchar(50),
@RoleName nvarchar(50)
)
as
declare @count int
select @count=COUNT(*) from UserList where UserName=@UserName
if @count=0
begin
select ' ' as 'result'
end
else
begin
begin tran Up
declare @DeptID int,@RoleID int
select @DeptID=DepartID from DepartmentList where DepartmentName=@DeptName
select @RoleID=RoleID from RoleList where RoleName=@RoleName
update UserList set Name=@Name,UserName=@UserName,DepartID=@DeptID,RoleID=@RoleID,UserPassword=@UserPassword
where UserName=@UserName
if @@error<>0
begin
rollback tran Up
select ' ' as 'result'
end
else
begin
commit tran Up
select ' ' as 'result'
end
end
실행 스토리지 프로세스 코드를 호출하려면 다음과 같이 하십시오.
SqlParameter[] parameter = new SqlParameter[]
{
// 5
new SqlParameter("@Name",SqlDbType.NVarChar,50),
new SqlParameter("@UserName",SqlDbType.NVarChar,50),
new SqlParameter("@UserPassword",SqlDbType.NVarChar,50),
new SqlParameter("@DeptName",SqlDbType.NVarChar,50),
new SqlParameter("@RoleName",SqlDbType.NVarChar,50),
};
// 5
parameter[0].Value = name;
parameter[1].Value = userName;
parameter[2].Value = name;
parameter[3].Value = departName;
parameter[4].Value = roleName;
//
string result = SQLdbHelper.ExcuteProcedure("ProUpUserList", parameter).ToString();
다만 이런 방법은 웹서비스와 응용하기에 적합하지 않다. 왜냐하면 SqlParameter 형식을 지원하지 않기 때문에 파라미터 전달을 할 수 없다