1、创建表purchaseRq
CREATE TABLE [dbo].[purchaseRq](
[sId] [int] IDENTITY(1,1) NOT NULL,
[pName] [nvarchar](50) NULL,
[pSpecifications] [nvarchar](50) NULL,
[pUnit] [nvarchar](50) NULL,
[pCount] [int] NULL,
[pApplicants] [nvarchar](50) NULL,
[pDepartment] [nvarchar](50) NULL,
[pAppDate] [datetime] NULL,
[pReviewer] [nvarchar](50) NULL,
[pReDate] [datetime] NULL,
[pReStatus] [nvarchar](50) NULL,
[pRemark] [nvarchar](max) NULL,
CONSTRAINT [PK_purchaseRq] PRIMARY KEY CLUSTERED
(
[sId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
2、存储过程:proc_getPurchaseRq
use [ERP]
go
if (exists (select * from sys.objects where name = 'proc_getPurchaseRq'))
drop proc proc_getPurchaseRq
go
create proc proc_getPurchaseRq
@sId int, --默认输入参数
@pName varchar(50), --输入参数
@pSpecifications varchar(50),--输入参数
@pUnit varchar(50),--输入参数
@pCount int,--输入参数
@pApplicants varchar(50),
@pDepartment varchar(50),
@pAppDate datetime,
@pReviewer varchar(50),
@pReDate datetime,
@pReStatus varchar(50),
@pRemark varchar(Max)
as
if @sId=0 /*如果是0说明就当查询select*/
begin
select sId,pName,pSpecifications,pUnit,pCount,pApplicants,pDepartment,pAppDate,pReviewer,pReDate,pReStatus,pRemark from purchaseRq order by sId asc
end
else if @sId=-1/*如果是-1说明就当插入insert*/
begin
insert into purchaseRq values (@pName,@pSpecifications,@pUnit,@pCount,@pApplicants,@pDepartment,@pAppDate,@pReviewer,@pReDate,@pReStatus,@pRemark)
end
else /*如果是其他>0说明就当更新update*/
begin
update purchaseRq set pName=@pName,pSpecifications=@pSpecifications,pUnit=@pUnit,pCount=@pCount,pApplicants=@pApplicants,pDepartment=@pDepartment,pAppDate=@pAppDate,pReviewer=@pReviewer,pReDate=@pReDate,pReStatus=@pReStatus,pRemark=@pRemark where sId=@sId
end
go
3、后台调用存储过程
1)参数数组
SqlParameter[] Para = new SqlParameter[]{//传参数
new SqlParameter("@sId",SqlDbType.Int),
new SqlParameter("@pName",SqlDbType.VarChar),
new SqlParameter("@pSpecifications",SqlDbType.VarChar),
new SqlParameter("@pUnit",SqlDbType.VarChar),
new SqlParameter("@pCount",SqlDbType.Int),
new SqlParameter("@pApplicants",SqlDbType.VarChar),
new SqlParameter("@pDepartment",SqlDbType.VarChar),
new SqlParameter("@pAppDate",SqlDbType.DateTime),
new SqlParameter("@pReviewer",SqlDbType.VarChar),
new SqlParameter("@pReDate",SqlDbType.DateTime),
new SqlParameter("@pReStatus",SqlDbType.VarChar),
new SqlParameter("@pRemark",SqlDbType.VarChar),
};
2) 数组赋值
Para[0].Value = 0;//赋值
Para[1].Value = "1";
Para[2].Value = "1";
Para[3].Value = "1";
Para[4].Value = 8;//赋值
Para[5].Value = "大树";
Para[6].Value = "生产部";
Para[7].Value = DateTime.Now;
Para[8].Value = "树哥";
Para[9].Value = DateTime.Now;
Para[10].Value = "已通过";
Para[11].Value = "备注信息:请某某同学来一趟";
string connstring="数据库连接";
string strproc="getPurchaseRq";//存储过程名
SqlConnection sqlCon = new SqlConnection(connstring);//sql 连接
SqlCommand scmd = new SqlCommand(strproc, sqlCon);
scmd.CommandType = CommandType.StoredProcedure;//类型存储过程
SqlDataAdapter da = new SqlDataAdapter();
scmd.Parameters.AddRange(Para) ; //传递多个参数(数组)
da.SelectCommand = scmd;
DataSet ds = new DataSet(); //获得数据存入DataSet
da.Fill(ds);