简体   繁体   中英

How to get data from SQL Server database in Xml format and from that Xml fill DataSet

I am trying to get data from SQL Server in xml format and from that xml fill a dataset.

Here is my query:

SELECT * 
FROM TblAcademicYear 
FOR XML RAW('AcademicYear'), ELEMENTS;

This query give me following output :

 <AcademicYear>
    <AcademicYearId>3</AcademicYearId>
    <AcademicYearName>دو ‌ہزار ‌پندرہ</AcademicYearName>
    <StartingYear>2015-01-01</StartingYear>
   <EndingYear>2015-12-31</EndingYear>
   <Comments>دو ‌ہزار ‌پندرہ ‌کا ‌تعلیم ‌سال</Comments>
  <RCO>2014-07-02</RCO>
  <UserID>2</UserID>
</AcademicYear>

And my C# code is :

SqlConnection conn = new SqlConnection(ConnectionString);

SqlCommand cmd = new SqlCommand();
System.Xml.XmlReader xmlreader;

try
{
    cmd.Connection = conn;
    conn.Open();
    cmd.CommandText = _Query;
    xmlreader = cmd.ExecuteXmlReader();
    conn.Close();

    DataSet ds = new DataSet();
    dt.Columns.Add("AcademicYearId", typeof(string));
    dt.Columns.Add("AcademicYearName", typeof(string));
    dt.Columns.Add("StartingYear", typeof(string));
    dt.Columns.Add("EndingYear", typeof(string));
    dt.Columns.Add("Comments", typeof(string));
    dt.Columns.Add("RCO", typeof(string));
    dt.Columns.Add("UserID", typeof(string));

    ds.Tables.Add(dt);

    ds.ReadXml(xmlreader);
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    if (conn.State != ConnectionState.Closed)
    {
       conn.Close();
    }
}

and the above code run perfectly but it does not give me the data

Any help would be appreciated .....

SqlConnection conn = new SqlConnection(ConnectionString);

SqlCommand cmd = new SqlCommand();
System.Xml.XmlReader xmlreader;
XmlDataDocument xmlDataDoc = new XmlDataDocument();
try
{
    cmd.Connection = conn;
    conn.Open();
    cmd.CommandText = _Query;
    xmlreader = cmd.ExecuteXmlReader();
    DataSet ds = new DataSet();
    dt.Columns.Add("AcademicYearId", typeof(string));
    dt.Columns.Add("AcademicYearName", typeof(string));
    dt.Columns.Add("StartingYear", typeof(string));
    dt.Columns.Add("EndingYear", typeof(string));
    dt.Columns.Add("Comments", typeof(string));
    dt.Columns.Add("RCO", typeof(string));
    dt.Columns.Add("UserID", typeof(string));

    ds.Tables.Add(dt);
    while(xmlreader.Read()
    {
      xmlDataDoc.DataSet.ReadXml(xmlreader);
    }
    ds = xmlDataDoc.DataSet;
    xmlreader.Close();
    conn.Close();
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    if (conn.State != ConnectionState.Closed)
    {
       conn.Close();
    }
}

You have not get data because you closed the connection before read.

DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("AcademicYearId", typeof(string));
dt.Columns.Add("AcademicYearName", typeof(string));
dt.Columns.Add("StartingYear", typeof(string));
dt.Columns.Add("EndingYear", typeof(string));
dt.Columns.Add("Comments", typeof(string));
dt.Columns.Add("RCO", typeof(string));
dt.Columns.Add("UserID", typeof(string));
ds.Tables.Add(dt);

cmd.Connection = conn;
conn.Open();
cmd.CommandText = _Query;
xmlreader.Read();    -- // Add this line
xmlreader = cmd.ExecuteXmlReader();
// conn.Close();  -- Remove this line and add it at the end   
ds.ReadXml(xmlreader);
conn.Close();

You have to invoke read() method of xmlreader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM