c#登陆代码

这篇博客展示了如何在C#中实现登录功能。通过使用SqlConnection和ConfigurationManager连接数据库,执行SQL查询来验证用户名和密码,实现了用户输入检查及登录结果提示。点击按钮后,代码会检查输入框是否为空,然后进行数据库查询,根据查询结果决定是否显示登录成功或失败的消息。
摘要由CSDN通过智能技术生成

using System.Data.SqlClient;

using System.Configuration;//首先在资源管理器的"引用"里引用System.configuration文件

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ToString();
            string sqlstr = "select count(*) from adm where(Auname='"+maskedTextBox1.Text+"'and Apw='"+maskedTextBox2.Text+"')";

            SqlCommand cmd = new SqlCommand(sqlstr,conn);
            conn.Open();
            int flag = System.Convert.ToInt32(cmd.ExecuteScalar());
            conn.Close();
            if (maskedTextBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            else if (maskedTe

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#编程登陆界面代码 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.OleDb; namespace 人事管理系统 { public partial class FormLogin : Form { public FormLogin() { InitializeComponent(); } private void FormLogin_Load(object sender, EventArgs e) { } public void buttonLogin_Click_1(object sender, EventArgs e) { try { if (textBoxName .Text =="") { MessageBox.Show ("用户名不能为空!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning ); } else { if (textBoxPassword .Text =="") { MessageBox.Show("密码不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } else { String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\人事数据库.mdb"; OleDbConnection conn = new OleDbConnection(connStr); //String sql = "select count(*) from 用户密码表 where username='" + textBoxName.Text + "' and password='" + textBoxPassword.Text + "'"; conn.Open(); string sql = "select count(*) from 用户密码表 where username=@name and password=@pass"; OleDbCommand cmd = new OleDbCommand(sql ,conn ); cmd.Parameters.Add(new OleDbParameter ("@name",OleDbType .Char )); cmd.Parameters["@name"].Value = textBoxName.Text; cmd.Parameters.Add(new OleDbParameter("@pass", OleDbType.Char)); cmd.Parameters["@pass"].Value = textBoxPassword.Text; //OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); //DataTable dt = new DataTable(); //da.Fill(dt); //int i = dt.Rows.Count; int i =(int )cmd.ExecuteScalar (); conn.Close(); if (i > 0) { FormMain fm = new FormMain(); fm.admintime = DateTime.Now.ToShortDateString(); fm.adminname = textBoxName.Text; fm.Show(); this.Hide (); } else { MessageBox.Show("用户名密码错误!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error ); { textBoxName.Text = ""; textBoxPassword.Text = ""; textBoxName.Focus(); } } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void button1_Click(object sender, EventArgs e) { if (MessageBox.Show("您确定要退出本系统吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { Application.Exit(); } } private void textBoxName_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { textBoxPassword.Focus(); e.Handled = true; } } private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { buttonLogin.Focus(); e.Handled = true; } } private void linkLabeladd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Form添加 fpty = new Form添加(); fpty.Show(); } private void linkLabelpass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Formpasswordmodify fpmd = new Formpasswordmodify(); fpmd.Show(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值