FormLogin.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 WindowsFormsApplication1 {
public partial class FormLogin : Form {
string ConnectionString = \
\Catalog=家庭财务管理系统;Persist Security Info=True; \
\
//封装链接字符串,协调统一数据库链接信息 public string getCS()
13
{
return ConnectionString; } SqlConnection conn; string strSQL;
SqlCommand Command; SqlDataReader reader; public FormLogin() {
InitializeComponent(); } static string Uname = \
//封装登录的用户名,便于登录后显示 private void setUname(string s) {
Uname = s; }
public string getUname() {
return Uname; }
14
static string Role = \ //封装用户身份
private void setRole(string s) {
Role = s; }
public string getRole() {
return Role; }
//初始化参数
private void FormLogin_Load(object sender, EventArgs e) {
//取出系统的所有用户名
//显示在下拉列表,这样用户登录的时候 //选择用户名就可以了,不用再输入用户名了 conn = new SqlConnection(ConnectionString); strSQL = \账号 FROM family\ try {
Command = new SqlCommand(strSQL, conn); conn.Open();
15
reader = Command.ExecuteReader();//结果集放到reader对 int flag = 0; //把数据库账号加载到组合框中 while (reader.Read()) {
this.comboBox1.Items.Add(reader[0].ToString()); //如果能加载账号,则选中密码框 if (flag == 0) {
textBox1.Focus(); textBox1.Select(); } flag++; }
//如果系统存在用户,则默认选中第一个用户 if (flag > 0)
this.comboBox1.SelectedIndex = 0; }
16
象中
catch (Exception ex) {
ex.GetBaseException(); } finally
码为空
{
reader.Close(); Command.Dispose(); } }
//确定,完成登陆
private void button1_Click(object sender, EventArgs e) {
int flag = 0; if (textBox1.Text == \ {
showError(\账号或密码为空!\第一种错误,账号或密 return; } 17