ASP also to play three layers?

Source: Internet
Author: User
Tags insert key sql query string sort table name

Recently want to play ASP, Miss, think of in the ASP JavaScript script, js Some things seem more interesting. In fact, I wrote two simple classes, a dbhelper, a businessrules, not so clear.

There are actually only 5 methods in DBHelper:

Open (connectionString);//opens a data connection. ConnectionString: Database connection string (slightly, and slightly, the default connection string specified in the class is invoked).

Close ()//closes the data connection.

ExecuteNonQuery (SQL);//Execute SQL statement, [return affected rows] (the contents of the brackets are not implemented, there seems to be some problems).

ExecuteReader (SQL);//Returns a data recordset, which, of course, is disconnected from the data source.

Executescaler (SQL);//executes the SQL statement, returning data from the first column of the row.

BusinessRules also has only a few simple ways:

GetEntity (conditions);//Gets a single entity based on the specified query criteria.

Insert (entity, identity);//Inserts a single entity into the database. Identity: If the primary key column name is passed in, the primary key property of the entity after the data is inserted gets the corresponding primary key value in the database.

Update (entity, conditions)//updates a single entity to the database, conditions: Update the condition.

Delete (conditions);//Deletes data by specified criteria. (Parameters are slightly deleted all data)

GetList (num, conditions, sortOrder);//Get a list of data. Num: Specify the quantity, 0 to take all the data that meets the criteria. Conditions: Query criteria, SortOrder: Sort the way. (All parameters can be abbreviated)

Getpagelist (PageIndex, pageSize, conditions, sortOrder);//Get a list of paging data. Conditions: Query criteria (nullable), SortOrder: Sort by (required).

You can use it directly.

How to use it:

Suppose we have a comment table comment.

The following fields are available:

Id:int, self-add field.

Title:nvarchar, title.

Body:nvarchar, content.

Userid:int, the user ID of the comment.

State:bit, state.

Createtime:datetime, publishing time.

Sample code:


< @LANGUAGE = "JAVASCRIPT" codepage= "65001"%>
<%
var commentbll = new BusinessRules ("Comment");//Parameter Comment is the table name.
var entity = commentbll.getentity ("id = 1");//This gets a comment record with ID 1.
for (var x in entity)//prints out all the properties and values of the entity.
{
Response.Write (x + ":" + entity[x] + "<br>");
}
Of course JS has a number of ways to take value:
Response.Write ("Title:" + entity.) /Sorry is no intelligent perception, how to point also can not point to the attribute, hehe.
Response.Write ("UserID:" + entity["UserID"]);

Insert:
var comment = {
Title: "Javascript Demo",
Body: "Hello, robot!",
Userid:1,
State:false,
Createtime:new Date (). toLocaleString ()
}
if (Commentbll.insert (Comment, "ID"))
{
Response.Write ("success!" + comment.id);//The ID of the comment after inserting the data will get the value automatically incremented in the database.
}

Update: Continue with the above entity
With (comment)
{
ID = undefiend;//This is an automatically incremented column and cannot be assigned, so delete it.
Title = + "-->update";
state = true;
}
if (Commentbll.update (entity, "id = 1")//assume the ID to be updated is 1.
{
Response.Write ("success!");
}

Delete:
if (Commentbll.delete ("ID = 1"))//delete the comment with ID 1.
{
Response.Write ("success!");
}

Get a list of data:
var list = commentbll.getlist (0, "Body like% Blog Park%", "Createtime Desc");/get the list of data in the content that contains "blog Park".
for (var x = 0; x < List.length + +)/Print list.
{
Response.Write (x + "-->" + "Title:" + list[x]. Title + "<br>");
Response.Write (x + "-->" + "body:" + list[x]["body"] + "<br>");
}

Get a list of paging data:
var pagelist = commentbll.getpagelist (1, NULL, "Createtime Desc");//Get a list of all data on the first page of data.
The data returned here is one more RecordCount attribute than the data returned by GetList.
Response.Write ("The total number of eligible records:" + Pagelist.recordcount + "<br>");
Print data ditto, slightly ...
%>


Only to achieve a number of simple functions, the problem is also a lot of problems, but the interest came casually write, I compare Mars, there are few things, we do not blame. Oh. Getpagelist use is MSSQL2005 row_number () to pagination, not general, mssql2k/access change on the line.

Of course, can be more powerful, such as a few classes to map data tables/fields and so on, but there is no time to write so much, have time to do a simple project play can also. or some nostalgia for the simple ASP, although I asp comparison dishes.



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.