using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace AIRS.usercontrol { /// /// Page_UserControl1.xaml 的交互逻辑 /// public partial class Page_UserControl1 : UserControl { public Page_UserControl1() { InitializeComponent(); } #region 数据库连接对象 /// /// 数据库连接对象 /// SQLiteConnection conn = null; /// /// 数据库连接字符串 /// SQLiteConnectionStringBuilder connstr = null; /// /// 当前工作目录 /// private string path = Directory.GetCurrentDirectory(); #endregion #region 分页变量 /// /// 当前页码 /// private int m_Page = 1; /// /// 总条数 /// private int m_Total = 0; /// /// 总页数 /// private int m_Count = 1; /// /// 每页条数 /// private int m_Page_Count = 1; #endregion #region 分页按钮事件 /// /// 上一页 /// /// /// private void pro_btn_Click(object sender, RoutedEventArgs e) { if (m_Page == 1) { MessageBox.Show("已经是第一页!"); return; } m_Page--; GetAllUser(m_Page, m_Page_Count); } /// /// 首页 /// /// /// private void first_btn_Click(object sender, RoutedEventArgs e) { if (m_Page == 1) { MessageBox.Show("已经是第一页!"); return; } m_Page = 1; GetAllUser(m_Page, m_Page_Count); } /// /// 下一页 /// /// /// private void next_btn_Click(object sender, RoutedEventArgs e) { if (m_Page >= m_Count) { MessageBox.Show("已经是最后一页!"); return; } m_Page++; GetAllUser(m_Page, m_Page_Count); } /// /// 尾页 /// /// /// private void last_btn_Click(object sender, RoutedEventArgs e) { if (m_Page >= m_Count) { MessageBox.Show("已经是最后一页!"); return; } m_Page = m_Count; GetAllUser(m_Page,m_Page_Count); } #endregion #region 数据库操作 /// /// 获取数据库连接对象 /// /// private SQLiteConnection GetConnection() { SQLiteConnection con = new SQLiteConnection(); SQLiteConnectionStringBuilder constr = new SQLiteConnectionStringBuilder(); constr.DataSource = path + "\\datas.db"; con.ConnectionString = constr.ToString(); con.Open(); return con; } /// /// 获取所有用户 /// /// 当前页码 /// 每页条数 private void GetAllUser(int page,int count) { try { conn = GetConnection(); string strSql = "select * from t_username where username!='admin' limit " + ((page - 1) * count) + "," + count + ""; SQLiteDataAdapter da = new SQLiteDataAdapter(strSql, conn); DataTable dt = new DataTable(); da.Fill(dt); string strcountSql = "select count(*) as count from t_username where username!='admin' "; da = new SQLiteDataAdapter(strcountSql, conn); DataTable dtcount = new DataTable(); da.Fill(dtcount); if (dtcount.Rows.Count>0) { m_Total = Convert.ToInt32(dtcount.Rows[0][0]);//总条数 m_Count = (int)Math.Ceiling(((double)m_Total /(double) m_Page_Count)); } List list = new List(); for (int i = 0; i < dt.Rows.Count; i++) { RowData dar = new RowData(); dar.username = dt.Rows[i]["username"].ToString(); dar.password = dt.Rows[i]["password"].ToString(); list.Add(dar); } date_grid.DataContext = list; //this.pageuser.FontSize = 36; this.pageuser.Page = m_Page; //this.pageuser.PageCount = m_Count; this.pageuser.PageSize = m_Page_Count; this.pageuser.RecordCount = m_Total; } catch (Exception ex) { } finally { try { conn.Close(); } catch { } } } /// /// 删除指定用户 /// /// private bool DelUserByName(string username) { try { conn = GetConnection(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = "delete from t_username where username=@username"; cmd.Parameters.Add(new SQLiteParameter("@username")); cmd.Parameters["@username"].Value = username; if (cmd.ExecuteNonQuery() > 0) { return true; } else { return false; } } catch (Exception ex) { } finally { try { conn.Close(); } catch { } } return false; } #endregion /// /// 序号列 /// /// /// private void date_grid_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Header = e.Row.GetIndex() + 1; } /// /// 查询按钮 /// /// /// private void btn_query_Copy_Click(object sender, RoutedEventArgs e) { m_Page = 1; GetAllUser(m_Page, m_Page_Count); } /// /// 添加 /// /// /// private void btn_add_Click(object sender, RoutedEventArgs e) { UpUser us = new UpUser(); us.BtnLogin.Content = "添加"; us.titel.Content = "添加检察员"; us.ShowInTaskbar = false; us.conn = GetConnection(); us.ShowDialog(); if (us.DialogResult == true) { MessageBox.Show("添加成功!"); m_Page = 1; GetAllUser(m_Page, m_Page_Count); try { us.conn.Close(); } catch { } } } /// /// 修改用户 /// /// /// private void Button_Click(object sender, RoutedEventArgs e) { string username = ((Button)sender).Tag == null ? "" : ((Button)sender).Tag.ToString(); if (username.Equals("admin")) { MessageBox.Show("管理员不可以修改!"); return; } UpUser us = new UpUser(); us.BtnLogin.Content = "修改"; us.titel.Content = "修改检察员"; us.ShowInTaskbar = false; us.username = username; us.conn = GetConnection(); us.cbbLoginname.Text = username; us.ShowDialog(); if (us.DialogResult == true) { MessageBox.Show("修改成功!"); m_Page = 1; GetAllUser(m_Page, m_Page_Count); try { us.conn.Close(); } catch { } } } /// /// 删除 /// /// /// private void Button_Click_1(object sender, RoutedEventArgs e) { string username = ((Button)sender).Tag == null ? "" : ((Button)sender).Tag.ToString(); if (username.Equals("admin")) { MessageBox.Show("管理员不可以删除!"); return; } if (DelUserByName(username)) { MessageBox.Show("删除成功!"); m_Page = 1; GetAllUser(m_Page, m_Page_Count); } else { MessageBox.Show("删除失败!"); } } /// /// 窗体加载完后执行 /// /// /// private void UserControl_Loaded(object sender, RoutedEventArgs e) { this.pageuser.PageChanged += new EventHandler(Eventpage); GetAllUser(m_Page, m_Page_Count); } /// /// 自定义分页事件 /// /// /// public void Eventpage(object sender, PageChangedEventArgs a) { m_Page = a.Page; GetAllUser(m_Page, m_Page_Count); } } #region 显示列类 /// /// 显示列类 /// public class RowData { /// /// 用户名 /// public string username { get; set; } /// /// 密码 /// public string password { get; set; } } #endregion }