private void frm_SaveHandle(FtpFile obj) { if (editType == EditType.Add) { //定义变量 DataRow row = ds.Tables[0].NewRow(); //增加一行 SetRow(obj, row); ds.Tables[0].Rows.Add(row); } else if (editType == EditType.Modify) { object[] keys = new object[] { obj.PrimaryID }; DataRow row = ds.Tables[0].Rows.Find(keys); if (row == null) { MessageBox.Show("查找项目出错!"); return; } else { SetRow(obj, row); } } ds.Tables[0].AcceptChanges(); }
/// <summary> /// 修改 /// </summary> private void Modify() { editType = EditType.Modify; //修改 #region 获取信息 FtpFile obj = new FtpFile(); obj.PrimaryID = this.lbPrimaryID.Text; obj.FileName = this.lbFileName.Text; obj.LocalDirectory = this.lbFilePath.Text; obj.FileVersion = this.lbFileVersion.Text; obj.OperCode = this.lbOperCode.Text; obj.OperDate = System.DateTime.Now; //string strSql = "select t.file_content from com_downloadfile t where t.primary_id = '{0}'"; //strSql = string.Format(strSql,obj.PrimaryID); //System.Data.DataSet ds = mgr.SelectData(con,strSql); //byte [] blob = null; //foreach(DataRow row in ds.Tables[0].Rows) //{ // blob = (byte [])row[0]; //} //obj.FileContent = blob; modified by xipeter 20120831 更新文件时,没有必要得到文件内容 #endregion frmInfo frm = new frmInfo(); frm.Con = con; frm.SetInfo(obj); frm.myEditType = EditType.Modify; //修改 frm.SaveHandle += new AutoUpdate.frmInfo.SaveDelegate(frm_SaveHandle); frm.ShowDialog(); }
/// <summary> /// 获取文件 /// </summary> /// <param name="con"></param> /// <param name="FileID">主键</param> /// <param name="FileName">文件名</param> /// <returns></returns> public int DownLoadFile(OracleConnection con, string FileID, string FileName) { FtpFile ftpObj = new FtpFile(); string strSql = "SELECT primary_id, file_name,local_directory,file_content, file_version, oper_code, oper_date FROM com_downloadfile t WHERE t.primary_id = '{0}'"; strSql = string.Format(strSql, FileID); System.Data.DataSet ds = SelectData(con, strSql); if (ds == null) { Err = "查询需要更新的文件失败"; return(-1); } if (ds.Tables.Count == 0) { return(1); } if (ds.Tables[0].Rows.Count == 0) { return(1); } #region 载文件 wait.lblTip.Text = "开始下载程序....."; foreach (DataRow row in ds.Tables[0].Rows) { ftpObj.PrimaryID = row[0].ToString(); ftpObj.FileName = row[1].ToString(); ftpObj.LocalDirectory = row[2].ToString(); ftpObj.LocalDirectory = ConvertPath(ftpObj.LocalDirectory); //转换路径 ftpObj.FileVersion = row[4].ToString(); ftpObj.OperCode = row[5].ToString(); try { //读取文件 byte [] data = (byte [])row["file_content"]; DirectoryCheckAndCreate(FileName); //下载到本地的名称 string LocalFileName = FileName; if (File.Exists(LocalFileName)) { System.IO.File.SetAttributes(LocalFileName, System.IO.FileAttributes.Normal); } FileStream fs = new FileStream(LocalFileName, System.IO.FileMode.Create); int arraysize = new int(); //注意这句话 arraysize = data.Length; fs.Write(data, 0, arraysize); fs.Close(); } catch (Exception ex) { this.Err = ex.Message; return(-1); } } //关闭连接 con.Close(); #endregion return(1); }
/// <summary> /// 赋值 /// </summary> /// <param name="obj"></param> /// <param name="row"></param> private void SetRow(FtpFile obj, DataRow row) { row["主键"] = obj.PrimaryID; row["文件名"] = obj.FileName; row["本地路径"] = obj.LocalDirectory; row["版本号"] = obj.FileVersion; row["操作员"] = obj.OperCode; row["操作日期"] = obj.OperDate; }
/// <summary> /// 赋值 /// </summary> /// <param name="file"></param> /// <returns></returns> public int SetInfo(FtpFile file) { FileList = new ArrayList(); this.lbID.Text = file.PrimaryID; this.txtFileName.Text = file.FileName; this.txtFilePath.Text = file.LocalDirectory; this.txtVision.Text = file.FileVersion; FileList.Add(file); // return(1); }
/// <summary> /// 获取信息 /// </summary> /// <returns></returns> private FtpFile GetInfo() { FtpFile obj = new FtpFile(); obj.PrimaryID = this.lbID.Text; obj.FileName = this.txtFileName.Text; obj.FileVersion = this.txtVision.Text; obj.LocalDirectory = this.txtFilePath.Text; obj.OperCode = "man"; return(obj); }
/// <summary> /// 判断同一目录下是否存在同名文件 /// </summary> /// <param name="con"></param> /// <returns>1 存在 ,0 不存在 ,-1 出错 </returns> public int IsExistPrimaryKey(OracleConnection con, FtpFile obj) { string strSql = "SELECT primary_id, file_name,local_directory,file_content, file_version, oper_code, oper_date FROM com_downloadfile t WHERE t.file_name = '{0}' and local_directory = '{1}' "; strSql = string.Format(strSql, obj.FileName, obj.LocalDirectory); System.Data.DataSet ds = SelectData(con, strSql); if (ds == null) { this.Err = "判断同一目录下是否存在同名文件失败"; return(-1); } if (ds.Tables.Count == 0) { return(0); } if (ds.Tables[0].Rows.Count == 0) { return(0); } bool Result = false; foreach (DataRow row in ds.Tables[0].Rows) { try { if (row[0].ToString() != obj.PrimaryID) //文件名 路径相同 id不同 { Result = true; } } catch (Exception ex) { this.Err = ex.Message; return(-1); } } if (Result) { return(1); } else { return(0); } }
/// <summary> /// 获取文件 /// </summary> /// <param name="con"></param> /// <returns></returns> public int DownLoadFile(OracleConnection con) { Process[] proc = Process.GetProcessesByName("His"); if (proc.Length >= 1) { if (MessageBox.Show("当前系统中有JinYiGroupStation在运行!\n必须关掉才能更新系统!是否关闭进行更新!", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { for (int i = 0; i < proc.Length; i++) { proc[i].Kill(); } } else { Application.Exit(); } } string filePath = Application.StartupPath; FtpFile ftpObj = new FtpFile(); string strSql = "SELECT primary_id, file_name,local_directory,file_content, file_version, oper_code, oper_date FROM com_downloadfile t WHERE t.oper_date > to_date('{0}','yyyy-mm-dd hh24:mi:ss')"; string tempLastTime = ReadConfig("UpdateTime"); //上次更新时间 strSql = string.Format(strSql, tempLastTime); wait.lblTip.Text = "正在查询数据....."; System.Windows.Forms.Application.DoEvents(); wait.Refresh(); System.Data.DataSet ds = SelectData(con, strSql); if (ds == null) { Err = "查询需要更新的文件失败"; return(-1); } if (ds.Tables.Count == 0) { return(1); } if (ds.Tables[0].Rows.Count == 0) { return(1); } wait.lblTip.Text = "正在下载,请等待。。。。"; System.Windows.Forms.Application.DoEvents(); wait.Refresh(); wait.progressBar1.Maximum = ds.Tables[0].Rows.Count; wait.progressBar1.Minimum = 0; #region 载文件 foreach (DataRow row in ds.Tables[0].Rows) { ftpObj.PrimaryID = row[0].ToString(); ftpObj.FileName = row[1].ToString(); ftpObj.LocalDirectory = row[2].ToString(); ftpObj.LocalDirectory = ConvertPath(ftpObj.LocalDirectory); //转换路径 ftpObj.FileVersion = row[4].ToString(); ftpObj.OperCode = row[5].ToString(); wait.lblTip.Text = "正在下载" + ftpObj.FileName; wait.lblTip.Refresh(); wait.progressBar1.Value++; wait.progressBar1.Refresh(); System.Windows.Forms.Application.DoEvents(); try { //读取文件 byte [] data = (byte [])row["file_content"]; DirectoryCheckAndCreate(filePath + ftpObj.LocalDirectory); //下载到本地的名称 string LocalFileName = filePath + ftpObj.LocalDirectory + ftpObj.FileName; if (File.Exists(LocalFileName)) { System.IO.File.SetAttributes(LocalFileName, System.IO.FileAttributes.Normal); } FileStream fs = new FileStream(LocalFileName, System.IO.FileMode.Create); int arraysize = new int(); //注意这句话 arraysize = data.Length; fs.Write(data, 0, arraysize); fs.Close(); } catch (Exception ex) { this.Err = ex.Message; return(-1); } } //关闭连接 con.Close(); #endregion return(1); }
/// <summary> /// 保存图片 /// </summary> /// <param name="info"></param> /// <param name="trans"></param> private int SaveInfo(FtpFile info, OracleTransaction trans) { // string sConn = SystemUpdate.ReadConfig("MSSQLCLIENT"); // Oracon = mgr.ConnectOracle(sConn); // Oracon.Open(); DataTable m_Dt = new DataTable(); string m_Sql = ""; try { // int i = mgr.IsExistPrimaryKey(Oracon,info.PrimaryID); // if(i == -1) // { // trans.Rollback(); // MessageBox.Show("查询是否存在主键失败"); // return -1; // } int j = mgr.IsExistPrimaryKey(Oracon, info); if (j == -1) { trans.Rollback(); MessageBox.Show("查询同一目录下重复文件名失败"); return(-1); } if (j == 1) { trans.Rollback(); MessageBox.Show("同一目录下不能存在重复文件名"); return(-1); } if (myEditType == EditType.Modify) { m_Sql = "UPDATE com_downloadfile SET file_name= '{1}',local_directory= '{2}',file_version= '{3}', oper_code= '{4}',oper_date= sysdate WHERE primary_id= '{0}'"; } else if (myEditType == EditType.Add) { #region 插入一条空值 m_Sql += "INSERT INTO com_downloadfile " + "( primary_id," + "file_name," + "local_directory," + " file_content, " + " file_version," + " oper_code, " + " oper_date ) " + "VALUES " + "( '{0}'," + " '{1}'," + " '{2}'," + " empty_blob()," + " '{3}'," + " '{4}'," + " SYSDATE" + ")"; #endregion } m_Sql = string.Format(m_Sql, info.PrimaryID, info.FileName, info.LocalDirectory, info.FileVersion, info.OperCode); OracleCommand mCmd = new OracleCommand(m_Sql, Oracon); mCmd.Transaction = trans; mCmd.ExecuteNonQuery(); return(SavePhoto(info.PrimaryID, info.FileContent, "primary_id", "file_content", "com_downloadfile", trans)); } catch (Exception ex) { trans.Rollback(); MessageBox.Show(ex.Message); return(-1); } }
/// <summary> /// 加载文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btFileContent_Click(object sender, System.EventArgs e) { OpenFileDialog myDialog = new OpenFileDialog(); //新生成列表 FileList = new ArrayList(); Stream fs; if (this.myEditType == EditType.Add) { myDialog.Multiselect = true; } else { myDialog.Multiselect = false; } if (myDialog.ShowDialog() == DialogResult.OK) { foreach (string fileName in myDialog.FileNames) { FtpFile obj = new FtpFile(); string str = fileName; int i = str.LastIndexOf("\\"); if (i == -1) { i = 0; } #region 获取基本信息 if (this.myEditType == EditType.Add) { obj.FileName = fileName.Substring(i + 1); obj.LocalDirectory = this.txtFilePath.Text; obj.FileVersion = this.txtVision.Text; if (txtFileName.Text.Length == 0) { txtFileName.Text = obj.FileName; } else { txtFileName.Text += " " + obj.FileName; } } else if (this.myEditType == EditType.Modify) { if (fileName.Substring(i + 1) != txtFileName.Text) { ISBool = false; MessageBox.Show("要修改的文件名跟加载的文件不一致", "警告", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } else { ISBool = true; } obj.FileName = this.txtFileName.Text; obj.LocalDirectory = this.txtFilePath.Text; obj.FileVersion = this.txtVision.Text; obj.PrimaryID = this.lbID.Text; } obj.OperCode = "wx"; obj.OperDate = System.DateTime.Now; // obj.PrimaryID = lbID.Text; #endregion #region 获取文件内容 OpenFileDialog myDialog11 = new OpenFileDialog(); myDialog11.FileName = fileName; if ((fs = myDialog11.OpenFile()) != null) { byte[] blob = null; if (fs.Length == 0) { blob = new byte[1]; blob[0] = 32; } else { blob = new byte[fs.Length]; int n = fs.Read(blob, 0, int.Parse(blob.Length.ToString())); fs.Close(); } obj.FileContent = blob; //内容 } #endregion FileList.Add(obj); } } }