using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OracleClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace PDM { public partial class BaseInfo : Form { public BaseInfo() { InitializeComponent(); this.tbxFilePath.Text = "d:/TestPicture/ln/"; this.hash.Text = "d:/TestPicture/ln/"; this.server.Text = "PDMFiles/FMSC/"; } List<SpareBaseInfo> SpareBaseInfos = null; List<SpareBaseInfo> SpareBaseInfoDataBases = null; List<SpareBaseInfo> SpareBaseInfoisexist= null; StreamWriter sw = File.AppendText(Environment.CurrentDirectory + "\\SpareBaseInfoFile.txt"); private void UpdateDataBase_Click(object sender, EventArgs e) { try { var updatesql = new StringBuilder(); SpareBaseInfos = new List<SpareBaseInfo>(); SpareBaseInfoDataBases = new List<SpareBaseInfo>(); SpareBaseInfoisexist = new List<SpareBaseInfo>(); foreach(var item in Directory.GetFiles(this.tbxFilePath.Text)) { var extenname = Path.GetFileName(item); var name = Path.GetFileNameWithoutExtension(item); if(name.Contains("#")) { string[] codename = name.Split(‘#‘); SpareBaseInfo sbi = new SpareBaseInfo(); sbi.Code = codename[0].ToString(); sbi.Name = codename[1].ToString(); sbi.FilePath = this.server.Text + extenname; if(!SpareBaseInfos.Contains(sbi)) { SpareBaseInfos.Add(sbi); } updatesql.AppendLine("update SpareBaseInfo set FilePaths=‘" + server.Text + extenname + "‘" + "where code=‘" + sbi.Code + "‘ and name= ‘" + sbi.Name + "‘;"); if(!File.Exists(this.hash.Text + extenname.GetHashCode())) { Directory.CreateDirectory(this.hash.Text + Math.Abs(extenname.GetHashCode()) % 1000); } File.Copy(item, this.hash.Text + Math.Abs(extenname.GetHashCode()) % 1000 + "/" + extenname); } } sw.Write(updatesql.ToString()); MessageBox.Show("成功"); sw.Close(); } catch(Exception ex) { throw ex; } string sql = "select * from SpareBaseInfo where code in ("; var Codes = SpareBaseInfos.Select(l => l.Code); foreach(var item in Codes) { sql += "‘" + item + "‘,"; } if(!string.IsNullOrEmpty(sql)) { sql = sql.Remove(sql.Length - 1); } sql += ")"; sql += "and name in("; var Names = SpareBaseInfos.Select(l => l.Name); foreach(var item in Names) { sql += "‘" + item + "‘,"; } if(!string.IsNullOrEmpty(sql)) { sql = sql.Remove(sql.Length - 1); } sql += ")"; string conn = "user id=mfepc;data source=" + server.Text + ";password=mfepc"; OracleConnection cn = new OracleConnection(conn); cn.Open();//要打开连接 string strcmd = sql; OracleCommand cmd = new OracleCommand(strcmd, cn); OracleDataReader reader = cmd.ExecuteReader(); try { while(reader.Read()) { var a = reader[0].ToString(); SpareBaseInfo sbi = new SpareBaseInfo(); sbi.Id = Convert.ToInt32(reader["Id"].ToString()); sbi.Code = reader["Code"].ToString(); sbi.Name = reader["Name"].ToString(); if(!SpareBaseInfoDataBases.Contains(sbi)) { SpareBaseInfoDataBases.Add(sbi); } } dataGridView1.DataSource = SpareBaseInfoDataBases; } finally { reader.Close(); } var updatesql = new StringBuilder(); try { if(MessageBox.Show(SpareBaseInfoDataBases.Count() + "条数据是否继续", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { updatesql.AppendLine("begin"); foreach(var item in SpareBaseInfoDataBases) { item.FilePath = SpareBaseInfos.FirstOrDefault(l => l.Name == item.Name && l.Code == item.Code).FilePath; updatesql.AppendLine("update SpareBaseInfo set FilePaths=‘" + item.FilePath + "‘" + "where code=‘" + item.Code + "‘ and name= ‘" + item.Name + "‘;"); } updatesql.AppendLine("end;"); //OracleCommand cmdupdate = new OracleCommand(updatesql.ToString(), cn); //cmdupdate.ExecuteNonQuery(); MessageBox.Show("成功"); } } catch(Exception ecp) { throw ecp; } cn.Close(); } } public class SpareBaseInfo { private int id; public int Id { get { return id; } set { id = value; } } private string code; public string Code { get { return code; } set { code = value; } } private string name; public string Name { get { return name; } set { name = value; } } private string filePath; public string FilePath { get { return filePath; } set { filePath = value; } } //通过DataSet来读取数据: //创建和数据库的连接192.168.17.42/epcv2 //OracleConnection oraCon = new OracleConnection("user id=mfepc0521;data source=192.168.17.42/mfdmstest;password=mfepc0521"); //OracleConnection oraCon = new OracleConnection("user id=mfepc;data source=192.168.17.42/epcv2;password=mfepc"); //OracleDataAdapter oraDap = new OracleDataAdapter("select * from SpareBaseInfo where rownum<10", oraCon); //DataSet ds = new DataSet(); //oraDap.Fill(ds); //DataTable _table = ds.Tables[0]; //int count = _table.Rows.Count; //dataGridView1.DataSource = _table; } }
时间: 2024-10-07 17:45:37