public override void OnClick() { Exception eError = null; #region 通过树节点获取源数据库的连接地址 DevComponents.AdvTree.Node DBNode = null; string treeNodeType = m_Hook.ProjectTree.SelectedNode.DataKey.ToString(); if (treeNodeType == "") { return; } DBNode = m_Hook.ProjectTree.SelectedNode;//数据库节点,根节点 while (DBNode.Parent != null) { DBNode = DBNode.Parent; } if (DBNode.DataKey.ToString() != EnumTreeNodeType.DATABASE.ToString()) { return; } if (DBNode.Name != "文件连接") { return; } XmlElement connElem = DBNode.Tag as XmlElement; if (connElem == null) { return; } string conStr = connElem.GetAttribute("MetaDBConn"); #endregion SaveFileDialog saveDial = new SaveFileDialog(); saveDial.Title = "保存数据"; saveDial.Filter = "控制点数据(*.mdb)|*.mdb"; if (saveDial.ShowDialog() == DialogResult.OK) { FrmProcessBar frmbar = new FrmProcessBar(); frmbar.Show(); string pFileName = saveDial.FileName; FileInfo pFileInfo = new FileInfo(ModData.v_TempControlDB); FileInfo desFileInfo = pFileInfo.CopyTo(pFileName, true); //文件存储路径加名称 string path = desFileInfo.DirectoryName; //文件存储路径 //连接目标表 SysCommon.DataBase.SysTable desSysTable = new SysCommon.DataBase.SysTable(); desSysTable.SetDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pFileName + ";Persist Security Info=True", SysCommon.enumDBConType.OLEDB, SysCommon.enumDBType.ACCESS, out eError); if (eError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("错误", "连接元数据库失败!\n连接地址为:" + pFileName); desSysTable.CloseDbConnection(); return; } //获得目标表格 DataTable desTable = desSysTable.GetTable("metadata", out eError); if (eError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", eError.Message + "在库中不存在表格'metadata',请检查!"); desSysTable.CloseDbConnection(); return; } //insert 语句 string insertStr = "insert into metadata ("; for (int i = 1; i < desTable.Columns.Count; i++) { //第一列为自动编号ID,从第二列开始 insertStr += desTable.Columns[i].ColumnName + ","; } insertStr = insertStr.Substring(0, insertStr.Length - 1) + ")"; insertStr += " values ("; //连接源数据库 SysCommon.DataBase.SysTable pSysTable = new SysCommon.DataBase.SysTable(); ////pSysTable.SetDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + conStr + ";Persist Security Info=True", SysCommon.enumDBConType.OLEDB, SysCommon.enumDBType.ACCESS, out eError); pSysTable.SetDbConnection(conStr, SysCommon.enumDBConType.ORACLE, SysCommon.enumDBType.ORACLE, out eError); if (eError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("错误", "连接元数据库失败!\n连接地址为:" + conStr); pSysTable.CloseDbConnection(); desSysTable.CloseDbConnection(); return; } //源数据表格 DataTable ordDt = pSysTable.GetTable("ControlPointMDTable", out eError); if (eError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "查询控制点元信息出错!"); pSysTable.CloseDbConnection(); desSysTable.CloseDbConnection(); return; } pSysTable.CloseDbConnection(); if (desTable.Columns.Count + 5 != ordDt.Columns.Count) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", "目标数据库与源数据库字段结构不一致!"); desSysTable.CloseDbConnection(); return; } #region 遍历要导出的数据 //设置进度条最大值 frmbar.SetFrmProcessBarMax(m_Hook.DataInfoGrid.SelectedRows.Count); int pValue = 0; for (int i = 0; i < m_Hook.DataInfoGrid.RowCount; i++) { if (m_Hook.DataInfoGrid.Rows[i].Selected) { string savePath = m_Hook.DataInfoGrid.Rows[i].Cells["存储位置"].FormattedValue.ToString().Trim(); if (!savePath.Contains("控制点数据")) { //其他数据导出 //pValue++; //frmbar.SetFrmProcessBarValue(pValue); continue; } frmbar.SetFrmProcessBarText("正在进行下载数据:" + m_Hook.DataInfoGrid.Rows[i].Cells["数据文件名"].FormattedValue.ToString().Trim()); if (m_Hook.DataInfoGrid.Rows[i].Cells["ID"].FormattedValue.ToString().Trim() == "") { return; } long dataID = Convert.ToInt64(m_Hook.DataInfoGrid.Rows[i].Cells["ID"].FormattedValue.ToString().Trim()); //执行导出操作 OutPutOneRacord(dataID, insertStr, ordDt, desSysTable, path, out eError); if (eError != null) { SysCommon.Error.ErrorHandle.ShowFrmErrorHandle("提示", eError.Message); desSysTable.CloseDbConnection(); frmbar.Dispose(); frmbar.Close(); return; } pValue++; frmbar.SetFrmProcessBarValue(pValue); } } #endregion frmbar.Dispose(); frmbar.Close(); } }
/// <summary> /// 下载文件 /// </summary> /// <param name="ftpfilepath">相对根目录的文件完整路径</param> /// <param name="filePath">本地保存文件的完整路径</param> /// <param name="fileName">文件名</param> /// <returns></returns> public bool Download(string ftpfilepath, string filePath, string fileName, out string errorinfo)////上面的代码实现了从ftp服务器下载文件的功能 { Exception ex = null; errorinfo = ""; FrmProcessBar ProcBar = new FrmProcessBar(); ProcBar.Show(); try { filePath = filePath.Replace("我的电脑\\", ""); String onlyFileName = Path.GetFileName(fileName); string newFileName = filePath; if (File.Exists(newFileName)) { errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName); return(false); } ftpfilepath = ftpfilepath.Replace("\\", "/"); string url = "ftp://" + ftpServerIP + "/" + ftpfilepath; Connect(url);//连接 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Timeout = 10000; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); long ProcBarValue = 0; ////// long all = GetFileSize(ftpfilepath, out ex); { if (null != ex) { return(false); } } ProcBar.SetFrmProcessBarMax(all / 2048); ////// while (readCount > 0) { ProcBarValue++; ProcBar.SetFrmProcessBarText("正在下载文件:" + fileName); ProcBar.SetFrmProcessBarValue(ProcBarValue); Application.DoEvents(); outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); errorinfo = "Succeed"; return(true); } catch (Exception exx) { errorinfo = string.Format("因{0},无法下载", exx.Message); return(false); } finally { ProcBar.Dispose(); ProcBar.Close(); } }
/// <summary> /// 上传文件 /// </summary> /// <param name="filename"></param> /// <param name="path"></param> public bool Upload(string filename, string path, string savename, out string errorinfo) //上面的代码实现了从ftp服务器上载文件的功能 { FrmProcessBar frmbar = new FrmProcessBar(); frmbar.Show(); path = path.Replace("\\", "/"); FileInfo fileInf = new FileInfo(filename); savename = savename.Replace("+", "-"); // 处理非法字符 savename = savename.Replace("#", "-"); // string uri; if (string.IsNullOrEmpty(path)) { uri = "ftp://" + ftpServerIP + "/" + savename; } else { uri = "ftp://" + ftpServerIP + "/" + path + "/" + savename; } Connect(uri);//连接 // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; reqFTP.Timeout = 10000; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为kb long procMax = fileInf.Length / 2048; //进度条最大值 frmbar.SetFrmProcessBarMax(procMax); string task = "正在上传文件:" + savename; frmbar.SetFrmProcessBarText(task); int value = 0; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流(System.IO.FileStream) 去读上传的文件 try { FileStream fs = fileInf.OpenRead(); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的kb contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { value++; frmbar.SetFrmProcessBarValue(value); Application.DoEvents(); // 把内容从file stream 写入upload stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); fs.Close(); errorinfo = "Succeed"; return(true); } catch (Exception ex) { errorinfo = string.Format("因{0},无法完成上传", ex.Message); return(false); } finally { frmbar.Dispose(); frmbar.Close(); } }
/// <summary> /// 续传文件 /// </summary> /// <param name="filename"></param> public bool Upload(string filename, long size, string path, out string errorinfo) //上面的代码实现了从ftp服务器上载文件的功能 { FrmProcessBar frmbar = new FrmProcessBar(); frmbar.Show(); path = path.Replace("\\", "/"); FileInfo fileInf = new FileInfo(filename); //string uri = "ftp://" + path + "/" + fileInf.Name; string uri = "ftp://" + ftpServerIP + "/" + path + filename; Connect(uri);//连接 // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; reqFTP.Timeout = 10000; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.AppendFile; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为kb long procMax = (fileInf.Length - size) / 2048; frmbar.SetFrmProcessBarMax(procMax); frmbar.SetFrmProcessBarText("正在续传文件:" + filename); int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流(System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); int value = 0; try { StreamReader dsad = new StreamReader(fs); fs.Seek(size, SeekOrigin.Begin); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的kb contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入upload stream value++; frmbar.SetFrmProcessBarValue(value); Application.DoEvents(); strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); fs.Close(); errorinfo = "Succeed"; return(true); } catch (Exception ex) { errorinfo = string.Format("因{0},无法完成上传", ex.Message); return(false); } finally { if (null != fileInf) { fileInf = null; } frmbar.Close(); frmbar.Dispose(); } }
/// <summary> /// 删除一个工程 /// </summary> /// <returns></returns> public bool DeleteProject(DevComponents.AdvTree.Node ProjectNode, string ConStr, long Projiectid , out string err) { Exception ex = null; err = ""; if (string.IsNullOrEmpty(this._ProName)) { err = "没有指定项目名称!"; return(false); } if (string.IsNullOrEmpty(ConStr)) { err = "没有指定元信息库连接信息!"; return(false); } int value = 0; FrmProcessBar DelBar = new FrmProcessBar(10); DelBar.Show(); DelBar.SetFrmProcessBarText("正在连接元信息库"); Application.DoEvents(); SysCommon.DataBase.SysTable pSysDB = new SysCommon.DataBase.SysTable(); //属性库连接类 pSysDB.SetDbConnection(ConStr, SysCommon.enumDBConType.ORACLE, SysCommon.enumDBType.ORACLE, out ex); if (ex != null) { err = "元信息库连接失败!连接地址为:" + ConStr; pSysDB.CloseDbConnection(); DelBar.Dispose(); DelBar.Close(); return(false); } long ProjectID = Projiectid; #region 除项目的所有产品 string Sql = "SELECT ID,产品名称,存储位置 FROM ProductMDTable WHERE 项目ID=" + ProjectID; DataTable GetTable = pSysDB.GetSQLTable(Sql, out ex); if (null != ex) { err = "获取项目的产品信息失败!"; pSysDB.CloseDbConnection(); DelBar.Dispose(); DelBar.Close(); return(false); } if (null != GetTable) { for (int i = 0; i < GetTable.Rows.Count; i++) { long ProducetId = -1; string ProductName = string.Empty; string ProductPath = string.Empty; ProducetId = long.Parse(GetTable.Rows[i][0].ToString()); ProductName = GetTable.Rows[i][1].ToString().Trim(); ProductPath = GetTable.Rows[i][2].ToString().Trim(); DelBar.SetFrmProcessBarValue(value); DelBar.SetFrmProcessBarText(" 正在删除产品:" + ProductName); value += 1; if (value == 10) { value = 0; } Application.DoEvents(); DevComponents.AdvTree.Node ProductNode = ModDBOperator.GetTreeNode(ProjectNode, ProductPath, ProductName, EnumTreeNodeType.PRODUCT.ToString(), ProducetId, out ex); if (null != ex) { err = "获取项目的产品:‘" + ProductName + "’树节点失败!"; pSysDB.CloseDbConnection(); DelBar.Dispose(); DelBar.Close(); return(false); } if (null != ProductNode) { if (!ModDBOperator.DelProduct(ProductNode, out ex)) { err = "删除项目的产品:‘" + ProductName + "’失败!"; pSysDB.CloseDbConnection(); DelBar.Dispose(); DelBar.Close(); return(false); } } } } #endregion #region 除项目的文件夹 DelBar.SetFrmProcessBarText("正在删除工程项目目录"); Application.DoEvents(); if (!ModDBOperator.DelDirtory(this._ftpIP, this._ftpUSER, this._ftpPassWord, this._ProName, out ex)) { err = "工程项目项目目录删除失败!"; pSysDB.CloseDbConnection(); DelBar.Dispose(); DelBar.Close(); return(false); } #endregion DelBar.Dispose(); DelBar.Close(); pSysDB.CloseDbConnection(); return(true); }