笔记(登录程序)

Posted on 2018-10-18 01:02  x1a  阅读(72)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace baoxianguanli
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}

private void button_login_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == " " || txtUserName.Text.Trim() == " ")
{
MessageBox.Show("用户名或密码不能为空");
txtUserName.Focus();
return;
}
string connString = @"Data Source=(local);Initial Catalog=date1; uid=sa;pwd=123";
string sqlstr = string.Format("select count(*) from [User] where UserName='{0}' and Password='{1}'",txtUserName.Text.Trim(),txtUserPassword.Text.Trim());
using (SqlConnection conn =new SqlConnection(connString))
{
SqlCommand cmd =new SqlCommand(sqlstr, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
MessageBox.Show("登陆成功" );

}
else
{
MessageBox.Show("用户名或密码错误,请重新输入!","错误");
txtUserName.Text= " ";
txtUserPassword.Text = " ";
txtUserName.Focus ();
}
}
}

private void txtUserPassword_TextChanged(object sender, EventArgs e)
{

}
}
}