C # simple movie record management system: developer 1 [user logon]

Source: Internet
Author: User
Tags parse string

Opening

In actual program development, we develop the most widely used data management systems. Of course, different systems have different functions and databases, but most importantly, the most common function is nothing more than basic data operations (add, delete, modify, and query ). this system development series records the entire development process of the simple movie record management system. The design and implementation of most function modules are attached, and most Code also contains annotations, it is very suitable for beginners of C # to learn and master.

 

Overview

In the first phase, let's take a look at how to use Visual Studio 2008 to create a project and implement a user login function.

 

So let's get started ~~~~

1. Create a project

 

2. Create a Windows form application)

 

3. Create the Winform and name it FrmLogin. cs.

Interface Design: first, drag a Button from the toolbar on the left to enter the account and password and then click log on to use ().

 

4. The label box is used to prompt the content to be entered in the Textbox of the text box.

 

5. Textbox is used to enter the account and password.

 

6. After the general interface is designed, We need to assign the name attribute name to the controls (as shown below)

Form name: FrmLogin. cs (previously named) username Text Box name: txtName Password text box name: txtPwd login button name: btnLogin

 

Then add a jump form name: FrmManager. cs after successful logon.

 

After the interface is designed, we need to create a database to access the user's account and password information.

1. Create a database

 

2. Set the database name to Movie and click OK to create the database.

 

3. After the database is created, create a new data table.

 

4. Set the Data Type of the column name (field name) and whether the field can be blank

The column names are respectively user ID: MId User name: MUserName User Password: MUserPwd

 

5. Set the primary key of the MId

 

6. After setting the primary key, set the Identity Specification in the following attributes.

 

7. Set the Is Identity in Identity Specification to Yes (so that the number of new data IDs for each inserted row Is automatically + 1 ).

 

8. Save the data table after it is set. The table name is MovieAccount.

 

9. After the data table is created, we need to manually insert the username and password required by the user to log on to the system.

 

10. manually insert the user name and password (of course, the user name and password can be written at will, as long as the name is meaningful and easy to remember ).

 

In this way, the database is created.

 

Next, set the logon form.

1. Because it is for database operations, first we need to add a configuration file App. config.

Right-click the project name and choose add> new item.

 

Select the application configuration file and use the default App. config name.

 

2. After adding the configuration file, we need to add the connection string name add name = "str" and the database connection string content connectionString = ".....".

<?xml version="1.0" encoding="utf-8" ?><configuration>  <connectionStrings>    <add name="str" connectionString="Data Source = .\SQLExpress2008; Initial Catalog=Movie;Integrated Security=True"/>   </connectionStrings></configuration>

Set the App. after config connects to the configuration file, we need to connect it in the login form code; here we need to use ConfigurationManager (which provides access to the client application configuration file ). the System does not use its namespace by default, so we need to parse it; before parsing, we need to add a System. configuration Assembly reference.

Figure 1

Find "Reference" under Solution Explorer, right-click "add reference ".

 

Figure 2

Find the. net item, select System. configuration, and click OK.

 

Figure 3

When System. configuration appears under the reference, it indicates that the reference has been added.

 

Figure 4

After adding a reference, we add a connection string in the code to connect to the configuration file.

// Connect to the configuration file App. config string connStr = ConfigurationManager. ConnectionStrings ["str"]. ConnectionString;

 

Add location as shown in Figure

 

Move the mouse over ConfigurationManager and right-click it. A resolution option is displayed;

Select using System. Configuration (use the Configuration namespace in the System ).

 

Of course, because the login form requires some database operation command namespaces, such as SqlConnection, which are not used by default, we also need to resolve it ourselves.

Similarly, right-click SqlConnection resolution and select using System. Data. SqlClient or System. Data. SqlClient. SqlConnection.

 

The complete code is as follows:

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. configuration; using System. data. sqlClient; /*************************************** ******************* Author: soFlash-blog Park http://www.cnblogs.com/longwu ** Description: FrmLogin. cs is used to implement the user login form ***************************** * ***********************/Namespace movie record management system {public partial class FrmLogin: form {public FrmLogin () {InitializeComponent ();} // first, connect to the configuration file to build the App. config // Of course, The ConfigurationManager system does not use its namespace by default. Therefore, you need to parse string connStr = ConfigurationManager. connectionStrings ["str"]. connectionString; private void btnLogin_Click (object sender, EventArgs e) {// use SqlConnection to connect to the database using (SqlConnection conn = New SqlConnection (connStr) {// create an SQL query statement string SQL = "select MUserPwd from MovieAccount where MUserName = '" + txtName. text + "'"; // create SqlCommand execution command using (SqlCommand cmd = new SqlCommand (SQL, conn) {// open the database connection conn. open (); // use SqlDataReader to read the database using (SqlDataReader sdr = cmd. executeReader () {// SqlDataReader reads if (sdr) One by one starting from 1st data records in the database. read () // If the account is Read successfully (the username in the text box exists in the Database) {// The 1st passwords are assigned to the string pwd, and all passwords are read later. // Trim () is used to remove the blank string pwd = sdr before and after the string. getString (0 ). trim (); // if the password entered in the text box = password in the database if (pwd = txtPwd. text) {// indicates that the password under this account is correct and MessageBox is successfully logged on to the system. show ("the system is successfully logged on. You are redirected to the home page... "); FrmManager manager = new FrmManager (); manager. show (); this. hide ();} else {// otherwise, the password is incorrect and enter MessageBox again. show ("Incorrect password! Enter it again! "); // And automatically clear the current password txtPwd. text = "" ;}} else {// If reading account data fails, the user name does not exist MessageBox. show ("the user name does not exist. Please try again! "); // And automatically clear the account name txtName. Text = "";}}}}}}}

Running Effect display

Figure 1

Figure 2

 

Figure 3

 

Log on to the management form page.

In this way, the login module of the movie record management system is ready.

 

Attach source code (including database files)

MovieRecordManagementSystem01.zip

Related Recommendations [click here to view the Directory]
  • C # simple movie record management system: Development 2 [add, delete, modify, and query]
  • C # simple movie record management system: Development 3 [Password modification]
  • C # simple movie record management system: Development 4 [log view]
  • C # simple movie record management system: developer 5 [User Registration]
  • C # simple movie record management system: Development 6 [data search]
  • C # simple movie record management system: Development 7 [User Classification]
  • C # simple movie record management system: Development 8 [data backup]
  • C # simple movie record management system: Development 9 [data recovery]
  • C # simple movie record management system: Conclusion [permission Assignment]

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.