Use the nhib.pdf sample query and the nhib.pdf sample
The most common example query is a combined query. We often need to provide several query options on the interface, and then return the results that meet the conditions according to the user input.
Complex conditions are often involved when processing code directly. The combination of conditions is not determined, resulting in complicated logic judgment statement structure. Multiple optional parameters may become more serious.
You can use the example query to easily solve this problem.
During the query, the collected query conditions are assigned to an object attribute. Of course, the object type is the object to be queried.
For example, if there is a User type in nhib.pdf, We need to query its name and password in combination. the User is defined as follows:
Namespace Demo. Dao. Domain
{
// User object
Public class User
{
Public virtual int UserId {set; get ;}
Public virtual string Username {set; get ;}
Public virtual string Password {set; get ;}
Public virtual Address {set; get ;}
}
}
In the interface, we provide user input boxes.
<P>
<Label for = "<% = this. tbxName %>">
Username: </label>
<Asp: TextBox ID = "tbxName" runat = "server"> </asp: TextBox>
</P>
<P>
<Label for = "<% = this. tbxPassword %>">
Password: </label>
<Asp: TextBox ID = "tbxPassword" runat = "server"> </asp: TextBox>
</P>
<P>
<Asp: Button ID = "btnSearch" runat = "server" Text = "query" OnClick = "btnSearch_Click"/>
</P>
In the button clicking event, we combine user input into a sample object. Then, submit it to nhib.pdf for query.
protected void btnSearch_Click(object sender, EventArgs e)
{
if (this.IsValid)
{
Demo.Dao.Domain.User example
= new Demo.Dao.Domain.User()
{
Username = this.tbxName.Text,
Password = this.tbxPassword.Text
};
IList<Demo.Dao.Domain.User> list = this.UserService.GetUsers(example);
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
In the background service, we use the example query for processing.
The sample Query type is defined in the namespace nhib.pdf. Criterion. You can use the static Create method to Create a sample object.
First, Create a query condition object, and then use the Create method of Example to Create a query Example object. The last step is the actual query.
Public IList <Demo. Dao. Domain. User> GetUsers (Demo. Dao. Domain. User exampleUser)
{
// Create a query Standard
Global: nhib.pdf. ICriteria criteria
= This. Session. CreateCriteria <Demo. Dao. Domain. User> ();
// Create a query example
Global: nhib.pdf. Criterion. Example example
= Global: nhib.pdf. Criterion. Example. Create (exampleUser );
// Set the query conditions for the sample object
Criteria. Add (example );
Example. EnableLike (global: nhib.pdf. Criterion. MatchMode. Anywhere );
IList <Demo. Dao. Domain. User> list = criteria. List <Demo. Dao. Domain. User> ();
Return list;
}
For string-type conditions, exact match is used by default. However, you can use the EnableLike example to set the condition. Nhib.pdf. Criterion. MatchMode is an enumeration that supports the following matching modes.
// The default string is completely matched and can be set through EnableLike
// Anywhere indicates matching at any position
// End appears at the last position
// Start appears at the starting position
// Exact match, set by default
Example. EnableLike (global: nhib.pdf. Criterion. MatchMode. Anywhere );
When a is input in the name input box, the SQL statement generated by nhib.pdf is as follows:
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (this_.Username like @p0)
In addition to the powerful EnableLike, it also provides several set parameters.
ExcludeZeroes() Method, which can exclude the attribute with a value of 0. For example, adding an integer property of Age to a User also results in the following SQL statement:
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_,
this_.Age as Age0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (this_.Username like @p0 and this_.Age = @p1)
Use the settings with exclusion 0
// Attribute whose exclusion value is 0
Example. ExcludeZeroes ();
Now SQL becomes
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_,
this_.Age as Age0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (this_.Username like @p0)
However, you need to be careful with two other settings. The ExcludeNulls () method seems to be an attribute whose exclusion value is null. However, in the Code, the ToString () method is called first, and then the length of the string is viewed again, therefore, the previous ExcludeZeroes () will become invalid.
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_,
this_.Age as Age0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (this_.Username like @p0 and this_.Age = @p1)
The ExcludeNone () method indicates that all attributes of the sample object must be used, which also invalidates ExcludeZeroes.
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_,
this_.Age as Age0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (this_.Username like @p0 and this_.Password is null and this_.Age = @p1)
Finally, there is an IgnoreCase () method that compares strings using case-insensitive methods, which is effective for Oracle and unavailable for SQL Server.
SELECT this_.UserId as UserId0_0_, this_.Username as Username0_0_, this_.Password as Password0_0_,
this_.Age as Age0_0_, this_.AddressId as AddressId0_0_
FROM tbl_Users this_
WHERE (lower(this_.Username) like lower(@p0) and this_.Age = @p1)