.NET大作业之网吧后台管理系统2
连接sqlserver ,使用sa用户验证模式
DBwangba.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace WB
{
class DBwangba
{
/// <summary>
/// 数据库连接的数据库操作 增删改操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
///
public static int BaseExec(string sql)
{
int rz = 0;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=NetBarDB;uid=sa;pwd=20194023";
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
rz = cmd.ExecuteNonQuery();
conn.Close();
return rz;
}
/// <summary>
/// 用户获取数据库连接
/// </summary>
/// <returns></returns>
public static SqlConnection getConn()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=NetBarDB;uid=sa;pwd=20194023";
return conn;
}
}
}
ValidCode.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
namespace WB
{
class ValidCode
{
#region 私有字段
private const double PI = 3.1415926535897932384626433832795;
private const double PI2 = 6.283185307179586476925286766559;
private int _len;
private CodeType _codetype;
private readonly Single _jianju = (float)18.0;
private readonly Single _height = (float)24.0;
private string _checkCode;
#endregion
#region 公有方法
public string CheckCode
{
get
{
return _checkCode;
}
}
#endregion
#region 构造方法
/// <summary>
/// 构造方法
/// </summary>
/// <param name="len">验证码长度</param>
/// <param name="ctype">验证码类型:字母、数字、字母+数字</param>
public ValidCode(int len, CodeType ctype)
{
this._len = 4;
this._codetype = ctype;
}
#endregion
#region 枚举
/// <summary>
/// 验证码类型枚举
/// </summary>
public enum CodeType
{
Words,
Numbers,
Characters,
Alphas
}
#endregion
#region 私有方法
/// <summary>
/// 数字验证码
/// </summary>
/// <returns></returns>
private string GenerateNumbers()
{
string strOut = "";
Random random = new Random();
for (int i = 0; i < _len; i++)
{
string num = Convert.ToString(random.Next(10000) % 10);
strOut += num;
}
return strOut.Trim();
}
/// <summary>
/// 字母验证码
/// </summary>
/// <returns></returns>
private string GenerateCharacters()
{
string strOut = "";
Random random = new Random();
for (int i = 0; i < _len; i++)
{
string num = Convert.ToString((char)(65 + random.Next(10000) % 26));
strOut += num;
}
return strOut.Trim();
}
/// <summary>
/// 混合验证码
/// </summary>
/// <returns></returns>
private string GenerateAlphas()
{
string strOut = "";
string num = "";
Random random = new Random();
for (int i = 0; i < _len; i++)
{
if (random.Next(500) % 2 == 0)
{
num = Convert.ToString(random.Next(10000) % 10);
}
else
{
num = Convert.ToString((char)(65 + random.Next(10000) % 26));
}
strOut += num;
}
return strOut.Trim();
}
private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 将位图背景填充为白色
Graphics graph = Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? destBmp.Height : destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点的颜色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width && nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
#endregion
#region 公有方法
public Stream CreateCheckCodeImage()
{
string checkCode;
switch (_codetype)
{
case CodeType.Alphas:
checkCode = GenerateAlphas();
break;
case CodeType.Numbers:
checkCode = GenerateNumbers();
break;
case CodeType.Characters:
checkCode = GenerateCharacters();
break;
default:
checkCode = GenerateAlphas();
break;
}
this._checkCode = checkCode;
MemoryStream ms = null;
//
if (checkCode == null || checkCode.Trim() == string.Empty)
return null;
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * _jianju)), (int)_height);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
// 画图片的背景噪音线
for (int i = 0; i < 18; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.FromArgb(random.Next()), 1), x1, y1, x2, y2);
}
Font font = new Font("Times New Roman", 14, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
if (_codetype != CodeType.Words)
{
for (int i = 0; i < checkCode.Length; i++)
{
g.DrawString(checkCode.Substring(i, 1), font, brush, 2 + i * _jianju, 1);
}
}
else
{
g.DrawString(checkCode, font, brush, 2, 2);
}
// 画图片的前景噪音点
for (int i = 0; i < 150; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
// 画图片的波形滤镜效果
if (_codetype != CodeType.Words)
{
image = TwistImage(image, true, 3, 1);
}
// 画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
}
finally
{
g.Dispose();
image.Dispose();
}
return ms;
}
#endregion
}
}
denglu.cs
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WB
{
public partial class denglu : Form
{
ValidCode code = new ValidCode(6, ValidCode.CodeType.Numbers);//实例化ValidCode类
public denglu()
{
InitializeComponent();
tishi.Visible = false;
Bitmap bitmap = new Bitmap(code.CreateCheckCodeImage());
xianshi.Image = bitmap;
}
SqlConnection conn = null;
private void button1_Click(object sender, EventArgs e)
{
string qq = this.username.Text;
string ww = this.password.Text.Trim();
string ee = this.textBoxxx.Text;
if (qq == "" || ww == "" || ee == "")
{
Bitmap bitmap = new Bitmap(code.CreateCheckCodeImage());
xianshi.Image = bitmap;
MessageBox.Show("请填写完整信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
tishi.Visible = true;
tishi.ForeColor = Color.Red;
tishi.Text = "验证码错误";
textBoxxx.Clear();
textBoxxx.Focus();
return;
}
string constr = "server=.;database=NetBarDB;uid=sa;pwd=20194023";
// string constr = "server=.;database=myschool;integrated security=SSPI";
conn = new SqlConnection();
conn.ConnectionString = constr;
conn.Open();
if (conn.State == ConnectionState.Open)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format("select*from admin where admin= '{0}' and password='{1}'", qq, ww);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.Read() && textBoxxx.Text == code.CheckCode)
{
tishi.Visible = true;
tishi.ForeColor = Color.Green;
tishi.Text = "验证码正确";
MessageBox.Show("登陆成功","提示");
jiemian dl = new jiemian();
this.Hide();
dl.Show();
}
else
{
tishi.Visible = true;
tishi.ForeColor = Color.Red;
tishi.Text = "验证码错误";
textBoxxx.Clear();
textBoxxx.Focus();
Bitmap bitmap = new Bitmap(code.CreateCheckCodeImage());
xianshi.Image = bitmap;
MessageBox.Show("登录失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void xianshi_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(code.CreateCheckCodeImage());
xianshi.Image = bitmap;
}
private void denglu_Load(object sender, EventArgs e)
{
string skin = "Skins//DiamondBlue.ssk";
skinEngine2.SkinFile = skin;
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("是否退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (dr == DialogResult.Yes)
{
Application.Exit();
}
}
private void textBoxxx_TextChanged(object sender, EventArgs e)
{
}
private void textBoxxx_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
{
e.Handled = true;
}
else
{
//MessageBox.Show("请输入数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
private void password_TextChanged(object sender, EventArgs e)
{
}
private void zhuce_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
guanliyuanzhuce guan = new guanliyuanzhuce();
guan.Show();
}
}
}
guanliyuzhuce.cs
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WB
{
public partial class guanliyuanzhuce : Form
{
public guanliyuanzhuce()
{
InitializeComponent();
}
private void guanliyuanzhuce_Load(object sender, EventArgs e)
{
string skin = "Skins//DiamondBlue.ssk";
skinEngine2.SkinFile = skin;
}
private void btn_zhuce_Click(object sender, EventArgs e)
{
string qq = this.name.Text;
string ww = this.password.Text.Trim();
string ee = this.password2.Text.Trim();
if (qq == "" || ww == "" || ee == "")
{
MessageBox.Show("请填写完整信息!");
return;
}
if (qq.Length < 6 || ww.Length < 6)
{
MessageBox.Show("用户名或密码不能小于6位!");
return;
}
if (ww != ee)
{
MessageBox.Show("两次密码不一致!请重新输入!");
return;
}
string name = this.name.Text;
string password = this.password.Text;
SqlConnection conn = DBwangba.getConn();
conn.Open();
if (conn.State == ConnectionState.Open)
{
string we = string.Format("select * from admin where admin = '{0}'", qq);
SqlCommand cmd = new SqlCommand(we, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.Read())
{
MessageBox.Show("卡号已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string sql = string.Format("insert into admin values('{0}','{1}')", name, password);
int rz = DBwangba.BaseExec(sql);
if (rz > 0)
{
MessageBox.Show("添加成功!");
this.Hide();
denglu denglu = new denglu ();
denglu.ShowDialog();
this.Close();
return;
}
else
{
MessageBox.Show("添加失败!");
}
}
}
}
private void name_KeyPress(object sender, KeyPressEventArgs e)
{
//if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
//{
// e.Handled = true;
//}
//else
//{
// //MessageBox.Show("请输入数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//}
if (e.KeyChar < 'A' || e.KeyChar > 'Z' && e.KeyChar < 'a' || e.KeyChar > 'z')
{
e.Handled = true;
}
//此为退格键可以输入
if (e.KeyChar == 8)
{
e.Handled = false;
}
}
private void btn_cancle_Click(object sender, EventArgs e)
{
this.Hide();
denglu deng = new denglu();
deng.ShowDialog();
this.Close();
}
private void name_TextChanged(object sender, EventArgs e)
{
}
}
}