標題:
2011 04 16 資料庫操作- 修改使用者密碼
[打印本頁]
作者:
buy
時間:
2011-4-16 10:55
標題:
2011 04 16 資料庫操作- 修改使用者密碼
[attach]195[/attach]
作者:
p17johnny
時間:
2011-4-16 12:00
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;
namespace Ex20110312_02
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
string CurrentLoginUser = "";
private void btnSave_Click_1(object sender, EventArgs e)
{
//前置判斷,判斷使用者申請的帳號有無非法字元:" " "," "." "@" Regular Expression
if (tbxUserId.Text.Trim() == "" )
{
MessageBox.Show("請輸入帳號");
return;
}
Database1Entities db = new Database1Entities(); //把資料庫實體化
var addobj = new TestDB //設定要加入資料庫的一筆資料
{
id = db.TestDB.NextId(a=>a.id),
UserID = tbxUserId.Text,
UserPW = tbxUserPW.Text
};
db.TestDB.AddObject(addobj); //新增一筆'資料
try //還會有更好方法
{
db.SaveChanges(); //儲存
}
catch (System.Data.UpdateException)
{
MessageBox.Show("您的帳號已經存在");
}
RefreshDataGridView(); //更新畫面
}
private void Form1_Load(object sender, EventArgs e)
{
Database1Entities db = new Database1Entities();
var result = from s in db.TestDB select s;
dataGridView1.DataSource = result;
}
//更新畫面
private void RefreshDataGridView()
{
Database1Entities db = new Database1Entities();
var result = from s in db.TestDB select s;
dataGridView1.DataSource = result;
}
//登入
private void button3_Click(object sender, EventArgs e)
{
string UserID = tbxLoginUserId.Text;
string UserPW = tbxLoginUserPW.Text;
Database1Entities db = new Database1Entities(); //把資料庫實體化
//LinQ語法不曉得資料庫怎麼定義,所以選取時候預設結果是複數個。
var result = (from s in db.TestDB where s.UserID == UserID select s).FirstOrDefault();
if (result == null)
{
MessageBox.Show("查無此帳號,請先註冊帳號");
return;
}
else
{
if (result.UserPW == UserPW)
{ //記住目前使用者的UserId
CurrentLoginUser = result.UserID;
MessageBox.Show("登入成功");
}
else
{
MessageBox.Show("密碼錯誤");
return;
}
}
}
//public FileResult BMP()
public Bitmap GetBMP()
{
string[] Code ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"1", "2", "3", "4", "5", "6", "7", "8", "9" };
string strRd = string.Empty; //string strRd = "";
Random rd = new Random(); //unchecked((int)DateTime.Now.Ticks)
//******
Bitmap Bmp = new Bitmap(150, 50); //建立實體圖檔並設定大小
Graphics Gpi = Graphics.FromImage(Bmp);
Font Font1 = new Font("Verdana", 20, FontStyle.Italic);
for (int i = 0; i < 5; i++) // 亂數產生驗證文字
{
strRd += Code[rd.Next(34)];
}
Pen PenLine = new Pen(Brushes.Red, 2); //實體化筆刷並設定顏色、大小(畫X,Y軸用)
Gpi.Clear(Color.Black); //設定背景顏色為黑色
Gpi.DrawLine(PenLine, 0, rd.Next(80), 90, rd.Next(25)); //畫直線
Gpi.DrawString(strRd, Font1, Brushes.AntiqueWhite, 0, 0);
Gpi.DrawBezier(PenLine, new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)), new Point(rd.Next(80), rd.Next(80)));
for (int i = 0; i <= 25; i++) //亂數產生霧點,擾亂機器人辨別
{
int RandPixelX = rd.Next(0, 80);
int RandPixelY = rd.Next(0, 25);
Bmp.SetPixel(RandPixelX, RandPixelY, Color.Pink);
}
//Session["ValidateCode"] = strRd; //將驗證碼存入Session以便稍後進行驗
//System.IO.MemoryStream s = new System.IO.MemoryStream();
//Bmp.Save(s, System.Drawing.Imaging.ImageFormat.Gif);
//return File(s.GetBuffer(), "image/gif");
return Bmp;
}
private void getPWBMP_Click(object sender, EventArgs e)
{
pictureBox1.Image = GetBMP();
}
Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
f2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(f2.GetParam());
}
//使用者修改密碼
private void btnChangePwd_Click(object sender, EventArgs e)
{
if (CurrentLoginUser == "")
{
MessageBox.Show("請先登入");
return;
}
else
{
Database1Entities db = new Database1Entities(); //把資料庫實體化
var result = (from s in db.TestDB where s.UserID == CurrentLoginUser select s).FirstOrDefault();
//1. 先看原密碼對不對
if (result.UserPW == tbxOriginal.Text)
{
//2. 判斷使用者輸入的兩次密碼是否相同
if (tbxChange.Text == tbxChange2.Text)
{
//3.*防範使用者新密碼輸入空白
if (tbxChange.Text.Trim() == "")
{
return;
}
//4.修改使用者密碼
result.UserPW = tbxChange.Text;
db.SaveChanges();
MessageBox.Show("FInish changing");
RefreshDataGridView();
//* 顯示成功訊息,更新dataGridView
}
else
{
MessageBox.Show("兩次新密碼輸入不相同!");
return;
}
}
else
{
MessageBox.Show("密碼錯誤!");
return;
}
}
}
}
//手工打造
class FakeTestDB
{
public int id { get; set; }
public string UserID { get; set; }
public string UserPW { get; set; }
}
}
複製代碼
作者:
p17johnny
時間:
2011-4-16 12:02
PPajflijsafieljsaefasdfasdf
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2