private void CreateMysqlTbByXml() { foreach (var st_p in m_AllxmlTables) { if (st_p.Key.Contains("sec") || st_p.Key.Contains("Sec")) { continue; } StringBuilder sb = new StringBuilder(); sb.AppendFormat("DROP TABLE IF EXISTS `{0}`;", st_p.Key); MySqlCommand command = mycon.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = sb.ToString(); command.ExecuteNonQuery(); sb.Clear(); sb.AppendFormat("create TABLE `{0}` (id INT UNSIGNED AUTO_INCREMENT,PRIMARY KEY (id))ENGINE = InnoDB DEFAULT CHARSET = utf8 ;", st_p.Key); command.CommandText = sb.ToString(); command.ExecuteNonQuery(); List <stEntryNode> ls = st_p.Value; for (int i = 0; i < ls.Count; ++i) { stEntryNode st = ls[i]; sb.Clear(); string s_type = "varchar"; switch (st.type) { case "string": s_type = "varchar(100)"; break; case "datetime": s_type = "datetime"; break; case "int": s_type = "int"; break; case "bigint": s_type = "bigint"; break; case "float": s_type = "float"; break; default: MessageBox.Show(string.Format("未知类型:{0}", s_type)); break; } //sb.AppendFormat("alter table {0} add {1} VARCHAR(100)", st_p.Key, st.name); sb.AppendFormat("alter table {0} add {1} {2}", st_p.Key, st.name, s_type); command.CommandText = sb.ToString(); command.ExecuteNonQuery(); } textBoxSql.AppendText(string.Format("创建mysql表{0} ok!\n", st_p.Key)); } }
private void loadXml_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true;//忽略文档里面的注释 XmlReader reader = XmlReader.Create(xmlPath, settings); if (reader == null) { MessageBox.Show("请检查xml文件路径!"); return; } xmlDoc.Load(reader); reader.Close(); XmlNode xn = xmlDoc.SelectSingleNode("metalib"); XmlNodeList xnl = xn.ChildNodes; m_AllxmlTables.Clear(); foreach (XmlNode xn1 in xnl) { XmlElement xe = (XmlElement)xn1; string tablename = ""; if (xe.Name == "struct") { tablename = xe.GetAttribute("name").ToString(); List <stEntryNode> nodes = new List <stEntryNode>(); foreach (XmlElement xei in xe)//entry { string fieldname = xei.GetAttribute("name").ToString(); string type = xei.GetAttribute("type").ToString(); string size = xei.GetAttribute("size").ToString(); string defaultvalue = xei.GetAttribute("defaultvalue").ToString(); string desc = xei.GetAttribute("desc").ToString(); stEntryNode node = new stEntryNode(); node.name = fieldname; node.type = type; if (size != "") { node.size = Convert.ToInt32(size); } node.defaultvalue = defaultvalue; node.desc = desc; nodes.Add(node); } m_AllxmlTables.Add(tablename, nodes); } // 得到所有子节点 //XmlNodeList xnl0 = xe.ChildNodes; //bookModel.BookName = xnl0.Item(0).InnerText; //bookModel.BookPrice = Convert.ToDouble(xnl0.Item(2).InnerText); } TbsView.Nodes.Clear(); foreach (var val in m_AllxmlTables.Keys) { TreeNode tree = new TreeNode(); tree.Name = val; tree.Text = val; TbsView.Nodes.Add(tree); } textBoxTbNum.Text = m_AllxmlTables.Count.ToString(); }