public Form4(string serverIP, string dbName, string userName, string password) { System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; InitializeComponent(); dboperation = new DbOperation(); this.serverIP = serverIP; this.dbName = dbName; this.userName = userName; this.password = password; dboperation.Connect(serverIP, dbName, userName, password); }
private void Form1_Load(object sender, EventArgs e) { dboperation = new DbOperation(); }
private void Form4_Load(object sender, EventArgs e) { reader = dboperation.GetDataReader("select ChannelID,ChannelName from PE_Channel where ItemCount !=0"); DbOperation mydboperation = new DbOperation(); mydboperation.Connect(serverIP, dbName, userName, password); SqlDataReader myreader; while (reader.Read()) { checkedListBox1.Items.Add(reader.GetString(1)); TreeNode node = new TreeNode(); node.Text = reader.GetString(1); treeView1.Nodes.Add(node); myreader = mydboperation.GetDataReader("select ClassName from PE_Class where ChannelID =" + reader.GetInt32(0)); while (myreader.Read()) { TreeNode childNode = new TreeNode(); childNode.Text = myreader.GetString(0); node.Nodes.Add(childNode); } myreader.Close(); } mydboperation.Close(); //默认全部勾选 for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, true); } reader.Close(); }
//导出线程函数 private void exportXmlProcess() { //导出channel数据库表并写入xml文件 progressBar1.Minimum = 0; progressBar1.Maximum = checkedListBox1.CheckedItems.Count + 2; filePath = fileFolder + "\\ceas_channel_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); reader = dboperation.GetDataReader("select ChannelID,ChannelName from PE_Channel where ItemCount !=0"); while (reader.Read()) { sw.WriteLine("<result>\r\n<资源库ID>{0}</资源库ID>\r\n<资源库名称>{1}</资源库名称>\r\n</result>", reader.GetInt32(0), reader.GetString(1)); } sw.WriteLine("</results>"); reader.Close(); sw.Close(); fs.Close(); progressBar1.Value++; //导出class数据库表并写入xml文件 filePath = fileFolder + "\\ceas_class_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); reader = dboperation.GetDataReader("select ClassID,ChannelID,ClassName,ParentPath from PE_Class"); while (reader.Read()) { string parentPath = reader.GetString(3); string categoryCode = ""; if (parentPath != "0") { categoryCode = parentPath.Substring(2) + ";"; } sw.WriteLine("<result>\r\n<类目ID>{0}</类目ID>\r\n<资源库ID>{1}</资源库ID>\r\n<类目名称>{2}</类目名称>\r\n<类目编码>{3}</类目编码>\r\n</result>", reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), categoryCode + reader.GetInt32(0)); } sw.WriteLine("</results>"); reader.Close(); sw.Close(); fs.Close(); progressBar1.Value++; //循环需要导出的文件 DbOperation mydboperation = new DbOperation(); mydboperation.Connect(serverIP, dbName, userName, password); SqlDataReader myreader; int ChannelID = 0; string ChannelDir = ""; string ChannelShortName = ""; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { myreader = mydboperation.GetDataReader("select ChannelID,ChannelDir,ChannelShortName from PE_Channel where ChannelName ='" + checkedListBox1.CheckedItems[i].ToString()+"'"); if (myreader.Read()) { ChannelID = myreader.GetInt32(0); ChannelDir = myreader.GetString(1); ChannelShortName = myreader.GetString(2); } else { continue; } myreader.Close(); filePath = fileFolder + "\\ceas_" + checkedListBox1.CheckedItems[i].ToString() + "_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); exportXml("select ArticleID,Title,Keyword,Intro,UpdateTime,ClassID from PE_Article where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir,ChannelShortName, "Article"); exportXml("select SoftID,SoftName,Keyword,SoftIntro,UpdateTime,ClassID from PE_Soft where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir, ChannelShortName, "Soft"); exportXml("select PhotoID,PhotoName,Keyword,PhotoIntro,UpdateTime,ClassID from PE_Photo where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir, ChannelShortName, "Photo"); sw.WriteLine("</results>"); sw.Close(); fs.Close(); progressBar1.Value++; } mydboperation.Close(); if (MessageBox.Show("导出完成!") == DialogResult.OK) { progressBar1.Value = 0; } }
//导出每类的xml文件 private bool exportXml(string sqlstr, string ChannelDir, string ChannelShortName, string tableName) { try { DbOperation mydboperation = new DbOperation(); mydboperation.Connect(serverIP, dbName, userName, password); SqlDataReader myreader; myreader = dboperation.GetDataReader(sqlstr); while (myreader.Read()) { sw.WriteLine("<result>"); sw.WriteLine("<标识符>{0}</标识符>", myreader.GetInt32(0)); sw.WriteLine("<名称>{0}</名称>", myreader.GetString(1)); sw.WriteLine("<最近提交日期>{0}</最近提交日期>", myreader.GetDateTime(4)); sw.WriteLine("<描述>{0}</描述>", RemoveSpaceHtmlTag(myreader.GetString(3))); sw.WriteLine("<负责单位名称>{0}</负责单位名称>", chargeName); sw.WriteLine("<负责单位通讯地址>{0}</负责单位通讯地址>", chargeAddress); sw.WriteLine("<负责单位邮政编码>{0}</负责单位邮政编码>", chargeCode); sw.WriteLine("<负责单位联系电话>{0}</负责单位联系电话>", chargePhone); sw.WriteLine("<负责单位电子邮件地址>{0}</负责单位电子邮件地址>", chargeEmail); string keyword = myreader.GetString(2); if (keyword.Length >= 2) { keyword = keyword.Substring(1, keyword.Length - 2).Replace('|', ';'); } sw.WriteLine("<关键词>应急;{0};{1}</关键词>", keyword,ChannelShortName); sw.WriteLine("<访问限制>{0}</访问限制>", visitLimit); reader = mydboperation.GetDataReader("select ClassName,ParentPath from PE_Class where ClassID=" + myreader.GetInt32(5)); if (reader.Read()) { string parentPath = reader.GetString(1); string categoryCode = ""; if (parentPath != "0") { categoryCode = parentPath.Substring(2) + ","; } sw.WriteLine("<类目名称>{0}</类目名称>", reader.GetString(0)); sw.WriteLine("<类目编码>{0}</类目编码>", categoryCode + myreader.GetInt32(5)); } reader.Close(); sw.WriteLine("<分类标准名称>{0}</分类标准名称>", classifyName); sw.WriteLine("<分类标准版本号>{0}</分类标准版本号>", classifyVersion); sw.WriteLine("<资源信息链接地址>http://www.ceas.org.cn/{0}/Show{1}.asp?{1}ID={2}</资源信息链接地址>", ChannelDir, tableName, myreader.GetInt32(0)); sw.WriteLine("</result>"); } mydboperation.Close(); myreader.Close(); } catch (Exception) { if (reader != null) { reader.Close(); } return false; } return true; }
//导出线程函数 private void exportXmlProcess() { //导出channel数据库表并写入xml文件 progressBar1.Minimum = 0; progressBar1.Maximum = checkedListBox1.CheckedItems.Count + 2; filePath = fileFolder + "\\ceas_channel_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); reader = dboperation.GetDataReader("select ChannelID,ChannelName from PE_Channel where ItemCount !=0"); while (reader.Read()) { sw.WriteLine("<result>\r\n<资源库ID>{0}</资源库ID>\r\n<资源库名称>{1}</资源库名称>\r\n</result>", reader.GetInt32(0), reader.GetString(1)); } sw.WriteLine("</results>"); reader.Close(); sw.Close(); fs.Close(); progressBar1.Value++; //导出class数据库表并写入xml文件 filePath = fileFolder + "\\ceas_class_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); reader = dboperation.GetDataReader("select ClassID,ChannelID,ClassName,ParentPath from PE_Class"); while (reader.Read()) { string parentPath = reader.GetString(3); string categoryCode = ""; if (parentPath != "0") { categoryCode = parentPath.Substring(2) + ";"; } sw.WriteLine("<result>\r\n<类目ID>{0}</类目ID>\r\n<资源库ID>{1}</资源库ID>\r\n<类目名称>{2}</类目名称>\r\n<类目编码>{3}</类目编码>\r\n</result>", reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), categoryCode + reader.GetInt32(0)); } sw.WriteLine("</results>"); reader.Close(); sw.Close(); fs.Close(); progressBar1.Value++; //循环需要导出的文件 DbOperation mydboperation = new DbOperation(); mydboperation.Connect(serverIP, dbName, userName, password); SqlDataReader myreader; int ChannelID = 0; string ChannelDir = ""; string ChannelShortName = ""; for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { myreader = mydboperation.GetDataReader("select ChannelID,ChannelDir,ChannelShortName from PE_Channel where ChannelName ='" + checkedListBox1.CheckedItems[i].ToString() + "'"); if (myreader.Read()) { ChannelID = myreader.GetInt32(0); ChannelDir = myreader.GetString(1); ChannelShortName = myreader.GetString(2); } else { continue; } myreader.Close(); filePath = fileFolder + "\\ceas_" + checkedListBox1.CheckedItems[i].ToString() + "_" + dateString + ".xml"; fs = new FileStream(filePath, FileMode.Create); sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<results>"); exportXml("select ArticleID,Title,Keyword,Intro,UpdateTime,ClassID from PE_Article where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir, ChannelShortName, "Article"); exportXml("select SoftID,SoftName,Keyword,SoftIntro,UpdateTime,ClassID from PE_Soft where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir, ChannelShortName, "Soft"); exportXml("select PhotoID,PhotoName,Keyword,PhotoIntro,UpdateTime,ClassID from PE_Photo where ChannelID=" + ChannelID + " order by UpdateTime desc", ChannelDir, ChannelShortName, "Photo"); sw.WriteLine("</results>"); sw.Close(); fs.Close(); progressBar1.Value++; } mydboperation.Close(); if (MessageBox.Show("导出完成!") == DialogResult.OK) { progressBar1.Value = 0; } }