C#,vb.net,MVC,Jquery,javascript,jscript,vbscript,html,vb,sharepoint,COM,WPF,WCF,Wwf,Asp,Asp.net,questions & answers,

Latest in Sports

Friday, September 5, 2014

Suggestion in inserting a row into SQL from a XML document

Need a suggestion in inserting a row into SQL from a XML document.

 
Let’s say this is the XML that I have
 
<Request>
    <Source>Hello</Source>
    <ProductID>678</ProductID>
    <FormID>GHjd</FormID>
    <Copies>1</Copies>
<Request/>
 
What Am presently doing for building the insert command is
 
        public string BuildQueryString(XElement Xdoc, out CustomException cust)
        {
            string query;
            string xml = Util.GetXelementAsString(Xdoc);
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.LoadXml(xml);
            XDocument xDoc = XDocument.Parse(xml);
            XElement Xelement = XElement.Parse(xml);
            query = "insert into LPI_RequestTable ";
            string columnquery = string.Empty;
            string valuesquery = string.Empty;
            var columns = from c in xDoc.Elements()
                          select c;
            foreach (var column in columns)
            {
                if (column.Elements("Source").Any())
                {
                    if (!(column.Element("Source").Value == null))
                    {
                        columnquery = columnquery + "LCM_Source,";
                        valuesquery = valuesquery + "'" + column.Element("Source").Value + "'" + ",";
                    }
                }
                if (column.Elements("ProductID").Any())
                {
                    if (!(column.Element("ProductID").Value == null))
                    {
                        columnquery = columnquery + "ProductID,";
                        valuesquery = valuesquery + "'" + column.Element("ProductID").Value + "'" + ",";
                    }
                }
                if (column.Elements("FormID").Any())
                {
                    if (!(column.Element("FormID").Value == null))
                    {
                        columnquery = columnquery + "FormID,";
                        valuesquery = valuesquery + "'" + column.Element("FormID").Value + '" + ",";
                    }
                }
                if (column.Elements("Copies").Any())
                {
                    if (!(column.Element("Copies").Value == null))
                    {
                        columnquery = columnquery + "Copies,";
                        valuesquery = valuesquery + column.Element("Copies").Value + ",";
                    }
                }
            }
 
            query = query + "(" + columnquery + ")" + " Values (" + valuesquery + ");";
            return query;
 
        }
 
Problem here is My client doesn’t need the hard coding to be done in building the command. Could anybody help me by providing something which doesn’t need a hard coding.

No comments:

Post a Comment