Store XML in SQL Server Table

S

sippyuconn

Hi

I wanted to store xml in a SQL Server Table

How does one go about it????

How does the Insert look like??? If I do it from a textfile or xmldocument?

How doe the Select look like if I want to put back into an xml document ??

THanks
 
A

Arne Vajhøj

sippyuconn said:
I wanted to store xml in a SQL Server Table

How does one go about it????

How does the Insert look like??? If I do it from a textfile or xmldocument?

How doe the Select look like if I want to put back into an xml document ??

Do you want to store one XML document in:
- multiple fields ?
- single field of type NTEXT ?
- single field of type XML ?

Arne
 
S

sippyuconn

Arne Vajhøj said:
Do you want to store one XML document in:
- multiple fields ?
- single field of type NTEXT ?
- single field of type XML ?

Arne

Hi

I have small xmlfiles and each row would be a different file

So I was wondering do I insert as a textfile,stream or read xml into an
xmldocument and somehow do the insert???

Also on the select - how do I retrieve and what do I put the xml into??
a string ??? or into an xmldocument??

Thanks
 
A

Arne Vajhøj

sippyuconn said:
I would say #2

Then it is very simple using parameters.

Something like:

SqlCommand ins = new SqlCommand("INSERT INTO tbl VALUES(@id, @txt)", con);
ins.Parameters.Add("@id", SqlDbType.Int);
ins.Parameters.Add("@txt", SqlDbType.NText);
ins.Parameters["@id"].Value = id;
ins.Parameters["@txt"].Value = xmlstr;
ins.ExecuteNonQuery();

and:

SqlCommand sel = new SqlCommand("SELECT txt FROM tbl WHERE id = @id", con);
sel.Parameters.Add("@id", SqlDbType.Int);
sel.Parameters["@id"].Value = id;
SqlDataReader rdr = sel.ExecuteReader();
if(rdr.Read())
{
xmlstr = rdr.GetString(0);
}
else
{
// do something bad
}
rdr.Close();

Arne
 
J

Jialiang Ge [MSFT]

Good morning, sippyuconn

I agree with Arne. When we use NTEXT field to store XML string, it has no
difference from storing a normal string. We can read the XML file into a
string with the code:

StreamReader reader = new StreamReader("path to the xml file");
string xml = reader.ReadToEnd();
reader.Close();

and insert it into a SQL server table with Arne's code snippet.

Please let us if you have any other concerns or questions.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top