general ideas. According to user input username and password, to judge, and the database stored is not the same, if the same sign of success, otherwise login failed.
Programme I:
1.select* from table name where username= "user entered user name"
2. If there is reader. Read (), that is, the user name exists, and then determine the user entered the password, and the password to be taken (reader. GetString (reader. Getoridinal ("password field")) is not the same, if the same log on successfully, otherwise login failed.
Programme II:
SELECT * FROM table name where username= "user entered user name" and password= "user entered password" and log in successfully if data is found. Otherwise, the login fails.
Now, let's use scenario one to do a login case.
Here, for convenience, use a console application.
Prelude:
I'm going to write the connection string in the configuration file this time,
1. First we want to add a reference to the namespace: system.configuration;
2. Then add the relevant node information for the connection string below the <Configuration> node in our profile appconfig.
<configuration>
<connectionStrings>
<add name= "Constr" connectionstring= server=.; Database=db_users;uid=sa;pwd=pasword_1 "/>
</connectionStrings>
<startup>
< Supportedruntime version= "v4.0" sku= ". netframework,version=v4.5 "/>
</startup>
</configuration>
The place marked red color is the connection string node information that we added;
3. Then I used to create a DBHelper class that declares a method inside to get the connection string:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
The using system.configuration;//adds this reference to the project and adds the namespace namespace ADO to the class
. NET Login Case 1
{public
class DBHelper
{public
static string getconnectionstrings ()
{
// Use the ConfigurationManager class to get information about the connection string. return
configurationmanager.connectionstrings["Constr"]. ConnectionString
}}
}
4. This time I'm still using the stored procedure to create a stored procedure that queries based on the user name:
IF object_id (' Ins_user ', ' P ') is not NULL
DROP PROCEDURE ins_user
go
CREATE PROCEDURE ins_user
@name NVARCHAR as
SELECT * FROM dbo. T_users WHERE t_name= @name Go
Stored Procedures
Early preparation work, after doing well, now we start to write programs, coding implementation:
idea: scheme One, said, first of course we are let user input, username and password, and then according to user input user name to query the database corresponding table, there is no relevant data, if not, the prompt user name does not exist, there are, To continue to judge the user entered the password is correct (take the user input password and database corresponding password, to judge), if correct, prompt login success, otherwise prompted password error.
* Here I use parameterized queries to write a login case to prevent SQL injection attacks.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Data;
Using System.Data.SqlClient; Namespace ADO. NET Login Case 1 {class program {static void Main (string[] args) {//Prompt user for User name Console.WriteLine ("Please enter a username
:");
Use Console.ReadLine () to receive information entered by the user string userName = Console.ReadLine ();
Prompts the user to enter the password Console.WriteLine ("Please enter the password:");
string password = Console.ReadLine (); Now is the beginning to use Ado.net technology to query the database//connection way to access//1. Create a Connection object (connection string) SqlConnection Scon = new SqlConnection (DBHelper.
Getconnectionstrings ());
2. Create a Command object (and set the property value for the Command object) SqlCommand scmd = new SqlCommand ();
Scmd.commandtext = "Ins_user";
Scmd.commandtype = CommandType.StoredProcedure; Scmd.
Connection = Scon; 3 Open Connection Scon.
Open (); Sets the parameter scmd.
Parameters.Add (New SqlParameter ("@name", Username.trim ()); 4. Execute command SqlDataReader reader = SCMd.
ExecuteReader (commandbehavior.closeconnection); 5 processing data if (reader. Read ()) {if (password). Trim (). ToString () = = reader["T_pwd"].
ToString ()) {Console.WriteLine ("Login succeeded");
else {Console.WriteLine ("Password Error");
} else {Console.WriteLine ("User name does not exist"); ///reader must be turned off when finished.
Dispose ();
Console.readkey ();
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.