using System; using System.Collections.Generic; using System.Configuration; 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.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace AIRS { /// /// Sample_Information_Enter.xaml 的交互逻辑 /// public partial class Sample_Information_Enter : Window { public Sample_Information_Enter() { InitializeComponent(); } public delegate void MB(); /// /// 移除蒙版 /// public static event MB MBEvent; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); private string path = Directory.GetCurrentDirectory();//工作路径 //数据库 SQLiteConnection conn = new SQLiteConnection(); SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder(); SQLiteDataAdapter da = new SQLiteDataAdapter(); private int count = 10;//设定count几个就删除多余的以外的。假设设置为5那么大于5条的都会被删除 List list_Image_Names = new List();//图片路径名称列表(绝对路径) public string lbl_Inspection_project;//检验项目 public string comb_Number_prefix;//编号前缀 public string comb_Number_suffix;//编号后缀 public string comb_Inspection_instrument;//检验仪器 public string txt_Picture_catalog;//图片目录 //E:/images/img1.png.... public string grade_steel;//钢种 public string grade_standard;//标准 public string test_item;//检验项目 public void BLFZ() { if(input_Number_prefix.Text!=""&&input_Inspection_project.Content.ToString()!=""&&input_Inspection_instrument.Text != "" && input_Picture_catalog.Text !="") { lbl_Inspection_project = input_Inspection_project.Content.ToString(); // comb_Number_prefix = input_Number_prefix.Text; comb_Number_prefix = input_Number_prefix.Text; comb_Number_suffix = input_Number_suffix.Text; comb_Inspection_instrument = input_Inspection_instrument.Text; txt_Picture_catalog = Common.removeControlChars(input_Picture_catalog.Text);//lbf 2020/10/28 去除控制字符 //编号前缀 BHQZ(); //编号后缀 BHHZ(); //检验仪器 JYYQ(); MBEvent(); this.DialogResult = true;//或者等于fa } else { System.Windows.MessageBox.Show("请完善信息后点击确定!","警告",MessageBoxButton.OK,MessageBoxImage.Warning); } } /// /// 取消 /// /// /// private void btnCanel_Click(object sender, RoutedEventArgs e) { MBEvent(); Clear_SampleInfo(); this.DialogResult = false; // this.Close(); } /// /// 确定 /// /// /// private void btnOK_Click(object sender, RoutedEventArgs e) { BLFZ(); } /// /// 编号前缀 编辑删除增加 /// public void BHQZ() { if (input_Number_prefix.Text != "") { //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString(); int id = SelectDataWhereValue(lblnumberp.Content.ToString(), input_Number_prefix.Text); //Sample_Information_Enter.MBEvent(); if (id > -1) { FuncUpdateData(id); //System.Windows.MessageBox.Show("做更新"); } else { FuncInsertData(lblnumberp.Content.ToString(), input_Number_prefix.Text); //System.Windows.MessageBox.Show("做插入"); } //table = getDataTable(lblnumbers.Content.ToString()); //deleteDataTable(table); deleteDataTable(getDataTable(lblnumberp.Content.ToString())); //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString()); input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString()); } //else //{ // //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString()); // // System.Windows.MessageBox.Show("请输入" + lblnumberp.Content.ToString().Substring(0, lblnumberp.Content.ToString().Length - 1)); //} } /// /// 编号后缀 编辑删除增加 /// public void BHHZ() { if (input_Number_suffix.Text != "") { //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString(); int id = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text); //Sample_Information_Enter.MBEvent(); if (id > -1) { FuncUpdateData(id); //System.Windows.MessageBox.Show("做更新"); } else { FuncInsertData(lblnumbers.Content.ToString(), input_Number_suffix.Text); //System.Windows.MessageBox.Show("做插入"); } //table = getDataTable(lblnumbers.Content.ToString()); //deleteDataTable(table); deleteDataTable(getDataTable(lblnumbers.Content.ToString())); input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString()); } //else //{ // input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString()); // System.Windows.MessageBox.Show("请输入" + lblnumbers.Content.ToString().Substring(0, lblnumbers.Content.ToString().Length - 1)); //} } /// /// 检验仪器 编辑删除增加 /// public void JYYQ() { if (input_Inspection_instrument.Text != "") { //input_Inspection_instrument.Text = SelectDataWhereValue(lblnumbers.Content.ToString(), input_Number_suffix.Text).ToString(); int id = SelectDataWhereValue(lblInspection_instrument.Content.ToString(), input_Inspection_instrument.Text); //Sample_Information_Enter.MBEvent(); if (id > -1) { FuncUpdateData(id); //System.Windows.MessageBox.Show("做更新"); } else { FuncInsertData(lblInspection_instrument.Content.ToString(), input_Inspection_instrument.Text); //System.Windows.MessageBox.Show("做插入"); } //table = getDataTable(lblnumbers.Content.ToString()); //deleteDataTable(table); deleteDataTable(getDataTable(lblInspection_instrument.Content.ToString())); input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString()); } else { input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString()); System.Windows.MessageBox.Show("请输入" + lblInspection_instrument.Content.ToString().Substring(0, lblInspection_instrument.Content.ToString().Length - 1)); } } /// /// 查询数据 /// /// /// public void SelectDataCompare(string key, string value) { //connstr.DataSource = path + "\\datas.db"; //conn.ConnectionString = connstr.ToString(); try { conn.Open(); SQLiteCommand cmd = new SQLiteCommand(conn); cmd.CommandText = "SELECT * FROM t_Parameter WHERE key='" + key + "'"; SQLiteDataReader sr = cmd.ExecuteReader(); while (sr.Read()) { // Console.WriteLine($"{sr.GetString(1)},{sr.GetDateTime(5).ToString()}"); // Console.WriteLine($"{sr.GetInt32(0)},{sr.GetString(1)},{sr.GetString(2)},{sr.GetString(3)}"); } conn.Close(); } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } DataTable table = new DataTable(); /// /// combox绑定数据 /// public List DataTableSelectData(string key) { List comboxlist = new List(); try { conn.Open(); //查询钢种 da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'", conn); DataTable ds = new DataTable(); da.Fill(ds); DataView dataView = ds.DefaultView; // dataView.Sort = "time asc";//正序从小到大 dataView.Sort = "time desc";//逆序从大到小 ds = dataView.ToTable(); // deleteDataTable(ds); for (int i = 0; i < ds.Rows.Count; i++) { comboxlist.Add(ds.Rows[i][2].ToString()); // Console.WriteLine("看看" + i + " " + ds.Rows[i][0] + " " + ds.Rows[i][1] + " " + ds.Rows[i][2] + " " + ds.Rows[i][3]); } conn.Close(); table = ds; } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } return comboxlist; } /// /// 根据key(编号前缀/编号后缀/检验仪器)条件返回的DataTable /// /// /// public DataTable getDataTable(string key) { DataTable ds = new DataTable(); try { conn.Open(); //查询钢种 da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'", conn); da.Fill(ds); conn.Close(); DataView dataView = ds.DefaultView; // dataView.Sort = "time asc";//正序从小到大 dataView.Sort = "time desc";//逆序从大到小 ds = dataView.ToTable(); } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } return ds; } /// /// 查询数据返回编号 /// public int SelectDataWhereValue(string key, string value) { int id = -1; try { conn.Open(); da = new SQLiteDataAdapter("SELECT * FROM t_Parameter WHERE key='" + key + "'and value='" + value + "'", conn); DataTable ds = new DataTable(); da.Fill(ds); for (int i = 0; i < ds.Rows.Count; i++) { id = Convert.ToInt32(ds.Rows[i][0]); } conn.Close(); } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } return id; } /// /// 执行SQL语句,返回影响的记录数 /// /// SQL语句 /// 影响的记录数 public int ExecuteSql(string SQLString) { int rows=-1; try { using (SQLiteCommand cmd = new SQLiteCommand(SQLString, conn)) { try { conn.Open(); rows = cmd.ExecuteNonQuery(); //conn.Dispose(); conn.Close(); return rows; } catch (System.Data.SQLite.SQLiteException E) { // conn.Close(); throw new Exception(E.Message); } } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } return rows; } /// /// 添加数据 /// /// /// public void FuncInsertData(string key, string value) { //Random random = new Random(); //int id = random.Next(); try { if (value != "") { //string sqlstr = "INSERT INTO t_Parameter (key,value,time) VALUES ('" + key + "','" + value + "','" + DateTime.Now.ToString() + "')"; string sqlstr = "INSERT INTO t_Parameter (key,value,time) VALUES ('" + key + "','" + value + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')"; int i = ExecuteSql(sqlstr); if (i > 0) { //System.Windows.MessageBox.Show("添加了一条数据 " + key + " " + value); } } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 更新数据 /// /// public void FuncUpdateData(int id) { try { // string sqlstr = "update t_Parameter set time='" + DateTime.Now.ToString() + "' where id='" + id + "'"; string sqlstr = "update t_Parameter set time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where id='" + id + "'"; int i = ExecuteSql(sqlstr); if (i > 0) { //System.Windows.MessageBox.Show("更新了 " + id + " 的时间"); } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 删除数据当大于10条数据就删除 /// /// public void FuncDeleteData(int id) { try { string sqlstr = "delete from t_Parameter where id='" + id + "'"; int i = ExecuteSql(sqlstr); if (i > 0) { // System.Windows.MessageBox.Show("删除了 " + id); } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// /// 删除多出10条以上的所有 /// /// public void deleteDataTable(DataTable dt) { try { //设定count几个就删除多余的以外的。假设设置为5那么大于5条的都会被删除,显示最多也是五个 if (dt.Rows.Count > count) { DataView dataView = dt.DefaultView; // dataView.Sort = "time asc";//正序从小到大 dataView.Sort = "time desc";//逆序从大到小 dt = dataView.ToTable(); for (int i = 0; i < dt.Rows.Count; i++) { // i = i + count; if (i >= count) { FuncDeleteData(Convert.ToInt32(dt.Rows[i][0])); } } } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 清空输入信息 /// private void Clear_SampleInfo() { input_Inspection_project.Content = ""; input_Number_prefix.Text = ""; input_Number_suffix.Text = ""; input_Inspection_instrument.Text = ""; input_Picture_catalog.Text = ""; } /// /// 打开文件 /// /// /// private void openfiles() { try { List piclist = new List(); System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog(); dialog.SelectedPath = txt_Picture_catalog; if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel) { txt_Picture_catalog = dialog.SelectedPath; WriteApp("folderpath", txt_Picture_catalog); DirectoryInfo directoryInfo = new DirectoryInfo(txt_Picture_catalog); string jianyangname = directoryInfo.Name; input_Number_prefix.Text = jianyangname; DirectoryInfo TheFolder = new DirectoryInfo(txt_Picture_catalog); FileSystemInfo fileinfo1 = TheFolder as FileSystemInfo; input_Picture_catalog.Text = fileinfo1.ToString();//图片目录文件夹名 //遍历文件 foreach (FileInfo NextFile in TheFolder.GetFiles()) { if (System.IO.Path.GetExtension(NextFile.FullName).ToLower() == ".jpg" || System.IO.Path.GetExtension(NextFile.FullName).ToLower() == ".bmp") { //Console.WriteLine("111" + NextFile.Name);//图片名 //将文件夹下的图像文件添加到文件列表中 piclist.Add(NextFile.FullName); // Console.WriteLine("000"+NextFile.FullName);//带路径的图片名 } } if (piclist.Count == 0) { input_Picture_catalog.Text ="";//图片目录文件夹名 System.Windows.MessageBox.Show("您选择了没有图片的文件夹哦!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } list_Image_Names = piclist; //if (piclist.Count == 0) //{ // System.Windows.MessageBox.Show("您选择了没有图片的文件夹哦!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning); //} } catch (Exception e) { input_Picture_catalog.Text = ""; System.Windows.MessageBox.Show(e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } /// /// 浏览文件夹 /// /// /// private void btnBrowseFolders_Click(object sender, RoutedEventArgs e) { openfiles(); } /// /// 试样信息绑定数据源 /// public void BangdingComboxDataSource() { input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString()); //input_Number_prefix.ItemsSource = DataTableSelectData(lblnumberp.Content.ToString()); input_Number_prefix.SelectedIndex = 0; input_Number_suffix.ItemsSource = DataTableSelectData(lblnumbers.Content.ToString()); input_Number_suffix.SelectedIndex = 0; input_Inspection_instrument.ItemsSource = DataTableSelectData(lblInspection_instrument.Content.ToString()); input_Inspection_instrument.SelectedIndex = 0; } /// /// 写入文件夹 /// /// /// public void WriteApp(string NodeName, string value) { config.AppSettings.Settings[NodeName].Value = value; config.Save(ConfigurationSaveMode.Modified);// 保存在APP文件中的更改 ConfigurationManager.RefreshSection("appSettings");//强制重新加载更改的部分 } /// /// 装载事件 /// /// /// private void Window_Loaded(object sender, RoutedEventArgs e) { txt_Picture_catalog =Common.removeControlChars( ConfigurationManager.AppSettings["folderpath"]); connstr.DataSource = path + "\\datas.db"; conn.ConnectionString = connstr.ToString(); input_Inspection_project.Content = grade_standard + " " + test_item; BangdingComboxDataSource(); } } }