на тему рефераты Информационно-образоательный портал
Рефераты, курсовые, дипломы, научные работы,
на тему рефераты
на тему рефераты
МЕНЮ|
на тему рефераты
поиск
База даних "Телефонний довідник"
p align="left">select (int)q.Attribute("ID")).Max();

}

catch { }

maxID++;

XElement newItem = new XElement("Item", new XAttribute("ID", maxID),

new XAttribute("UserID", Variables.CurrentUserID),

new XAttribute("Name", textBoxName.Text.Trim()),

new XAttribute("Mobile", textBoxMobile.Text.Trim()),

new XAttribute("Phone", textBoxPhone.Text.Trim()),

new XAttribute("Email", textBoxEMail.Text.Trim()),

new XAttribute("Address", textBoxAddress.Text.Trim()),

new XAttribute("RegDate", DateTime.Now.ToString()));

var ItemsElement = (from q in Variables.xDocument.Descendants("Items")

select q).First();

ItemsElement.Add(newItem);

}

#endregion

#region edit item

else if (EditItem)

{

if (textBoxName.Text.Trim() == "")

{

errorProvider1.SetError(textBoxName, "Будь-ласка, введіть ім'я");

return;

}

var theItem = (from q in Variables.xDocument.Descendants("Item")

where q.Attribute("ID").Value == this.ItemID

select q).First();

theItem.Attribute("Name").Value = textBoxName.Text.Trim();

theItem.Attribute("Mobile").Value = textBoxMobile.Text.Trim();

theItem.Attribute("Phone").Value = textBoxPhone.Text.Trim();

theItem.Attribute("Email").Value = textBoxEMail.Text.Trim();

theItem.Attribute("Address").Value = textBoxAddress.Text.Trim();

}

#endregion

TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV);

//Variables.xDocument.Save("debug.xml");

this.Close();

}

catch (Exception ex)

{

StackFrame file_info = new StackFrame(true);

Messages.error(ref file_info, ex.Message, this);

}

}

#region

Image ResizeImage(Image FullsizeImage, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)

{

// Prevent using images internal thumbnail

FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (OnlyResizeIfWider)

{

if (FullsizeImage.Width <= NewWidth)

{

NewWidth = FullsizeImage.Width;

}

}

int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;

if (NewHeight > MaxHeight)

{

// Resize with height instead

NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;

NewHeight = MaxHeight;

}

System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

// Clear handle to original file so that we can overwrite it if necessary

FullsizeImage.Dispose();

// Save resized picture

return NewImage;

}

string ImageToBase64String(Image image, ImageFormat format)

{

MemoryStream memory = new MemoryStream();

image.Save(memory, format);

string base64 = Convert.ToBase64String(memory.ToArray());

memory.Close();

return base64;

}

Image ImageFromBase64String(string base64)

{

MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));

Image result = Image.FromStream(memory);

memory.Close();

return result;

}

#endregion

}

}

Форма користувача:

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.IO;

using System.Xml.Linq;

using Phonebook.Classes;

using System.Diagnostics;

using System.Net.Mail;

using System.Net;

namespace Phonebook

{

public partial class UserForm: Form

{

bool NewUser = false;

bool ChangeUser = false;

bool ChangeInfo = false;

public UserForm(bool newUser, bool changeUser, bool changeInfo)

{

InitializeComponent();

/////////////////////

this.NewUser = newUser;

this.ChangeInfo = changeInfo;

this.ChangeUser = changeUser;

if (NewUser)

{

this.Text = "Додати нового користувача";

labelPass1.Text = "Пароль:";

labelPass2.Text = "Підтвердити пароль:";

checkBoxForgetPass.Enabled = false;

}

else if (ChangeUser)

{

this.Text = "Обрати користувача";

labelPass1.Text = "Пароль:";

labelPass2.Text = "Новий пароль:";

labelPass2.Enabled = textBoxPassword2.Enabled = false;

labelEmail.Enabled = textBoxEmail.Enabled = false;

}

else if (ChangeInfo)

{

this.Text = "Змінити інформацію користувача";

labelPass1.Text = "Старий пароль:";

labelPass2.Text = "Новий пароль:";

}

}

private void buttonSubmit_Click(object sender, EventArgs e)

{

try

{

errorProvider1.Clear();

#region Forgets the password

if (checkBoxForgetPass.Enabled && checkBoxForgetPass.Checked)

{

if (textBoxUsername.Text.Trim() == "")

{

errorProvider1.SetError(this.textBoxUsername, "Будь-ласка, введіть ім'я користувача");

textBoxUsername.Focus();

return;

}

errorProvider1.Clear();

var user = Variables.xDocument.Descendants("User").Where(q => q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower());

if (user.Count() < 1)

{

errorProvider1.SetError(this.textBoxUsername, "Таке ім'я користувача відсутнє у базі даних!!!!");

return;

}

string password = user.First().Attribute("Password").Value;

try

{

NetworkCredential loginInfo = new NetworkCredential("username", "password");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("CyberkillerPS@gmail.ru");

msg.To.Add(new MailAddress(user.First().Attribute("Email").Value));

msg.Subject = "Phonebook Password";

msg.Body = "Yours Password = " + password;

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.ru");

client.EnableSsl = true;

client.UseDefaultCredentials = false;

client.Credentials = loginInfo;

client.Send(msg);

MessageBox.Show("Ваш пароль був відісланий вам на email", "Відправка пароля", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message, "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

return;

}

#endregion

#region add new user

else if (this.NewUser)

{

if (textBoxUsername.Text.Trim() == "" && textBoxUsername.Enabled)

{

errorProvider1.SetError(this.textBoxUsername, "Будь-ласка, введіть ім'я користувача");

return;

}

else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled)

{

errorProvider1.SetError(this.textBoxPassword1, "Будь-ласка, введіть пароль");

return;

}

else if (textBoxPassword2.Text.Trim() == "" && textBoxPassword2.Enabled)

{

errorProvider1.SetError(this.textBoxPassword2, "Будь-ласка, введіть підтвердження пароля");

return;

}

else if (textBoxPassword2.Text.Trim()!= textBoxPassword1.Text.Trim())

{

errorProvider1.SetError(this.textBoxPassword1, "Ваші паролі повинні співпадати");

errorProvider1.SetError(this.textBoxPassword2, "Ваші паролі повинні співпадати");

return;

}

else if (textBoxEmail.Text.Trim() == "" && textBoxEmail.Enabled)

{

errorProvider1.SetError(this.textBoxEmail, "Будь-ласка, введіть правельний Email");

return;

}

if (!File.Exists(Variables.DBFile))

{

Variables.xDocument = new XDocument(

new XComment("\n Don't edit manually \n"),

new XElement("PhoneBook",

new XElement("Users",

new XElement("User",

new XAttribute("ID", "01"),

new XAttribute("UserName", textBoxUsername.Text.Trim()),

new XAttribute("Password", textBoxPassword1.Text.Trim()),

new XAttribute("Email", textBoxEmail.Text.Trim()),

new XAttribute("RegDate", DateTime.Now.ToString()))),

new XElement("Settings",

new XElement("Setting",

new XAttribute("UserID", "01"),

new XAttribute("RightToLeft", "NO"),

new XAttribute("Dates", "Persian"),

new XAttribute("FontSize", "10"))),

new XElement("Items")));

Variables.CurrentUserID = "01";

}

else

{

Variables.xDocument = XDocument.Parse(TripleDES.DecryptFromFile(Variables.DBFile, TripleDES.ByteKey, TripleDES.IV));

var SameUserQuery = from q in Variables.xDocument.Descendants("User")

where q.Attribute("UserName").Value.ToLower() == textBoxUsername.Text.Trim().ToLower()

select q;

if (SameUserQuery.Count() >= 1)

{

errorProvider1.SetError(this.textBoxUsername, "Таке ім'я користувача вже існує, Будь-ласка, оберіть інше");

return;

}

int maxID = 0;

try

{

maxID = (from q in Variables.xDocument.Descendants("User")

select (int)q.Attribute("ID")).Max();

}

catch { }

maxID++;

Variables.CurrentUserID = maxID.ToString();

XElement xElement = new XElement("User",

new XAttribute("ID", maxID),

new XAttribute("UserName", textBoxUsername.Text.Trim()),

new XAttribute("Password", textBoxPassword1.Text.Trim()),

new XAttribute("Email", textBoxEmail.Text.Trim()),

new XAttribute("RegDate", DateTime.Now.ToString()));

var usersElement = (from q in Variables.xDocument.Descendants("Users")

select q).First();

usersElement.Add(xElement);

xElement = new XElement("Setting",

new XAttribute("UserID", maxID),

new XAttribute("RightToLeft", "NO"),

new XAttribute("Dates", "Persian"),

new XAttribute("FontSize", "10"));

var settingsElement = (from q in Variables.xDocument.Descendants("Settings")

select q).First();

settingsElement.Add(xElement);

}

Variables.CurrentUserName = textBoxUsername.Text.Trim();

TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV);

//Variables.xDocument.Save("debug.xml");

}

#endregion

#region change user

else if (this.ChangeUser)

{

if (Variables.xDocument == null)

{

MessageBox.Show("Ваше ім'я користувача та пароль невірні", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

}

if (textBoxUsername.Text.Trim() == "")

{

errorProvider1.SetError(this.textBoxUsername, "Будь-ласка, введіть ім'я користувача");

return;

}

else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled)

{

errorProvider1.SetError(this.textBoxPassword1, "Будь-ласка, введіть пароль");

return;

}

var query = from q in Variables.xDocument.Descendants("User")

where textBoxUsername.Text.Trim().ToLower() == q.Attribute("UserName").Value.ToLower()

&& textBoxPassword1.Text.Trim().ToLower() == q.Attribute("Password").Value.ToLower()

select q;

if (query.Count() == 1)

{

Variables.CurrentUserID = query.First().Attribute("ID").Value;

Variables.CurrentUserName = textBoxUsername.Text.Trim();

}

else

{

Variables.CurrentUserID = "";

Variables.CurrentUserName = "";

MessageBox.Show("Ваше ім'я користувача та пароль невірні", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

}

}

#endregion

#region change info

else if (this.ChangeInfo)

{

bool changePassword = true;

if (textBoxUsername.Text.Trim() == "" && textBoxUsername.Enabled)

{

errorProvider1.SetError(this.textBoxUsername, "Ваше ім'я користувача та пароль невірні");

return;

}

else if (textBoxEmail.Text.Trim() == "" && textBoxEmail.Enabled)

{

errorProvider1.SetError(this.textBoxEmail, "Будь-ласка, введіть вірний Email");

return;

}

else if (textBoxPassword1.Text.Trim() == textBoxPassword2.Text.Trim() && textBoxPassword2.Text.Trim() == "")

{

changePassword = false;

}

else if (textBoxPassword1.Text.Trim() == "" && textBoxPassword1.Enabled)

{

errorProvider1.SetError(this.textBoxPassword1, "Будь-ласка, введіть старий пароль");

return;

}

else if (textBoxPassword2.Text.Trim() == "" && textBoxPassword2.Enabled)

{

errorProvider1.SetError(this.textBoxPassword2, "Будь-ласка, введіть новий пароль");

return;

}

errorProvider1.Clear();

var query = (from q in Variables.xDocument.Descendants("User")

where q.Attribute("ID").Value == Variables.CurrentUserID

select q).First();

string oldPassword = query.Attribute("Password").Value;

if (oldPassword.ToLower()!= textBoxPassword1.Text.Trim().ToLower() && changePassword)

{

errorProvider1.SetError(this.textBoxPassword1, "Старий пароль невірний");

return;

}

else if (oldPassword == textBoxPassword1.Text.Trim() && changePassword)

{

query.Attribute("UserName").Value = textBoxUsername.Text.Trim();

query.Attribute("Password").Value = textBoxPassword2.Text.Trim();

query.Attribute("Email").Value = textBoxEmail.Text.Trim();

}

else if (!changePassword)

{

query.Attribute("UserName").Value = textBoxUsername.Text.Trim();

query.Attribute("Email").Value = textBoxEmail.Text.Trim();

}

Variables.CurrentUserID = query.Attribute("ID").Value;

Variables.CurrentUserName = textBoxUsername.Text.Trim();

TripleDES.EncryptToFile(Variables.xDocument.ToString(SaveOptions.DisableFormatting), Variables.DBFile, TripleDES.ByteKey, TripleDES.IV);

//Variables.xDocument.Save("debug.xml");

}

#endregion

this.Close();

}

catch (Exception ex)

{

Variables.CurrentUserID = Variables.CurrentUserName = "";

StackFrame file_info = new StackFrame(true);

Messages.error(ref file_info, ex.Message, this);

}

}

}

}

Додаток 2: Головне вікно програми

Додаток 3: Форма користувача БД

Додаток 4: Форма створення нового запису в БД

Страницы: 1, 2, 3, 4, 5, 6



© 2003-2013
Рефераты бесплатно, курсовые, рефераты биология, большая бибилиотека рефератов, дипломы, научные работы, рефераты право, рефераты, рефераты скачать, рефераты литература, курсовые работы, реферат, доклады, рефераты медицина, рефераты на тему, сочинения, реферат бесплатно, рефераты авиация, рефераты психология, рефераты математика, рефераты кулинария, рефераты логистика, рефераты анатомия, рефераты маркетинг, рефераты релиния, рефераты социология, рефераты менеджемент.