C # Call the stored procedure to implement the logon Interface

Source: Internet
Author: User

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

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.