开发环境
OS:Windows 7 64位
IDE:Microsoft VisualStudio 2010
数据库:SQL Server 2008 R2
最终效果
数据库
代码
Login.cs
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 Login
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void loginBtn_Click(object sender, EventArgs e)
{
string uername = name.Text.Trim();
string pwd = password.Text;
string constr = "Server=.;DataBase=Management;Integrated Security=True";
// 建立SqlConnection对象
SqlConnection con = new SqlConnection(constr);
// 打开连接
con.Open();
// 指定SQL语句
SqlCommand com = new SqlCommand("select account,password from register where account='" + uername + "' and password='" + pwd + "'", con);
// 建立SqlDataAdapter和DataSet对象
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
int n = da.Fill(ds, "register");
if (n != 0)
{
MessageBox.Show("登录成功!", "提示");
label_title.Visible = false;
label_userName.Visible = false;
label_userPwd.Visible = false;
name.Visible = false;
password.Visible = false;
button_reg.Visible = false;