项目开发中,我习惯将sql语句单独放到一个xml文档中,这样需要修改sql语句时候,就不用更新代码了。
1,展示xml文档结构:
<?xml version="1.0" encoding="utf-8" ?> <root> <sql id="GetUserInfo" description="查询用户信息"> <![CDATA[SELECT a.Id,a.userName,a.phone FROM u_tab_user a ;]]> </sql> </root>
PS:多个SQL 只需要配置多个<sql></sql> 节点即可
2,读取指定Id的SQL语句:
public class SqlMap { private const string key = "id"; public static string GetSql(string fileName, string sqlKey) { string result = string.Empty; IEnumerable<XElement> element = LoadXml(fileName); var ele = element.Single(o => o.Attribute(XName.Get(key)).Value == sqlKey); if (ele != null) { result = ele.Value; } return result; } private static IEnumerable<XElement> LoadXml(string fileName) { string dirPath = AppDomain.CurrentDomain.BaseDirectory; string fileDir = string.Format("SqlFile/{0}.xml", fileName); string filePath = Path.Combine(dirPath, fileDir); if (!File.Exists(filePath)) { fileDir = string.Format("bin/SqlFile/{0}.xml", fileName); filePath = Path.Combine(dirPath, fileDir); } XElement element = XElement.Load(filePath); return element.Descendants(); } }
PS: fileName 是xml文件名称例如用户模块的SQL与角色模块的SQL分开,那就建两个xml, User.xml 和 Role.xml
另外还有些经常会变动的配置,我也习惯用xml进行配置,配置文件如下:
<?xml version="1.0" encoding="utf-8" ?> <root> <user description="用户设置"> <item key="userKey" a="10" b="0.22" c="0.67" description="说明性文字" ></item> </user> <elements description="说明性文字"> <item key="key1" a= "1" b = "0.63" c = "0.114" type="text" fontSize="12" description="说明性文字"></item> <item key="key2" a= "1" b = "0.7" c = "0.5" type="text" fontSize="12" description="说明性文字"></item> </elements> </root>
读取方式:
public static List<ElementObj> GetElements(string elementKey, string fid) { List<ElementObj> elements = new List<ElementObj>(); if (string.IsNullOrEmpty(elementKey)) { return elements; } XmlNodeList contractItems = LoadXml(elementKey, fid, "elements"); ElementObj model = null; foreach (var e in contractItems) { XmlElement item = e as XmlElement; if (item != null && item.NodeType == XmlNodeType.Element) { string a= ((XmlElement)item).GetAttribute("a"); //获取a属性值 string b = ((XmlElement)item).GetAttribute("b"); //获取b属性值 string c = ((XmlElement)item).GetAttribute("c"); //获取c属性值 string type = ((XmlElement)item).GetAttribute("type"); //获取type属性值 string fontSize = ((XmlElement)item).GetAttribute("fontSize"); //获取fontSize属性值 string key = ((XmlElement)item).GetAttribute("key"); //获取key属性值 } } return elements; } private static XmlNodeList LoadXml(string elementKey, string fid, string nodeKey) { string tempFileSavePath = AppDomain.CurrentDomain.BaseDirectory + "elements\" + elementKey + "\"; if (!Directory.Exists(tempFileSavePath)) { Directory.CreateDirectory(tempFileSavePath); } string xmlPath = Path.Combine(tempFileSavePath, fid + ".xml");//string path = string.Format("elements/{0}.xml", elementKey); //string xmlPath = AppDomain.CurrentDomain.BaseDirectory + path; XmlDocument doc = new XmlDocument(); doc.Load(xmlPath); //加载Xml文件 XmlElement rootElem = doc.DocumentElement; //获取根节点 XmlNodeList elementNodes = rootElem.GetElementsByTagName(nodeKey); //获取预警类型子节点集合 XmlElement element = elementNodes[0] as XmlElement; XmlNodeList nodeItems = element.ChildNodes; return nodeItems; }