ADO. NET creates a login case and ado.net creates a case
General idea.Based on the user name and password entered by the user, it is determined that the login is successful if it is the same as that stored in the database. Otherwise, the login fails.
Solution 1:
1. select * from table name where username = "user input username"
2. if reader exists. read (), that is, the user name exists, and then judge the password entered by the user, and the obtained password (reader. getString (reader. getOridinal ("password field") is the same. If the same is true, the logon succeeds. Otherwise, the logon fails.
Solution 2:
Select * from table name where username = "user input username" and password = "user input password". If data is found, the logon is successful. Otherwise, the logon fails.
Next, let's use solution 1 to create a logon case.
For convenience, use the console application.
Prelude:
I will write the connection string in the configuration file this time,
1. First, we need to add the namespace reference: System. Configuration;
2. Add the node information of the connection string under the <Configuration> node in our Configuration file 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 highlighted area is the node information of the connection string we added;
3. Then I used to create a DBHelper class and declare a method in it to obtain the connection string:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. configuration; // Add this reference to the project and add the namespace ADO in this class. NET login case 1 {public class DBHelper {public static string GetConnectionStrings () {// use the ConfigurationManager class to obtain the connection string information. Return ConfigurationManager. ConnectionStrings ["ConStr"]. ConnectionString ;}}}
4. This time I still use the stored procedure to create a stored procedure that queries by user name:
IF OBJECT_ID('Ins_User','P') IS NOT NULL DROP PROCEDURE Ins_User GO CREATE PROCEDURE Ins_User @name NVARCHAR(20) AS SELECT * FROM dbo.T_USERS WHERE T_NAME=@name GO
Stored Procedure
After preparing for the preliminary stage, let's start to write programs and code implementation:
Ideas:Solution 1: Let's talk about it. First, let the user enter the user name and password, and then query the table corresponding to the database based on the user name entered by the user. If there is no relevant data, the system prompts that the user name does not exist. If yes, it continues to judge whether the password entered by the user is correct (take the password entered by the user and the password corresponding to the database for determination). If yes, the system prompts that the logon is successful. Otherwise, the system prompts that the password is incorrect.
* Here I use parameterized queries to write logon cases 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 the user to enter the user name Console. writeLine ("Enter the User name:"); // use Console. readLine () receives user input string userName = Console. readLine (); // prompt the user to enter the password Console. writeLine ("Enter password:"); string password = Console. readLine (); // now you are using ADO. NET technology, to query the database // connection method access // 1. create a connection object (connection string) SqlConnection scon = new SqlConnection (DBHelper. getConnectionStrings (); // 2. create a command object (and set the attribute value for the command object) SqlCommand scmd = new SqlCommand (); scmd. commandText = "Ins_User"; scmd. commandType = CommandType. storedProcedure; scmd. connection = scon; // 3 open the Connection to scon. open (); // set the scmd parameter. parameters. add (new SqlParameter ("@ name", userName. trim (); // 4. run SqlDataReader reader = scmd. executeReader (CommandBehavior. closeConnection); // 5 process data if (reader. read () {if (password. trim (). toString () = reader ["T_PWD"]. toString () {Console. writeLine ("Logon successful");} else {Console. writeLine ("Password error") ;}} else {Console. writeLine ("the user name does not exist");} // when the reader is used up, you must disable reader. dispose (); Console. readKey ();}}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.