using SnacksInventorySystem;
using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace lsd
{
public partial class FormLogin : Form
{
public FormLogin()
{
InitializeComponent();
InitializeUI();
}
private void InitializeUI()
{
// 设置窗口标题
Text = "零食店进销存管理系统 - 登录";
// 设置窗口大小
Width = 300;
Height = 200;
// 设置窗口起始位置为屏幕中央
NewMethod();
// 创建用户名标签
Label labelUsername = new Label();
labelUsername.Text = "用户名:";
labelUsername.Location = new Point(20, 20);
labelUsername.AutoSize = true;
// 创建用户名输入框
textBoxUsername = new TextBox();
textBoxUsername.Location = new Point(80, 20);
textBoxUsername.Width = 150;
// 创建密码标签
Label labelPassword = new Label();
labelPassword.Text = "密码:";
labelPassword.Location = new Point(20, 60);
labelPassword.AutoSize = true;
// 创建密码输入框(设置密码字符显示为黑点)
textBoxPassword = new TextBox();
textBoxPassword.Location = new Point(80, 60);
textBoxPassword.Width = 150;
textBoxPassword.PasswordChar = '*';
// 创建登录按钮
Button buttonLogin = new Button();
buttonLogin.Text = "登录";
buttonLogin.Location = new Point(100, 100);
buttonLogin.Click += buttonLogin_Click;
// 创建用于显示错误提示信息的标签
labelError = new Label();
labelError.Location = new Point(20, 140);
labelError.AutoSize = true;
labelError.ForeColor = Color.Red;
// 将各控件添加到登录窗口
Controls.Add(labelUsername);
Controls.Add(textBoxUsername);
Controls.Add(labelPassword);
Controls.Add(textBoxPassword);
Controls.Add(buttonLogin);
Controls.Add(labelError);
}
private static void NewMethod()
{
}
private void buttonLogin_Click(object sender, EventArgs e)
{
string username = textBoxUsername.Text;
string password = textBoxPassword.Text;
// 这里假设连接到本地的SQL Server数据库,实际根据你的配置修改连接字符串
string connectionString = "Data Source=LAPTOP-ODQTAPDG\\MSSQLSERVER01;Initial Catalog=SnacksInventoryDB;User ID=root;Password=123456";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Username", username);
command.Parameters.AddWithValue("@Password", password);
try
{
connection.Open();
int count = (int)command.ExecuteScalar();
if (count > 0)
{
// 登录成功,打开主界面
FormMain formMain = new FormMain();
formMain.Show();
this.Hide();
}
else
{
labelError.Text = "用户名或密码错误,请重新输入!";
}
}
catch (Exception ex)
{
labelError.Text = "登录出错:" + ex.Message;
}
}
}
private void FormLogin_Load(object sender, EventArgs e)
{
// 在这里编写登录页面加载时要执行的代码逻辑
// 例如设置窗口的背景颜色
this.BackColor = Color.White;
// 或者对用户名输入框设置初始提示文本(仅示例,实际可能有更好方式)
textBoxUsername.Text = "请输入用户名";
textBoxUsername.ForeColor = Color.Gray;
textBoxUsername.GotFocus += (s, args) =>
{
if (textBoxUsername.Text == "请输入用户名")
{
textBoxUsername.Text = "";
textBoxUsername.ForeColor = Color.Black;
}
};
textBoxUsername.LostFocus += (s, args) =>
{
if (string.IsNullOrEmpty(textBoxUsername.Text))
{
textBoxUsername.Text = "请输入用户名";
textBoxUsername.ForeColor = Color.Gray;
}
};
// 还可以加载一些比如记住密码相关的数据(如果有此功能)等,根据实际业务逻辑添加代码
}
private TextBox textBoxUsername;
private TextBox textBoxPassword;
private Label labelError;
}
}
namespace lsd
{
partial class FormLogin
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(300, 200);
this.IsMdiContainer = false;
this.MainMenuStrip = null;
this.Name = "FormLogin";
this.Text = "零食店进销存管理系统 - 登录";
this.Load += new System.EventHandler(this.FormLogin_Load);
}
#endregion
}
}