123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- 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
- {
- /// <summary>
- /// Sample_Information_Enter.xaml 的交互逻辑
- /// </summary>
- public partial class Sample_Information_Enter : Window
- {
- public Sample_Information_Enter()
- {
- InitializeComponent();
- }
- public delegate void MB();
- /// <summary>
- /// 移除蒙版
- /// </summary>
- 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<string> list_Image_Names = new List<string>();//图片路径名称列表(绝对路径)
- 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);
- }
- }
- /// <summary>
- /// 取消
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCanel_Click(object sender, RoutedEventArgs e)
- {
- MBEvent();
- Clear_SampleInfo();
- this.DialogResult = false;
- // this.Close();
- }
- /// <summary>
- /// 确定
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnOK_Click(object sender, RoutedEventArgs e)
- {
- BLFZ();
-
- }
- /// <summary>
- /// 编号前缀 编辑删除增加
- /// </summary>
- 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));
- //}
- }
- /// <summary>
- /// 编号后缀 编辑删除增加
- /// </summary>
- 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));
- //}
- }
- /// <summary>
- /// 检验仪器 编辑删除增加
- /// </summary>
- 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));
- }
- }
- /// <summary>
- /// 查询数据
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- 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();
- /// <summary>
- /// combox绑定数据
- /// </summary>
- public List<string> DataTableSelectData(string key)
- {
- List<string> comboxlist = new List<string>();
- 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;
- }
- /// <summary>
- /// 根据key(编号前缀/编号后缀/检验仪器)条件返回的DataTable
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 查询数据返回编号
- /// </summary>
- 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;
- }
-
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- 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;
- }
- /// <summary>
- /// 添加数据
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- 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);
- }
-
- }
- /// <summary>
- /// 更新数据
- /// </summary>
- /// <param name="id"></param>
- 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);
- }
- }
- /// <summary>
- /// 删除数据当大于10条数据就删除
- /// </summary>
- /// <param name="id"></param>
- 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);
- }
-
- }
- /// <summary>
- ///
- /// 删除多出10条以上的所有
- /// </summary>
- /// <param name="dt"></param>
- 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);
- }
- }
- /// <summary>
- /// 清空输入信息
- /// </summary>
- private void Clear_SampleInfo()
- {
- input_Inspection_project.Content = "";
- input_Number_prefix.Text = "";
- input_Number_suffix.Text = "";
- input_Inspection_instrument.Text = "";
- input_Picture_catalog.Text = "";
- }
- /// <summary>
- /// 打开文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void openfiles()
- {
- try
- {
- List<string> piclist = new List<string>();
- 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);
- }
- }
- /// <summary>
- /// 浏览文件夹
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnBrowseFolders_Click(object sender, RoutedEventArgs e)
- {
- openfiles();
- }
- /// <summary>
- /// 试样信息绑定数据源
- /// </summary>
- 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;
- }
- /// <summary>
- /// 写入文件夹
- /// </summary>
- /// <param name="NodeName"></param>
- /// <param name="value"></param>
- public void WriteApp(string NodeName, string value)
- {
- config.AppSettings.Settings[NodeName].Value = value;
- config.Save(ConfigurationSaveMode.Modified);// 保存在APP文件中的更改
- ConfigurationManager.RefreshSection("appSettings");//强制重新加载更改的部分
- }
- /// <summary>
- /// 装载事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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();
- }
- }
- }
|