C # Call stored procedures to implement the login interface detailed _c# tutorial

Source: Internet
Author: User

1, create the stored procedure

Copy Code code as follows:

Create proc Pro_login
(
@UserName nvarchar (10),
@PassWord nvarchar (10)
)
As
SELECT * FROM [User] username= @UserName and password= @PassWord

2, through the class is the implementation of the configuration database string connection
Copy Code code as follows:

Class ConnectionString
{
public static string constr = "Data source=mylove-pc;initial catalog=data;integrated security=true";
}

3, to achieve the login function
Copy Code code as follows:

#region
Connection Database Configuration string
using (SqlConnection con = new SqlConnection (CONNECTIONSTRING.CONSTR))
{
Con. Open ()//Opens the database
Calling stored procedures
using (SqlCommand cmd = new SqlCommand ("Pro_login", con)
{
Passing the value of a text box as an argument to a stored procedure
Cmd. Parameters.Add ("@UserName", SqlDbType.VarChar, 10). Value = TextBox1.Text.Trim ();
Cmd. Parameters.Add ("@PassWord", SqlDbType.VarChar, 10). Value = TextBox2.Text.Trim ();
Executed in the way stored procedures
Cmd.commandtype = CommandType.StoredProcedure;
Start reading data
using (SqlDataReader dr = cmd. ExecuteReader ())
{
If you read the username and password, switch to the interface Form2
if (Dr. Read ())
{
This. Hide ();
Form2 F2 = new Form2 ();
F2. Show ();
}
Otherwise, prompt for error
Else
{
MessageBox.Show ("Username or password error", "Please Re-enter", 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.