c# winform实现简单的登录功能
来源:互联网 发布:淘宝怎么删差评 编辑:程序博客网 时间:2024/05/01 22:45
通过对用户名的校验,获取密码字段然后在和用户输入的md5加密后的密码进行比较
由于我的后续程序中使用了固定的日期格式yyyy-mm-dd所以我这里设置了电脑的短日期格式
数据库连接字符串,代码中我是直接从配置文件读取的Data Source=ZJC-9;Initial Catalog=BILLINFO;User ID=sa;Password=123456" providerName="System.Data.SqlClient
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.Configuration;using System.Data.SqlClient;using System.Security.Cryptography;using System.Runtime.InteropServices;namespace user{ public partial class login : Form { public login() { InitializeComponent(); } public static string username; public static string password; //设置客户端电脑短日期格式为yyyy-mm-dd [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] public static extern int GetSystemDefaultLCID(); [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData); public const int LOCALE_SLONGDATE = 0x20; public const int LOCALE_SSHORTDATE = 0x1F; public const int LOCALE_STIME = 0x1003; public void SetDateTimeFormat() { try { int x = GetSystemDefaultLCID(); SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //短日期格式 } catch (Exception ex) { messagebox.show(ex.tostring()); } } //登录按钮 private void button1_Click(object sender, EventArgs e) { string sqlconn = ConfigurationManager.ConnectionStrings["connstring"].ToString();//读配置文件取得连接字符串 SqlConnection conn = new SqlConnection(sqlconn); SqlCommand cmd = new SqlCommand(); string pwd = getMD5(textBox2.Text); cmd.CommandText="select * from dbo.or_userinfo where userid='"+textBox1.Text+"' and password='"+ pwd +"'";//sql查询语句 cmd.Connection = conn; SqlDataReader myreader; try { conn.Open(); myreader = cmd.ExecuteReader(); if(myreader.Read()) { username = textBox1.Text; password = textBox2.Text; SetDateTimeFormat(); Form1 frm1 = new Form1(); this.Hide(); frm1.ShowDialog(); this.Close(); } else { MessageBox.Show("登录失败,用户名或密码错误!","Error"); } } catch (Exception ex) { MessageBox.Show("连接数据库失败,请检查网络!"+ex.Message.ToString()+"","Message"); } conn.Close(); } //获取MD5校验值 public static String getMD5(String requestBody) { if (requestBody == null) return null; MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(requestBody)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } }}
下面是登录窗体的贴图
0 0
- c# winform实现简单的登录功能
- C# Winform DataGridView分页功能的实现
- C# WinForm国际化实现的简单方法
- Winform实现登录窗体记住密码的功能
- 简单的SSH登录功能实现
- XMPP实现简单的登录注册功能
- javaweb简单的登录注册功能实现
- javaweb简单的登录注册功能实现
- javaweb简单的登录注册功能实现
- Servlet简单实现的注册登录功能
- javaweb简单的登录注册功能实现
- winform c# 实现被访问的网站自动登录
- winform登录窗体实现记住密码功能
- C#实现的简易含undo/redo功能的winForm
- c# winform TreeView控件中实现拖拽的功能
- c# winform TreeView控件中实现拖拽的功能
- c# winform TreeView控件中实现拖拽的功能
- c# winform TreeView控件中实现拖拽的功能
- CSS3动画属性 - animation整理
- Android 性能优化系列总篇 (五)
- 使用chrome浏览器和genymotion来调试ionic
- SQLite简介、常用SQL语句、SQL使用速查
- java入门#每天一个小程序#全排列
- c# winform实现简单的登录功能
- Python常见数据结构详解
- 编程训练
- Tapestry 会话缓存(@SessionState )导致串数据问题处理
- Mybatis的ResultMap详解
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据
- Retrofit2.0+RxJava+Dragger2实现不一样的Android网络架构搭建
- 光标定位gotoxy()
- 关于unity中的update、Lateupdate和FixedUpdate