Friday 28 June 2019

XML - Read XML data type from SQL database in C#

The code below pulls the Column with XML Data Type from the table in the SQL DATABASE
and keeps the data in XML format in c#. 


string connStr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;

string query = ";

// wrap your SqlConnection and SqlCommand in using blocks...

using (SqlConnection _con = new SqlConnection(connStr))

using (SqlCommand _cmd = new SqlCommand(query, _con))



{

// setup parameter for _cmd

// _cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1;



_con.Open();

string contentsOfYourXml = (string)_cmd.ExecuteScalar();



_con.Close();

Label1.Text = contentsOfYourXml;

}