1. Start service: SQL Server Configuration
2.windows authentication into the SQL server-security-sa-Password change;
3. New database Booksystem;
4.public Partial class Form1:form
{
SqlConnection Conn;
Public Form1 ()
{
InitializeComponent ();
}
private void Btnlogin_click (object sender, EventArgs e)
{
Try
{
Conn. Open ();
MessageBox.Show (Conn. State.tostring ());
}
catch (Exception ex)
{
Friendly Tips
MessageBox.Show (ex. message);
Throw ex;
}
finally {
Conn. Close ();
MessageBox.Show (Conn. State.tostring ());
}
}
private void Form1_Load (object sender, EventArgs e)
{
conn = new SqlConnection ("server=.; Database=booksystem;uid=sa;pwd=sa ");
}
}
5.sql
String sqlstr = "SELECT * from admin where admname= ' lyc ' and admpwd= ' 123 '";
Trim () Remove space on both sides
String sqlstr = @ "SELECT * FROM admin"
where admname= ' "+ this.txtName.Text.Trim () +" ' and admpwd= ' "+ this.txtPwd.Text.Trim () +" ' ";
cmd = new SqlCommand (SQLSTR, conn);
Conn. Open ();
Object Obj=cmd. ExecuteScalar ();
Try
{
//
Int32 count = (Int32) cmd. ExecuteScalar ();
int count = (int) cmd. ExecuteScalar ();
MessageBox.Show ("correct");
}
catch (Exception ex)
{
MessageBox.Show ("Incorrect username password");
Throw ex;
}
finally {
Conn. Close ();
}