如何从Xml格式和Xml填充DataSet中获取SQL Server数据库中的数据

时间:2014-09-16 12:23:43

标签: c# sql sql-server xml

我试图以xml格式从SQL Server获取数据,并从该xml填充数据集。

这是我的问题:

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

此查询为我提供以下输出:

 <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>

我的C#代码是:

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();
    }
}

并且上面的代码运行完美,但它没有给我数据

任何帮助都会受到赞赏.....

2 个答案:

答案 0 :(得分:1)

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();
    }
}

答案 1 :(得分:0)

由于您在阅读前已关闭连接,因此无法获取数据。

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();

您必须调用xmlreader的read()方法。