}//绘制Button的背景色 private bool CanBook() { MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; bool result = true; try { con.Open(); string cmd = "select * from seatbook where UserId = '" + Connect.GetUser() + "';"; command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (reader.HasRows) { result = false; } reader.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); return(result); }
private bool IsHaveQual(int num) { MySqlConnection con = Connect.Connection(); bool result = false; string cmd = "select count(UserId) as 'cnt' from borrow where UserId = '" + Connect.GetUser() + "';"; try { con.Open(); MySqlCommand command = new MySqlCommand(cmd, con); MySqlDataReader reader = command.ExecuteReader(); int i = 0; if (reader.Read()) { i = Convert.ToInt32(reader["cnt"].ToString()); } if (i + num <= 3) { result = true; } con.Close(); reader.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } return(result); }
//功能函数 //======================================================================================================================================= private void Init() { MySqlConnection con = Connect.Connection(); string cmd = "select * from UserMessage where UserId = '" + Connect.GetUser() + "';"; MySqlCommand command = new MySqlCommand(cmd, con); MySqlDataReader reader; bool isHave = true; try { con.Open(); reader = command.ExecuteReader(); reader.Read(); if (reader.HasRows) { isHave = false; } reader.Close(); if (isHave) { command.CommandText = "insert into UserMessage (UserId) values('" + Connect.GetUser() + "');"; command.ExecuteNonQuery(); } } catch (MySqlException ex) { MessageBox.Show(ex.Message + "Init"); } con.Close(); return; }//检查是否有有关该用户的信息记录
//功能函数 //================================================================================================================================================== private void Init() { List1AddHead(); MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; string cmd = "select * from book;"; try { con.Open(); command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); while (reader.Read()) { string bookname = reader["BookName"].ToString().Trim(); string writer = reader["Writer"].ToString().Trim(); bookname = Formatting(bookname, 15); writer = Formatting(writer, 15); string str = bookname + writer; this.list1.Items.Add(str); } reader.Close(); }catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); }
private void enter_Click(object sender, RoutedEventArgs e) { bool mark = false; string pwd = getPassWord(); MySqlConnection con = Connect.Connection(); MySqlCommand command = new MySqlCommand(); command.CommandText = "select * from login where PassWord = '******' and UserId = '" + Connect.GetUser() + "';"; command.Connection = con; con.Open(); MySqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { mark = true; } reader.Close(); con.Close(); if (mark) { ResetPassword reset = new ResetPassword(); reset.WindowStyle = WindowStyle.None; reset.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; reset.ShowDialog(); this.Close(); } else { MessageBox.Show("密码输入不正确!"); } }
//功能函数区域 //===================================================================================================================== //================================================================================================================================ private bool IsBorrowed(string bookname, string writter) { MySqlConnection con = Connect.Connection(); try { con.Open(); string cmd = "select * from borrow where BookName = '" + bookname + "' and Writer = '" + writter + "';"; MySqlCommand command = new MySqlCommand(cmd, con); MySqlDataReader reader = command.ExecuteReader(); bool result; if (reader.HasRows) { result = true; } else { result = false; } con.Close(); reader.Close(); return(result); } catch (MySqlException ex) { MessageBox.Show(ex.Message); return(true); } }//判断是否被借走
//点击事件 //================================================================================================================================================== private void Open_Click(object sender, RoutedEventArgs e) { text.Clear(); if (list1.SelectedItem == null) { return; } if (list1.SelectedIndex == 0) { return; } string str = list1.SelectedItem.ToString().TrimEnd('|'); string[] array = str.Split('|'); string bookname = array[0]; string writer = array[1]; MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; string cmd = "select Path from bookpath where BookName = '" + bookname + "' and Writer = '" + writer + "';"; try { con.Open(); command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); reader.Read(); if (!reader.HasRows) { MessageBox.Show("未找到文件!"); return; } string path = reader["Path"].ToString(); //if (reader.HasRows) //{ // path = reader["Path"].ToString(); //} //else //{ // MessageBox.Show("暂时没有相关书籍!"); //} reader.Close(); string temp = string.Empty; StreamReader streamReader = new StreamReader(@path, Encoding.UTF8); while (!streamReader.EndOfStream) { temp = streamReader.ReadLine().ToString(); this.text.Text = text.Text + " " + temp + "\n" + "\n"; } } catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); }
private void borrow_Click(object sender, RoutedEventArgs e) { if (!IsHaveQual(num)) { MessageBox.Show("可借阅数目超过最大上限!"); return; } if (num == 0) { return; } string[] gather = new string[num]; for (int i = 0; i < num; i++) { gather[i] = this.list1.Items[i + 1].ToString(); } for (int i = 0; i < num; i++) { string[] array = gather[i].Split('|'); array[0] = array[0].Trim(); array[1] = array[1].Trim(); MySqlConnection con = Connect.Connection(); try { con.Open(); string cmd = "insert into borrow values ('" + array[0] + "','" + array[1] + "','" + Connect.GetUser() + "');"; MySqlCommand command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); MessageBox.Show("借阅成功!"); //string check = "select * from borrow where BookName = '" + array[0] + "' and Writer = '" + array[1] + "' and UserId = '" + Connect.GetUser() + "';"; //command = new MySqlCommand(check, con); //MySqlDataReader reader = command.ExecuteReader(); //if (reader.HasRows) //{ // MessageBox.Show("借阅成功!"); //} //else //{ // MessageBox.Show("借阅失败!"); // return; //} //reader.Close(); con.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); return; } } list1.Items.Clear(); List1AddHead(); num = 0; }
private void Cancel(object sender, MouseButtonEventArgs e)//右键点击取消 { var button = sender as Button; if (button.Background == System.Windows.Media.Brushes.Green) { return; } if (CanBook()) { MessageBox.Show("你无权取消该位置的预约!"); return; } string name = button.Name; string[] array = name.Split('_'); string row = array[1]; string column = array[2]; bool mark = false; MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; try { con.Open(); string cmd = "select * from seatbook where x = '" + row + "' and y = '" + column + "'and UserId = '" + Connect.GetUser() + "';"; command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (reader.HasRows) { mark = true; } else { MessageBox.Show("你无权取消该位置的预约!"); } reader.Close(); if (mark) { cmd = "delete from seatbook where UserId = '" + Connect.GetUser() + "';"; command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); MessageBox.Show("取消预约成功!"); } } catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); ButtonBrush(); }
//点击事件区域 //=================================================================================================================== private void Button_Click(object sender, RoutedEventArgs e) { var button = sender as Button; if (button.Background == System.Windows.Media.Brushes.Red) { MessageBox.Show("该座位已被预约!"); return; } if (!CanBook()) { MessageBox.Show("你已预约另一位置,不能再次预约!"); return; } string name = button.Name; string[] array = name.Split('_'); string row = array[1]; string column = array[2]; MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; try { con.Open(); string cmd = "insert into seatbook values('" + row + "','" + column + "','" + Connect.GetUser() + "');"; command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); cmd = "select * from seatbook where x = '" + row + "' and y = '" + column + "'and UserId = '" + Connect.GetUser() + "';"; command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (reader.HasRows) { MessageBox.Show("预约成功!"); } reader.Close(); }catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); ButtonBrush(); }
}//获得字符串实际长度 private void Show() { MySqlConnection con = Connect.Connection(); try { con.Open(); string sel = "select * from book;"; MySqlCommand command = new MySqlCommand(sel, con); MySqlDataReader reader = command.ExecuteReader(); AddHead(); while (reader.Read()) { string[] str = new string[5]; str[0] = reader["BookName"].ToString(); str[1] = reader["Writer"].ToString(); str[2] = reader["Page"].ToString(); str[3] = reader["Public"].ToString(); str[4] = reader["DefaultPrice"].ToString(); string sep1 = Formatting(str[0], 20); string sep2 = Formatting(str[1], 20); string sep3 = Formatting(str[2], 20); string sep4 = Formatting(str[3], 20); string sep5 = Formatting(str[4], 20); string strsub = sep1 + sep2 + sep3 + sep4 + sep5; this.list.Items.Add(strsub); } con.Close(); reader.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } }//listbox初始化,显示数据库数据
//功能函数 //======================================================================================================== private void Init() { this.no1.Text = ""; this.no2.Text = ""; this.no3.Text = ""; MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; string cmd = "select * from borrow where UserId = '" + Connect.GetUser() + "';"; int i = 0; try { con.Open(); command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); string bookname, writer; TextBox tb; while (reader.Read()) { i++; bookname = reader["BookName"].ToString().Trim(); writer = reader["Writer"].ToString().Trim(); string str1 = Formatting(bookname, 15); string str2 = Formatting(writer, 15); str2 = str2.TrimEnd('|').Trim(); string str = str1 + str2; string name = "no" + i; tb = this.FindName(name) as TextBox; tb.Text = str; } i++; for (; i <= 3; i++) { Button btn = this.FindName("btn_no" + i) as Button; btn.IsEnabled = false; } reader.Close(); con.Close(); }catch (MySqlException ex) { MessageBox.Show(ex.Message); } }
}//检查是否有有关该用户的信息记录 private void SetValues() { DeleteTextChange(); string picturePath = "d:/LibraryResource/UserPicture/";//pack://SiteOfOrigin:,,,/ MySqlConnection con = Connect.Connection(); string cmd = string.Format("select * from usermessage where UserId = '{0}';", Connect.GetUser().ToString()); MySqlDataReader reader; try { con.Open(); MySqlCommand command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); reader.Read(); this.NickName.Text = reader["NickName"].ToString(); this.Age.Text = reader["Age"].ToString(); this.College.Text = reader["College"].ToString(); this.Address.Text = reader["Address"].ToString(); this.Motto.Text = reader["Motto"].ToString(); this.Introduce.Text = reader["Introduce"].ToString(); string pictureName = reader["Picture"].ToString().Trim();//图片设置 if (pictureName != "" && pictureName != null) { picturePath += pictureName; //this.Picture.Source = new BitmapImage(new Uri(picturePath, UriKind.Absolute)); setImageSource(picturePath); } else { this.Picture.Source = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/d:/libraryresource/Userpicture/defaultpic.jpg", UriKind.Absolute)); } reader.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message + "setvalues"); } con.Close(); AddTextChange(); return; }//设置所有文本框中的信息
}//判断是否被借走 private void canBorrow_Click(object sender, RoutedEventArgs e)//筛选未被借走的书籍 { int index = list.Items.Count - 1; MySqlConnection con = Connect.Connection(); try { MySqlDataReader reader; con.Open(); while (true) { if (index < 2) { break; } string str = list.Items[index].ToString(); string[] arrary = str.Split('|'); string cmd = "select * from borrow where BookName = '" + arrary[0].Trim() + "' and Writer = '" + arrary[1].Trim() + "';"; MySqlCommand command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (reader.HasRows) { list.Items.RemoveAt(index); index--; } else { index--; } reader.Close(); } con.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } }
private void enter_Click(object sender, RoutedEventArgs e) { string pwd1 = getPassWord(this.pb1); string pwd2 = getPassWord(this.pb2); if (pwd1 == "" || pwd2 == "") { MessageBox.Show("不能为空!"); } else if (pwd1 != pwd2) { MessageBox.Show("两次输入密码不同!"); pb1.Password = ""; pb2.Password = ""; } else if (pwd1.Length < 8) { MessageBox.Show("输入密码过短!"); pb1.Password = ""; pb2.Password = ""; } else if (JudgeEasility(pwd1)) { MessageBox.Show("密码太简单!"); pb1.Password = ""; pb2.Password = ""; } else { MySqlConnection con = Connect.Connection(); MySqlCommand command = new MySqlCommand(); command.Connection = con; command.CommandText = "update login set PassWord = '******' where UserId = '" + Connect.GetUser() + "';"; con.Open(); command.ExecuteNonQuery(); con.Close(); MessageBox.Show("更改成功!"); this.Close(); } }
//点击事件 //========================================================================================================== private void ReturnBook(object sender, RoutedEventArgs e) { Button btn = sender as Button; string name = btn.Name; string[] array = name.Split('_'); name = array[1]; TextBox tb = this.FindName(name) as TextBox; array = tb.Text.Split('|'); string bookname = array[0].Trim(); string writer = array[1].Trim(); MySqlConnection con = Connect.Connection(); MySqlCommand command; MySqlDataReader reader; string cmd = "delete from borrow where BookName = '" + bookname + "' and Writer = '" + writer + "' and UserId = '" + Connect.GetUser() + "';"; try { con.Open(); command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); cmd = "select * from borrow where BookName = '" + bookname + "' and Writer = '" + writer + "' and UserId = '" + Connect.GetUser() + "'; "; command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (!reader.HasRows) { MessageBox.Show("成功还书!"); } Init(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } }
private void btn2_Click(object sender, RoutedEventArgs e) { string User = this.user.Text.Trim(); string Pwd = GetPassword(); MySqlConnection con = Connect.Connection(); if (User == "" || Pwd == "") { this.message.Content = "用户名或密码不能为空."; } else { try { con.Open(); string cmdstr = "select * from login where UserId = '" + User + "' and Password = '******';"; MySqlCommand command = new MySqlCommand(cmdstr, con); MySqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { MessageBox.Show("登录成功!"); UserId = User; con.Close(); this.DialogResult = true; } else { this.message.Content = "用户名或密码错误."; con.Close(); } } catch (MySqlException ex) { MessageBox.Show("数据库连接失败" + ex.Message); } } }
//功能函数区域 //================================================================================================================ private void ButtonBrush() { MySqlConnection con = Connect.Connection(); MySqlDataReader reader; MySqlCommand command; try { con.Open(); string cmd = ""; for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { cmd = "select * from seatbook where x = '" + (i + 1) + "' and y = '" + (j + 1) + "';"; string name = "G_" + (i + 1) + "_" + (j + 1); Button btn = this.FindName(name) as Button; command = new MySqlCommand(cmd, con); reader = command.ExecuteReader(); if (reader.HasRows) { btn.Background = System.Windows.Media.Brushes.Red; } else { btn.Background = System.Windows.Media.Brushes.Green; } reader.Close(); } } } catch (MySqlException ex) { MessageBox.Show(ex.Message); } }//绘制Button的背景色
private void Submit_Click(object sender, RoutedEventArgs e) { SetEnabledFalse(); MySqlConnection con = Connect.Connection(); MySqlCommand command; TextBox textBox; string mark = ""; while (true) { if (ChangeNum == 0) { MessageBox.Show("更新完成!"); break; } con.Open(); for (int i = 0; i < 6; i++) { if (Havechanged[i] != null && Havechanged[i] != "") { mark = Havechanged[i]; Havechanged[i] = null; break; } } textBox = this.FindName(mark) as TextBox; string cmd = "update usermessage set " + textBox.Name.Trim() + " = '" + textBox.Text.ToString().Trim() + "' where UserId = '" + Connect.GetUser() + "';"; command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); con.Close(); ChangeNum--; } this.Change.IsEnabled = true; Havechanged = new string[6]; ChangeNum = 0; SetValues(); }
private void btn1_Click(object sender, RoutedEventArgs e)//注册按钮 { string user = this.Id.Text.Trim(); string pwd1 = GetPassword1(); string pwd2 = GetPassword2(); if (user == "" || pwd1 == "" || pwd2 == "") { MessageBox.Show("不能为空!"); } else if (user == "midpic" || user == "default" || user == "select" || user == "insert" || user == "update" || user == "user" || user == "delete" || user == "drop") { MessageBox.Show("不能以系统关键字为用户名!"); } else if (pwd1 != pwd2) { MessageBox.Show("两次输入密码不同!"); Pwd1.Password = ""; Pwd2.Password = ""; } else if (pwd1.Length < 8) { MessageBox.Show("输入密码过短!"); Pwd1.Password = ""; Pwd2.Password = ""; } else if (JudgeEasility(pwd1)) { MessageBox.Show("密码太简单!"); Pwd1.Password = ""; Pwd2.Password = ""; } else { MySqlConnection con = Connect.Connection(); try { con.Open(); string check = string.Format("select * from login where UserId = '{0}';", user); MySqlCommand command = new MySqlCommand(check, con); MySqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { MessageBox.Show("已存在该用户名!"); Id.Text = ""; Pwd1.Password = ""; Pwd2.Password = ""; con.Close(); } else { reader.Close(); string cmd = string.Format("insert into login values ('{0}','{1}');", user, pwd1); command = new MySqlCommand(cmd, con); command.ExecuteNonQuery(); string sel = string.Format("select * from login where UserId = '{0}' and Password = '******';", user, pwd1); command = new MySqlCommand(sel, con); reader = command.ExecuteReader(); if (reader.HasRows) { this.DialogResult = true; MessageBox.Show("注册成功!"); con.Close(); } else { MessageBox.Show("注册失败!"); con.Close(); } } } catch (MySqlException ex) { MessageBox.Show("数据库连接失败" + ex.Message); con.Close(); } } }
//点击事件 //============================================================================================================================== private void SelectPic_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFile = new OpenFileDialog();//用来打开图片选择窗口 openFile.Filter = "JPEG|*.jpg"; openFile.DefaultExt = "jpg"; openFile.Multiselect = false;//不能多选 Image image = new Image();//将原本image的source储存起来; image.Source = this.Picture.Source; this.Picture.Source = null;//设置image的源文件为空; if (openFile.ShowDialog() == true) { Computer computer = new Computer(); //computer类,先引入microsoft.visualBasic,再using device; string path = "d:/libraryresource/Userpicture/"; bool ishave = false; //用来标记用户之前是否设置过图片; //获取原图片文件 MySqlConnection con = Connect.Connection(); string cmd = "select * from usermessage where UserId = '" + Connect.GetUser() + "';"; MySqlCommand command = new MySqlCommand(cmd, con); MySqlDataReader reader; con.Open(); reader = command.ExecuteReader(); reader.Read(); if (reader["Picture"].ToString() != null && reader["Picture"].ToString().Trim() != "") { string pic = reader["Picture"].ToString().Trim(); computer.FileSystem.RenameFile(path + pic, "midpic.jpg");//把原文件名设置为 midpic.jpg ishave = true; } con.Close(); reader.Close(); string type = System.IO.Path.GetExtension(openFile.FileName); string newname = Connect.GetUser() + type;//设置图片新的名字,以用户名命名 System.IO.File.Copy(openFile.FileName, System.IO.Path.Combine(@path, System.IO.Path.GetFileName(openFile.FileName))); string oldname = System.IO.Path.GetFileName(openFile.FileName); ////string oldpath = path + openFile.FileName; ////File.Move(oldpath, path + name); string oldnamepath = path + oldname; if (oldname != newname) { computer.FileSystem.RenameFile(oldnamepath, newname); } if (ishave == true)//如果之前有记录,不需要更新,图片的名字不变;如果没有,则向数据库插入对应的信息。 { computer.FileSystem.DeleteFile(path + "midpic.jpg"); } else { con.Open(); command.CommandText = "update usermessage set Picture = '" + newname + "' where UserId = '" + Connect.GetUser() + "';"; command.ExecuteNonQuery(); con.Close(); } //pack://SiteOfOrigin:,,,/ setImageSource("d:/libraryresource/Userpicture/" + newname); return; } else { this.Picture.Source = image.Source; return; } }
}//向list1中加入图书数据 private void inquery_Click(object sender, RoutedEventArgs e) { string bookname = this.Bookname.Text.ToString().Trim(); string writer = this.Writer.Text.ToString().Trim(); string cmd = ""; MySqlConnection con = Connect.Connection(); if (bookname == "" && writer == "") { MessageBox.Show("请输入内容!"); return; } else if (bookname == "" && writer != "") { cmd = "select * from book where Writer like '%" + writer + "%"; } else if (writer == "" && bookname != "") { cmd = "select * from book where BookName like '%" + bookname + "%"; } else { cmd = "select * from book where BookName like '%" + bookname + "%' and Writer like '%" + writer + "%"; } cmd = cmd + "';"; try { con.Open(); MySqlCommand command = new MySqlCommand(cmd, con); MySqlDataReader reader = command.ExecuteReader(); list.Items.Clear(); AddHead(); string[] str = new string[5]; while (reader.Read()) { str[0] = reader["BookName"].ToString(); str[1] = reader["Writer"].ToString(); str[2] = reader["Page"].ToString(); str[3] = reader["Public"].ToString(); str[4] = reader["DefaultPrice"].ToString(); string sep1 = Formatting(str[0], 20); string sep2 = Formatting(str[1], 20); string sep3 = Formatting(str[2], 20); string sep4 = Formatting(str[3], 20); string sep5 = Formatting(str[4], 20); string strsub = sep1 + sep2 + sep3 + sep4 + sep5; this.list.Items.Add(strsub); } reader.Close(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } con.Close(); }