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