asp.net (C #) +sql Server three-tier architecture data access Scheme (ii)

Source: Internet
Author: User
asp.net|server| Schema | data stored procedures:

/**********************************

Function: Read function record according to certain condition

Author: rexsp

Date Created: 2004-01-13

Modified by:

Date Modified:

**********************************/

ALTER PROCEDURE getscoresetting

(

@ScoreSettingID Int=-1,---set ID

@FunctionID Int=-1,---feature ID

@OperationID Int=-1,---action ID

@RoleTypeID int=-1,---role type

@BBSTypeID int=-1,---Type of forum

@Score Int=-1,---integral setting

@BB int=-1,---currency settings

@BuyType int=-1,---Purchase type 0: not Purchase type 1: One-time purchase 2: Repeat purchase

@FunctionState Int=-1---functional status

)

As

SET NOCOUNT on

DECLARE @strSQL NVARCHAR (1000)

SET @strSQL = ' SELECT * from [scoresetting] WHERE @ckScoreSettingID = @ckScoreSettingID '

---Add KeyWords Begin---

IF @ScoreSettingID <>-1

BEGIN

SET @strSQL = @strSQL + ' and scoresettingid= @ckScoreSettingID '

End

IF @FunctionID <>-1

BEGIN

SET @strSQL = @strSQL + ' and functionid= @ckFunctionID '

End

IF @OperationID <>-1

BEGIN

SET @strSQL = @strSQL + ' and OperationID = @ckOperationID '

End

IF @RoleTypeID <>-1

BEGIN

SET @strSQL = @strSQL + ' and Roletypeid = @ckRoleTypeID '

End

IF @BBSTypeID <>-1

BEGIN

SET @strSQL = @strSQL + ' and Bbstypeid = @ckBBSTypeID '

End

IF @Score <>-1

BEGIN

SET @strSQL = @strSQL + ' and Score = @ckScore '

End

IF @BB <>-1

BEGIN

SET @strSQL = @strSQL + ' and bb= @ckBB '

End

IF @BuyType <>-1

BEGIN

SET @strSQL = @strSQL + ' and buytype= @ckBuyType '

End

IF @FunctionState <>-1

BEGIN

SET @strSQL = @strSQL + ' and functionstate= @ckFunctionState '

End

---Add Where Key Word---

---Run SQL Begin---

EXECUTE sp_executesql @strSQL,

N ' @ckScoreSettingID INT,

@ckFunctionID INT,

@ckOperationID INT,

@ckRoleTypeID INT,

@ckBBSTypeID INT,

@ckScore INT,

@ckBB INT,

@ckBuyType INT,

@ckFunctionState INT ',

@ckScoreSettingID = @ScoreSettingID,

@ckFunctionID = @FunctionID,

@ckOperationID = @OperationID,

@ckRoleTypeID = @RoleTypeID,

@ckBBSTypeID = @BBSTypeID,

@ckScore = @Score,

@ckBB = @BB,

@ckBuyType = @BuyType,

@ckFunctionState = @FunctionState

---Run SQL End---



A little note:

This stored procedure dynamically creates a query statement based on whether the class of the data layer passes the corresponding parameter value, and then executes the SQL statement with the system's own stored procedure, and the advantage of executing the SQL statement with the system stored procedure is that the character can be automatically escaped. The benefits of dynamically creating query statements are very large, this will save us to write a lot of conditions to judge, especially for those who have a lot of tables, a combination of the situation is too much, and the use of stored procedures to create SQL statements dynamically created by the number of judgments and the number of fields is basically the same, here will give the parameters of the initial value, If it is not equal to the initial value, the data-tier class has passed the argument in, so that the query condition of the corresponding conditional character is added.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.