2011-05-05 10 views
9

嗨 我有一個數據庫,其中包含一個從主xmlfile中取得的列(coursecode)。除了課程代碼,數據庫還包含一些其他數據,例如開始日期,結束日期等。我需要定期更新這些數據,因此我使用綁定到數據庫的gridview。問題是課程名稱只在xml文件中。有沒有一種方法可以將數據庫和xml文件鏈接起來(就像關係數據庫中的表一樣),以在gridview中呈現這些數據。我有一個去,但結果頁非常慢,所以我認爲它不能這樣做。這可以做到嗎?獲取數據庫綁定gridview中的xml文件的值

courses.aspx

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ myconn %>" 
    SelectCommand="SELECT CourseCode, Data1, Data2, CourseType FROM Courses ORDER BY CourseCode"> 
    </asp:SqlDataSource> 

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="CourseCode" DataSourceID="SqlDataSource1"> 
    <Columns> 
     <asp:TemplateField HeaderText="CourseCode" SortExpression="CourseCode"> 

      <ItemTemplate> 
       <asp:Label ID="Label4" runat="server" Text='<%# Bind("CourseCode") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 

      <asp:TemplateField HeaderText="Name"> 
     <ItemTemplate> 
     <asp:Label id="lblDate" runat="server"></asp:Label> 
     </ItemTemplate> 
     </asp:TemplateField> 

     restofgridviewhere 

     </Columns> 
     </asp:GridView> 

courses.aspx.vb

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    For i As Integer = 0 To GridView1.Rows.Count - 1 
     'create new label with the contents of lblDate 
     Dim labelDate As Label = DirectCast(GridView1.Rows(i).FindControl("lblDate"), Label) 
     Dim Label4 As Label = DirectCast(GridView1.Rows(i).FindControl("Label4"), Label) 

     For i As Integer = 0 To GridView1.Rows.Count - 1 
     Dim labelDate As Label = DirectCast(GridView1.Rows(i).FindControl("lblDate"), Label) 
     Dim Label4 As Label = DirectCast(GridView1.Rows(i).FindControl("Label4"), Label) 

     Dim ds As New DataSet() 
     ds.ReadXml("file.xml") 
     Dim xmlDoc As New XmlDataDocument(ds) 

     Dim nodeList As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("courses/course[code = '" & Label4.Text & "']") 

     Dim myRow As DataRow 
     For Each myNode As XmlNode In nodeList 
      myRow = xmlDoc.GetRowFromElement(DirectCast(myNode, XmlElement)) 
      If myRow IsNot Nothing Then 
       labelDate.Text = myRow("name") 
      End If 
     Next 
     Next 
     End Sub 

file.xml

<courses> 
    <course> 
     <name>Course name</name> 
     <code>Course code</code> 
    </course> 
    <courses> 
+0

什麼是包含XML的列的SQL數據類型? – 2011-05-12 06:40:05

回答

0

使用兩種數據表 一個爲你的SQL代碼和其他的SQL源 將連接中的多個活動結果集(MARS)屬性設置爲true特林。與相應的數據表細胞

希望 綁定網格視圖的列這將工作

1

一個好的方法將是你的XML解析爲Dictionary<string,string>,其中codekeynamevalue。現在你可以編寫一個小型的GridView輔助函數來使用CourseCode獲取CourseName。

這是一個工作示例。

標記。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
    SelectCommand="SELECT CourseCode, Data1, Data2, CourseType FROM Courses ORDER BY CourseCode"> 
</asp:SqlDataSource> 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseCode" 
    DataSourceID="SqlDataSource1"> 
    <Columns> 
     <asp:TemplateField HeaderText="Course Code" SortExpression="CourseCode"> 
      <ItemTemplate> 
       <asp:Label ID="CourseCodeLabel" runat="server" Text='<%# Eval("CourseCode") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Course Name"> 
      <ItemTemplate> 
       <asp:Label ID="CourseNameLabel" runat="server" Text='<%# GetCourseNameFromCode(Eval("CourseCode")) %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <%--restofgridviewhere--%> 
    </Columns> 
</asp:GridView> 

代碼隱藏

Private Shared courseMapping As New Dictionary(Of String, String)() 

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 

    If Not IsPostBack Then 
     'uri is the path to the xml 
     Dim uri As String = System.IO.Path.Combine(Server.MapPath("."), "App_Data/CourseXML.xml") 
     Dim courses As XElement = XElement.Load(uri) 
     For Each course As XElement In courses.Elements() 
      courseMapping.Add(course.Element("code").Value, course.Element("name").Value) 
     Next 
    End If 

End Sub 

Protected Function GetCourseNameFromCode(courseCode As Object) As String 
    Return courseMapping(courseCode.ToString()) 
End Function 

可能要導入System.Xml.Linq也。

希望這會有所幫助。

相關問題