c# How to read/write xml data from MSSQL

//SELECT [xml_data] FROM [MyDB]
 
using (SqlDataReader reader = cmd.ExecuteReader())
{
  while (reader.Read())
  {
    var xdoc = new XmlDocument();
    xdoc.Load(reader.GetSqlXml(0).CreateReader());
  }
}

public static void InsertXML(string xmlstring)
{
            string Qry = string.Format("INSERT INTO [dbo].[my_table] ([xml_data]) values('{0}')", xmlstring);
            using (SqlConnection cnn = new SqlConnection())
            using (SqlCommand cmd = new SqlCommand(Qry, cnn))
            {
                try
                {
                    cnn.ConnectionString = "Data Source=10.10.0.10;Initial Catalog=MYDB;User ID=user;Password=pass";
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
 
                catch (Exception ex)
                {
                    throw ex;
                }
            }
}

No comments:

Post a Comment