Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
hi,
This is dinakar. I am having input xml, which contain table name,column name ,type and values. I want generic function to insert a record by using above xml table values.
please help me out please.....
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 2:34am    
What's the record? what this function should do? Not clear.
--SA
Manfred Rudolf Bihy 22-Jun-11 12:55pm    
OP has the table name and column names along with their type and the value of each column should be set to in a XML file. OP wants to develop a method that will read this meta information and insert it into a DB. In my answer I just assumed that the tables are already there, but that would also not be very hard to achive either.

1 solution

It's a rather straight forward procedure to come up with a solution (I'll assume that the tables are already created in your DB):


  1. Extract the table name and column name(s), value(s) and type(s) from your XML. One way to achieve that would be the use of XPath expressions.
  2. Construct insert statements from the data retrieved in step 1.:
    C#
    String myStringSQLStatement = String.Format("INSERT INTO {0} {1} VALUES({3})", tableName, columnList, paramNameList);

    where tableName is the table name from the XML and the columnList is the list of the column names seperated by a comma. For the paramNameList use the column names prefixed by a @ and also separate them by a comma.
  3. Next make a connection to your DB, create a SQL statement form the string you generated.
  4. Next add SqlParameters to you statement with the appropriate type which you also fetched from your XML.
  5. Run your SqlStatement and fetch the results.


Follow this route, keep your question updated with your progress and tell us if you run across any problems while you're working this out.

Best Regards,

--MRB
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 18:20pm    
My 5, mostly for understanding what OP needed :-)
--SA
Member 7962316 24-Jun-11 12:56pm    
please give me an example for your answer. that is how to read and how to send columnlist and parameterlist to the insert statement...
Manfred Rudolf Bihy 25-Jun-11 13:46pm    
I put this statement here in my solution: "Follow this route, keep your question updated with your progress and tell us if you run across any problems while you're working this out."
I haven't found any updates on your question yet, so what do you think I should do? Get started solving this, update your question with your progress and when you run into problems come back and post a comment on my solution so I'll know that something happend that needs my attention.
Thanks for your cooperation!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900