真巧,我刚刚解决! 在你数据库用户表里面加多一 bit类型的标示符列 默认值为0,checkbox.checked==true触发为1 下面是我不严谨的写法,可以作为参考。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;namespace Mianbxs { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int i = 0; private void btnLogin_Click(object sender, EventArgs e) { bool isEmpty = CheckEmpty(); //判断用户和密码是否为空 if (isEmpty == true) //如果不为空 { Dbhelper conn = new Dbhelper(); string sql = "Select Count(*) from 用户 where 用户名='" + txtUserName.Text + "' and 密码='" + txtPassword.Text + "'"; SqlCommand cmd = new SqlCommand(sql, conn.cn); conn.cn.Open(); int n = (int)cmd.ExecuteScalar(); conn.cn.Close(); if (n > 0) { //记住密码 Updatabs();
//打开主窗口 MainForm mForm = new MainForm(); mForm.Show(); this.Hide(); } else { i++; if (i == 3) { MessageBox.Show("你已经三次输入用户和密码不正确"); this.Close(); }
MessageBox.Show("用户或密码不正确"); }
} } //验证用户是否为空 public bool CheckEmpty() { bool f = true; if (txtUserName.Text =="") { f = false; lblNameError.Visible = true; //让错误的lable显示
} else { lblNameError.Visible = false; } //验证用户密码是否为空 if (txtPassword.Text=="") { f = false; lblPswError.Visible = true;
} else { lblPswError.Visible = false; } return f; } private void btnRegist_Click(object sender, EventArgs e) { Registform rFrom = new Registform(); rFrom.Show(); } //记住用户密码 public void Updatabs() { if (ckbRemenber.Checked == true) { Dbhelper con = new Dbhelper(); string sql = string.Format("update 用户 set 标识符=1 where 用户名='{0}';", txtUserName.Text.Trim().ToString()); SqlCommand cm = new SqlCommand(sql, con.cn); con.cn.Open(); cm.ExecuteScalar(); con.cn.Close(); } } //如果上次是记住用户和密码的,把上次记住的用户和密码显示到对应的文本中 public void Showdata() { Dbhelper con = new Dbhelper(); SqlCommand cm = new SqlCommand("select 用户名,密码 from 用户 where 标识符=1", con.cn); con.cn.Open(); SqlDataReader sdr=cm.ExecuteReader(); if (sdr.Read()) { txtUserName.Text = sdr["用户名"].ToString(); txtPassword.Text = sdr["密码"].ToString(); ckbRemenber.Checked = true; } con.cn.Close(); } private void Form1_Load_1(object sender, EventArgs e) { Showdata(); //显示记住的用户和密码 } private void ckbRemenber_CheckedChanged(object sender, EventArgs e) { if(ckbRemenber.Checked==false) { Dbhelper con = new Dbhelper(); string sql = string.Format("update 用户 set 标识符=0 where 用户名='{0}';", txtUserName.Text.Trim().ToString()); SqlCommand cm = new SqlCommand(sql, con.cn); con.cn.Open(); cm.ExecuteScalar(); con.cn.Close(); } } } }
如果是一个普通的模态窗口,把用户名密码直接作为属性让父窗口读取就行了
然后在登录窗口中,
//加载上次登录用户
txbUserName.Text = Properties.Settings.Default.username ;//登录成功,保存本次登录用户
Properties.Settings.Default.username = txbUserName.Text;
Properties.Settings.Default.Save();
密码同样处理即可。
winForm用全局静态变量比较方便
webForm用Session或Cookies比较好
在你数据库用户表里面加多一 bit类型的标示符列 默认值为0,checkbox.checked==true触发为1 下面是我不严谨的写法,可以作为参考。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Mianbxs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private int i = 0;
private void btnLogin_Click(object sender, EventArgs e)
{
bool isEmpty = CheckEmpty(); //判断用户和密码是否为空
if (isEmpty == true) //如果不为空
{
Dbhelper conn = new Dbhelper();
string sql = "Select Count(*) from 用户 where 用户名='" + txtUserName.Text + "' and 密码='" + txtPassword.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn.cn);
conn.cn.Open();
int n = (int)cmd.ExecuteScalar();
conn.cn.Close();
if (n > 0)
{
//记住密码
Updatabs();
//打开主窗口
MainForm mForm = new MainForm();
mForm.Show();
this.Hide();
}
else
{
i++;
if (i == 3)
{
MessageBox.Show("你已经三次输入用户和密码不正确");
this.Close();
}
MessageBox.Show("用户或密码不正确"); }
}
}
//验证用户是否为空
public bool CheckEmpty()
{
bool f = true;
if (txtUserName.Text =="")
{
f = false;
lblNameError.Visible = true; //让错误的lable显示
}
else
{
lblNameError.Visible = false;
}
//验证用户密码是否为空
if (txtPassword.Text=="")
{
f = false;
lblPswError.Visible = true;
}
else
{
lblPswError.Visible = false;
}
return f;
} private void btnRegist_Click(object sender, EventArgs e)
{
Registform rFrom = new Registform();
rFrom.Show();
} //记住用户密码
public void Updatabs()
{
if (ckbRemenber.Checked == true)
{
Dbhelper con = new Dbhelper();
string sql = string.Format("update 用户 set 标识符=1 where 用户名='{0}';", txtUserName.Text.Trim().ToString());
SqlCommand cm = new SqlCommand(sql, con.cn);
con.cn.Open();
cm.ExecuteScalar();
con.cn.Close();
}
} //如果上次是记住用户和密码的,把上次记住的用户和密码显示到对应的文本中
public void Showdata()
{
Dbhelper con = new Dbhelper();
SqlCommand cm = new SqlCommand("select 用户名,密码 from 用户 where 标识符=1", con.cn);
con.cn.Open();
SqlDataReader sdr=cm.ExecuteReader();
if (sdr.Read())
{
txtUserName.Text = sdr["用户名"].ToString();
txtPassword.Text = sdr["密码"].ToString();
ckbRemenber.Checked = true;
}
con.cn.Close();
} private void Form1_Load_1(object sender, EventArgs e)
{
Showdata(); //显示记住的用户和密码
} private void ckbRemenber_CheckedChanged(object sender, EventArgs e)
{
if(ckbRemenber.Checked==false)
{
Dbhelper con = new Dbhelper();
string sql = string.Format("update 用户 set 标识符=0 where 用户名='{0}';", txtUserName.Text.Trim().ToString());
SqlCommand cm = new SqlCommand(sql, con.cn);
con.cn.Open();
cm.ExecuteScalar();
con.cn.Close();
}
} }
}
思路也与上面的差不多
winForm用 配置文件
webForm用Cookies密码别忘加密,哈不过你的提问也太简约了吧。
public static string M_str_name;//登录名字
public static string M_str_pass;//登录密码成功后赋值
M_str_name = textbox1.Text;
M_str_pass = textbox2.Text;其他窗体调用
frmlogin.M_str_name
frmlogin.M_str_pass