📄 formlogin.cs
字号:
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;
using System.IO;
using System.Xml;
using System.Collections;
using StudentManager.DbLayer;
namespace StudentManager
{
public partial class FormLogin : Form
{
public FormLogin()
{
InitializeComponent();
}
public static bool isValidate = false;
/// <summary>
/// “登录”按钮单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonLogin_Click(object sender, EventArgs e)
{
//1 构造查询语句,形如:" select * from [SysUser] where UserName = '张三' and [Password] = '123456' "
string sql = "";
sql += "select * from [SysUser] where [UserName] = ";
sql += SqlStringConstructor.GetQuotedString(this.textBoxUserName.Text);
sql += " and [Password] = ";
sql += SqlStringConstructor.GetQuotedString(this.textBoxPwd.Text);
//2 查询数据
Database db = new Database();
DataRow row = db.GetDataRow(sql);
//3 根据查询结果,判断用户是否合法
if (row != null)
{
FormLogin.isValidate = true;
((FormMain)this.MdiParent).ChangeMenuStatus();
this.Close();
}
else
{
MessageBox.Show("对不起,您输入的用户名或者密码错误!");
}
}
/// <summary>
/// “窗口加载”事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormLogin_Load(object sender, EventArgs e)
{
this.Height = 380;
//从文件“Database.config”中,读取数据库配置参数
XmlDocument xdc = new XmlDocument();
try
{
//读取XML文档格式的Database.config文件中,连接字符串数据
xdc.Load("Database.config");
string conString = xdc.ChildNodes[1].ChildNodes[0].Attributes["value"].Value;
//获取连接字符串中各项内容
//server=(local);database=StudentManager;user id=sa;pwd=frock
// 0 1 2 3 4 5 6 7
ArrayList arr = new ArrayList();
arr.AddRange(conString.Split(';', '='));
//把各项内容填到对应的TextBox中
this.textBoxServer.Text = arr[1].ToString();
this.textBoxDbName.Text = arr[3].ToString();
this.textBoxDbUser.Text = arr[5].ToString();
this.textBoxDbPwd.Text = arr[7].ToString();
}
catch { }
}
/// <summary>
/// “退出”按钮单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// “配置”按钮单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonConfig_Click(object sender, EventArgs e)
{
if (this.buttonConfig.Text == "服务器配置 △")
{
this.textBoxUserName.Focus();
this.buttonConfig.Text = "服务器配置 ▽";
this.Height = 380;
}
else
{
this.textBoxServer.Focus();
this.buttonConfig.Text = "服务器配置 △";
this.Height = 544;
}
}
/// <summary>
/// “配置确定”单击按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonConfigOk_Click(object sender, EventArgs e)
{
//将用户输入的配置参数,保存到当前目录下“Database.config”文件中
StreamWriter writer = new StreamWriter("Database.config", false, System.Text.Encoding.Default);
writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
writer.WriteLine("<configuration>");
string conStr = "<DBConnectionString value=\"";
conStr += "server=" + textBoxServer.Text + ";";
conStr += "database=" + this.textBoxDbName.Text + ";";
conStr += "user id=" + this.textBoxDbUser.Text + ";";
conStr += "pwd=" + this.textBoxDbPwd.Text;
conStr += "\"/>";
writer.WriteLine(conStr);
writer.WriteLine("</configuration>");
writer.Close();
Database db = new Database();
db.Open();
this.buttonConfig.Text = "服务器配置 ▽";
this.Height = 380;
}
/// <summary>
/// “配置重置”单击按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonConfigReset_Click(object sender, EventArgs e)
{
//把各项内容清空
this.textBoxServer.Text = "";
this.textBoxDbName.Text = "";
this.textBoxDbUser.Text = "";
this.textBoxDbPwd.Text = "";
this.textBoxServer.Focus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -