5. Use these classes
Now you know most of activerecord. We will use these classes below. You will find that it is no better than this.
Create the first user
My applications generally have Logon Windows. You must enter the correct login name and password for verification to use properly. Even if a table user is created, how can we add a note?
The following code creates a user and adds it to the main function.
User user = new user( "admin", "123 ");
User. Create ();
What happens when you run the program multiple times? A better solution is to add a new user only when the user cannot be found in the database. We will check whether there are users in the database and the data can be inserted when the value is 0.
If (user. getuserscount () = 0)
{
User user = new user( "admin", "123 ");
User. Create ();
}
Obviously, we need to add a method getuserscount to the user class:
[Activerecord ("[user]")]
Public class user: activerecordbase <user>
{
...
Public static int getuserscount ()
{
Return count ();
}
}
Logon box
This window requires the user to enter the login information:
This code is very simple. You can use the search method to find the user. So far, there is no extension method:
Private void loginbutton_click (Object sender, system. eventargs E)
{
User user = user. findbyusername (logintext. Text );
If (user = NULL)
{
MessageBox. Show (this, "user not found", "error", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
If (user. Password! = Passwordtext. Text)
{
MessageBox. Show (this, "Wrong Password", "error", messageboxbuttons. OK, messageboxicon. Error );
Return;
}
Dialogresult = dialogresult. OK;
Hide ();
}
To complete this function, the findbyusername method is actually very simple:
Using nhib.pdf. criterion;
[Activerecord ("[user]")]
Public class user: activerecordbase <user>
{
...
Public static user findbyusername (string username)
{
// Note that we use the property name, _ not _ the column name
Return findone (expression. eq ("username", username ));
}
}
(The key is to understand findone (expression. eq ("username", username )))
Blog Management
In this window, you can add, modify, and delete a blog. It also provides an entry to the management bar announcement window for the selected blog.
When you click Add... or edit a blog, another window is displayed.
The following method selects all the blog classes from the bloglist: (displayed in the blog List)
Private void populatebloglist ()
{
Blogslist. Items. Clear ();
Foreach (blog in blog. findall ())
{
Listviewitem item = blogslist. Items. Add (blog. Id. tostring ());
Item. Tag = blog;
Item. subitems. Add (blog. Name );
Item. subitems. Add (blog. Author );
}
}
To add a new blog record to the database, we need to do the following:
Blog blog = new blog ();
Blog. Name = "my blog ";
Blog. Author = "Hammett ";
Blog. Create ()
If you do not know the blog instance, But you know its ID, we can modify the blog:
Blog blog = blog. Find (100); // ID that we know exists
Blog. Name = "different name ";
Blog. Author = "different author ";
Blog. Update ();
To delete an instance, you only need to call the delete method.
Manage the announcement board
We still haven't forgotten that this announcement topic depends on Blog instances. When we create this topic, we should pay attention to it:
Urrentpost. Blog = parentblog;
Currentpost. Title = titletext. text;
Currentpost. Contents = contentstext. text;
Currentpost. Category = categorytext. text;
Currentpost. Created = createddttime. value;
Currentpost. Published = publishedcheck. checked;
Currentpost. Save ();