Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a table(DataTable) into a single cell in a database?

Is it possible to save a DataTable into SQL database in one cell of type binary for example and read it back again into a DataTable?

like image 961
Ahmad Farid Avatar asked Dec 20 '25 00:12

Ahmad Farid


1 Answers

I would create, if possible, a xml field inside the sql database and save the datatable as xml

XML Support in Microsoft SQL Server 2005

and

C# and Vb.net example for XML data type tips in SQL Server 2005

should help you

another example took from here

   protected bool LoadXml(SqlConnection cn, XmlDocument doc)
   {
    //Reading the xml from the database
    string sql =  @"SELECT Id, XmlField  FROM TABLE_WITH_XML_FIELD WHERE Id = @Id";
    SqlCommand cm = new SqlCommand(sql, cn);
    cm.Parameters.Add(new SqlParameter("@Id",1));
    using (SqlDataReader dr = cm.ExecuteReader())
    {
             if (dr.Read())
            {
                      SqlXml MyXml= dr.GetSqlXml(dr.GetOrdinal("XmlField"));
                      doc.LoadXml( MyXml.Value);
                      return true;
            }
            else
            {
                      return false;
            }
     }
    }
like image 155
Fredou Avatar answered Dec 21 '25 14:12

Fredou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!