1. Create a stored procedure
Copy codeThe Code is as follows: create proc Pro_Login
(
@ UserName nvarchar (10 ),
@ PassWord nvarchar (10)
)
As
Select * from [User] UserName = @ UserName and PassWord = @ PassWord
2. Configure the database string connection through the classCopy codeThe Code is as follows: class ConnectionString
{
Public static string conStr = "Data Source = MyLove-PC; Initial Catalog = data; Integrated Security = True ";
}
3. Implement the logon FunctionCopy codeThe Code is as follows: # region
// Connection database configuration string
Using (SqlConnection con = new SqlConnection (ConnectionString. conStr ))
{
Con. Open (); // Open the database
// Call the Stored Procedure
Using (SqlCommand cmd = new SqlCommand ("Pro_Login", con ))
{
// Pass the value of the text box as a parameter to the stored procedure
Cmd. Parameters. Add ("@ UserName", SqlDbType. VarChar, 10). Value = textBox1.Text. Trim ();
Cmd. Parameters. Add ("@ PassWord", SqlDbType. VarChar, 10). Value = textBox2.Text. Trim ();
// Run the Stored Procedure
Cmd. CommandType = CommandType. StoredProcedure;
// Start reading data
Using (SqlDataReader dr = cmd. ExecuteReader ())
{
// If you have read the user name and password, go to the Form2 page.
If (dr. Read ())
{
This. Hide ();
Form2 f2 = new Form2 ();
F2.Show ();
}
// Otherwise, an error is prompted.
Else
{
MessageBox. Show ("incorrect user name or password", "Enter again", MessageBoxButtons. OK );
TextBox1.Clear ();
TextBox2.Clear ();
TextBox1.Focus ();
}
}
}
}
# Endregion
4. Interface Test