/// <summary> /// 初始化学生消息界面 /// </summary> public InfSendStu() { InitializeComponent(); WindowStartupLocation = WindowStartupLocation.CenterScreen; //建立定时器,时间间隔为1s,并启动定时器 timer.Tick += new EventHandler(timer_Tick); timer.Interval = new TimeSpan(0, 0, 1); timer.Start(); string n = MainWindow.UserCode; //UserNameStu为登录用户名 string UserNameStu = n.ToString(); //Time为现在的时间 DateTime Time = DateTime.Now; DateTime SetTime = new DateTime(); TimeSpan dt; //定义Information类型的结构体Stu Information Stu = new Information(); #region 连接数据库 SqlConnection myConnection; string connStr = "Server ='" + IPAddress.ip + "'; database =测试数据库; uid =sa; pwd=1738010002"; //string connStr = @"Server = LAPTOP-25MJ4H0M\SQLEXPRESS; database =教学系统; Trusted_Connection=SSPI"; myConnection = new SqlConnection(connStr); try { myConnection.Open(); } catch (Exception) { MessageBox.Show("连接失败"); } #endregion string myUpdate = "update xxb set 状态 = '" + 1 + "' where 接收方 = '" + UserNameStu + "'"; SqlCommand myCom = new SqlCommand(myUpdate, myConnection); myCom.ExecuteNonQuery(); //根据UserNameStu(登录用户名)选择出对应的个人信息 SqlCommand cmd = new SqlCommand("select 发送方,消息内容,时间 from xxb where 接收方 = '" + UserNameStu + "'", myConnection); SqlDataReader sdr = cmd.ExecuteReader(); //读取信息并对Stu结构体赋值 while (sdr.Read()) { SetTime = DateTime.Parse(sdr[2].ToString()); if (sdr[0].ToString().Trim().Equals("定时提醒") == true) { try { dt = Time - SetTime; if (dt.Minutes >= 01) { Stu.Sender = sdr[0].ToString(); //将发送人姓名赋值 Stu.Info = sdr[1].ToString(); //将信息内容赋值 Stu.Time = DateTime.Parse(sdr[2].ToString().Trim()); infoList1.Add(Stu); //将Stu的内容返回给infoList1 } } catch { } } else { Stu.Sender = sdr[0].ToString(); //将发送人姓名赋值 Stu.Info = sdr[1].ToString(); //将信息内容赋值 Stu.Time = DateTime.Parse(sdr[2].ToString().Trim()); infoList1.Add(Stu); //将Stu的内容返回给infoList1 } } infoList1.Sort(delegate(Information x, Information y) { return(y.Time.CompareTo(x.Time)); }); DG2.AutoGenerateColumns = false; DG2.ItemsSource = infoList1; //对DataGrid的文本框赋值 }
private void Button_Click(object sender, RoutedEventArgs e) { string n = MainWindow.UserCode; //UserNameStu为登录用户名 string UserNameStu = n.ToString(); //定义Information类型的结构体Stu DateTime Time = DateTime.Now; DateTime SetTime = new DateTime(); TimeSpan dt; #region 连接数据库 Information Stu = new Information(); SqlConnection myConnection; string connStr = "Server ='" + IPAddress.ip + "'; database =测试数据库; uid =sa; pwd=1738010002"; //string connStr = @"Server = LAPTOP-25MJ4H0M\SQLEXPRESS; database =教学系统; Trusted_Connection=SSPI"; myConnection = new SqlConnection(connStr); try { myConnection.Open(); } catch (Exception) { MessageBox.Show("连接失败"); } #endregion //根据UserNameStu(登录用户名)选择出对应的个人信息 SqlCommand cmd = new SqlCommand("select 发送方,消息内容,时间 from xxb where 接收方 = '" + UserNameStu + "'", myConnection); SqlDataReader sdr = cmd.ExecuteReader(); //清空infoList1的内容 infoList1.Clear(); //读取信息并对Stu结构体赋值 while (sdr.Read()) { SetTime = DateTime.Parse(sdr[2].ToString()); if (sdr[0].ToString().Trim().Equals("定时提醒")) { try { dt = Time - SetTime; if (dt.Minutes >= 0) { Stu.Sender = sdr[0].ToString(); //将发送人姓名赋值 Stu.Info = sdr[1].ToString(); //将信息内容赋值 Stu.Time = DateTime.Parse(sdr[2].ToString().Trim()); infoList1.Add(Stu); //将Stu的内容返回给infoList1 } } catch { } } else { Stu.Sender = sdr[0].ToString(); //将发送人姓名赋值 Stu.Info = sdr[1].ToString(); //将信息内容赋值 Stu.Time = DateTime.Parse(sdr[2].ToString().Trim()); infoList1.Add(Stu); //将Stu的内容返回给infoList1 } } sdr.Close(); infoList1.Sort(delegate(Information x, Information y) { return(y.Time.CompareTo(x.Time)); }); DG2.AutoGenerateColumns = false; DG2.ItemsSource = null; //将DataGrid清空 DG2.ItemsSource = infoList1; //对DataGrid的文本框赋值 }