1. 程式人生 > >【機房重構】——儲存過程

【機房重構】——儲存過程

 

D層

public DataTable GroupQuery(Entity.GroupQuery group)
        {
            SqlHelper sqlHelper = new SqlHelper();
            //儲存過程
            string sql = @"PROC_GroupQuery";
            SqlParameter[] sqlParams={
                                         new SqlParameter("@comboBox1",group.comboBox1),
                                         new SqlParameter("@comboBox2",group.comboBox2),
                                         new SqlParameter("@comboBox3",group.comboBox3),
                                         new SqlParameter("@comboBox4",group.comboBox4),
                                         new SqlParameter("@comboBox5",group.comboBox5),
                                         new SqlParameter("@comboBox6",group.comboBox6),
                                         new SqlParameter("@comboBox7",group.comboBox7),
                                         new SqlParameter("@comboBox8",group.comboBox8),
                                         new SqlParameter("@textBox1",group.textBox1),
                                         new SqlParameter("@textBox2",group.textBox2),
                                         new SqlParameter("@textBox3",group.textBox3),
                                         new SqlParameter("@GetDBName",group.GetDBName)
                                     };
            //儲存過程
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.StoredProcedure);
            return table;
        }

SQL

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<張文慧>
-- Create date: <2018-10-14>
-- Description:	<註冊時和資料庫的互動>
-- =============================================
ALTER PROCEDURE [dbo].[Proc_Register]

	-- Add the parameters for the stored procedure here 所需要的引數
	@cardNo varchar(20),
	@studentName varchar(20),
	@sex char(10),
	@department varchar(20),
	@grade varchar(50),
	@specialty varchar(50),
	@balance numeric(10,2),
	@UserID varchar(20),
	@PWD varchar(20),
	@RechargeCash numeric(10,2)
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- 新增記錄給Student表
    insert into Student(cardNo,studentName,sex,department,grade,specialty,balance,UserID,state,Ischeck)values(@cardNo,@studentName,@sex,@department,@grade,@specialty,@balance,@UserID,'True','False')
    
    --新增記錄給User_Info表
    insert into User_Info(UserID,PWD,Level,UserName,state)values(@cardNo,@PWD,'學生',@studentName,'True')
    
    --新增記錄給ReCharge表
    insert into ReCharge(cardNo,RechargeCash,Balance,UseID,Ischeck)values(@cardNo,@RechargeCash,@RechargeCash,@UserID,'False')
    
END
USE [charge_sys]
GO
/****** Object:  StoredProcedure [dbo].[PROC_GroupQuery]    Script Date: 2018/11/12 9:01:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		張文慧
-- Create date: 2018—11—8
-- Description:	組合查詢時候進行資料互動
-- =============================================
ALTER PROCEDURE [dbo].[PROC_GroupQuery]  
/****需要傳遞的引數****/
@cboFiled1 varchar(20),
@cboFiled2 varchar(20),
@cboFiled3 varchar(20),
@cboMark1 varchar(10),
@cboMark2 varchar(10),
@cboMark3 varchar(10),
@Content1 varchar(20),
@Content2 varchar(20),
@Content3 varchar(20),
@Relationship1 varchar(10),
@Relationship2 varchar(10),
@DbtableName varchar(20)
AS
	declare @TempSql varchar(500)
BEGIN
	SET @TempSql='SELECT * FROM '
[email protected]
+' WHERE '[email protected][email protected]+char(39)[email protected]+char(39) if(@Relationship1!='') BEGIN SET @[email protected][email protected]+CHAR(32)[email protected][email protected]+char(39)[email protected]+char(39) if(@Relationship2!='') BEGIN SET @[email protected][email protected]+CHAR(32)[email protected][email protected]+char(39)[email protected]+char(39) END END ExECUTE(@TempSql) END