//查询数据库所有类型(是否存在文件) //1.路径,名称,类型 //2.反回查询结果 public static AccessType CheckData(string FilePath, string DataType, string MD5) { if (MD5 == "") { MD5 = CodeAll.GetMD5(FilePath); } DataTable dt = GetAccess("select * from " + DataType + " where MD5='" + MD5 + "'"); if (dt == null) { return(null); } AccessType AT = new AccessType(); if (dt.Rows.Count != 0) { AT.Name = dt.Rows[0][1].ToString(); AT.Size = dt.Rows[0][2].ToString(); AT.MD5 = dt.Rows[0][3].ToString(); AT.UpdateTime = dt.Rows[0][4].ToString(); AT.CreatTime = dt.Rows[0][5].ToString(); AT.isDel = dt.Rows[0][6].ToString(); } else { return(null); } return(AT); }
//检查是否存在 public static bool ExistData(string FilePath, string DataType, string MD5) { FileInfo fi = new FileInfo(FilePath); long FileSize = fi.Length; DateTime CreatTime = fi.CreationTime; if (MD5 == "") { MD5 = CodeAll.GetMD5(FilePath); } OleDbCommand command = new OleDbCommand(); command.Connection = conn; OleDbDataReader dr = null; command.CommandText = "select [MD5] from " + DataType + " where [MD5] = '" + MD5 + "'"; try { dr = command.ExecuteReader(); bool Exist = dr.HasRows; return(Exist); } catch (Exception ex) { CodeAll.AddLog("查询数据库失败", FilePath, ex.Message); return(true); } }
//添加数据库 //1.路径,名称,类型,是否删除 //2.校验MD5数值,查询数据库是否存在 //3.存在没标记删除(标记删除并记录),存在已标记删除(报错),不存在(添加) public static bool AddData(string FilePath, string Name, string DataType, string MD5, bool isDel) { if (DataType == "") { CodeAll.AddLog("数据库添加失败", "", "为选择类型"); return(false); } //获取文件信息 FileInfo fi = new FileInfo(FilePath); long FileSize = fi.Length; DateTime CreatTime = fi.CreationTime; if (MD5 == "") { MD5 = CodeAll.GetMD5(FilePath); } if (MD5 == "") { return(false); } //先查询是否存在 存在跳过 不存在添加数据 if (ExistData(FilePath, DataType, MD5) == true) { CodeAll.AddLog("数据库添加失败", FilePath, "已存在"); if (isDel) { UpdateAccess("update " + DataType + " set [isDel] = " + isDel + " where [MD5] = '" + MD5 + "'"); CodeAll.AddLog("数据库标记删除", "", isDel.ToString()); } else { CodeAll.AddLog("跳过", "", ""); } return(false); } else { return(UpdateAccess("insert into " + DataType + "([Name],[Size],[MD5],[UpdateTime],[CreatTime],[isDel]) values ('" + Name.Replace("'", "") + "','" + FileSize + "','" + MD5 + "','" + DateTime.Now + "','" + CreatTime + "'," + isDel + ")")); } }
//检查文件是否存在数据库 private void button10_Click(object sender, EventArgs e) { access.Open(); int n = 0; this.listBoxMD5.Items.Clear(); for (int i = 0; i < this.listBoxFile.Items.Count; i++) { string path = this.listBoxFile.Items[i].ToString(); //校验MD5数值 string MD5 = CodeAll.GetMD5(path); //检查数据库是否存在 string DataType = "G"; access.AccessType AT = access.CheckData(path, DataType, MD5); if (AT == null) { DataType = "F"; AT = access.CheckData(path, DataType, MD5); } if (AT == null) { DataType = "J"; AT = access.CheckData(path, DataType, MD5); } if (AT == null) { DataType = "TV"; AT = access.CheckData(path, DataType, MD5); } if (AT == null)//不存在,添加listboxMD5值 { this.listBoxMD5.Items.Add(MD5); n++; } else//存在,从listboxfile中移除,并添加Datatable { this.listBoxFile.Items.RemoveAt(i); i--; FileInfo fi = new FileInfo(path); string Size = fi.Length.ToString(); string NewName = ""; if (AT.Name != System.IO.Path.GetFileNameWithoutExtension(path)) { NewName = System.IO.Path.GetFileNameWithoutExtension(path); } this.dataGridView1.Rows.Add( AT.isDel, AT.Name, NewName, DataType, GetDataType(), AT.Size, Size, MD5, path ); } this.label2.Text = n.ToString() + "/" + this.listBoxFile.Items.Count.ToString(); Application.DoEvents(); } //检查MD5数量和文件数量 if (this.listBoxMD5.Items.Count != this.listBoxFile.Items.Count) { MessageBox.Show("MD5数据数量和文件不一致"); } //检查MD5是否有重复 List <string> lst = new List <string>(); CheckSameMD5List(); access.Close(); }