Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > .NET Framework > Forum > อยากทราบวิธีการดึงข้อมูลใน sql2005 ที่เก็บข้อมูล type เป็น xml ครับ



 

อยากทราบวิธีการดึงข้อมูลใน sql2005 ที่เก็บข้อมูล type เป็น xml ครับ

 



Topic : 036384



โพสกระทู้ ( 14 )
บทความ ( 0 )



สถานะออฟไลน์




Vs2008
ภาษา : asp.net c#
dbms : sql 2005
LINQ Syntax

สมมติ table ชื่อ customer
โดยกำหนดให้ field แรกคือ id มี type เป็น int
field ที่สองคือ information มี type เป็น xml

ตัวอย่างข้อมูล information ที่เก็บข้อมูลเป็น type xml
<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person>
<Name>Ingrid</Name>
<City>Oslo</City>
<Age>63</Age>
</Person>
</Persons>

คือแต่ละ id ก็จะมีข้อมูล information ใช่มั้ยครับ
คือผมต้องการดึงข้อมูล information อ่าคับที่เก็บเป็น xml ให้แสดงออกมาทาง GridView
โดยคอลัมน์ที่แสดงใน GridView เป็นชื่อที่อยู่ใน tag อ่าคับ พวก Name City Age
และให้แสดงข้อมูลเป็นข้อมูลภายใน tag อ่าคับ ก็คือ Ingrid Oslo 63 ให้ตรงตาม column
และให้แสดงข้อมูล information ของทุกๆ id ทาง gridview

โดยที่ GridView ผมอยากให้สามารถ Update Insert Delete ข้อมูลเหล่านั้นได้อ่าคับ
คือผมอยากให้ที่กล่าวมาทั้งหมดเป็นการจัดการแบบ LINQ Syntax อ่าคับ

ปล1. อันนี้ผมสมมติ table field เองนะครับ แต่ผมแค่อยากรู้วิธีการจัดการและเขียนโค้ด
ปล2.รบกวนด้วยนะครับ ผมทำไม่เป็นจิงๆ ^/\^



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-12-30 22:16:09 By : nutto54 View : 1468 Reply : 6
 

 

No. 1



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ทำความเข้ากันก่อนนะว่า xml = string

เรามีตาราง customer ซึ่งมี 2 field คือ
1. ID (pk, int)
2. Information (xml)

เรา query field Information มาเก็บไว้ในตัวแปร string ก่อน จากนั้นก็พยายามแปลง string xml นั้นให้ DataTable ให้ได้ โดยใช้

Code (C#)
//สมมติ xml string ที่ query มาแล้ว เราเก็บไว้ในตัวแปรที่ชื่อ xmlString นะ

System.IO.TextReader Tr = new System.IO.StringReader(xmlString);

DataSet Ds = new DataSet();
Ds.ReadXml(Tr);
DataTable Dt = Ds.Tables[0];


เราก็จะได้ Dt ที่มีข้อมูลของ xml นั้น จากนั้นเราจะเอาไปทำอะไรก็ง่าย

ไม่ว่าจะแสดงใน GridView

Code (C#)
GridView1.DataSource = Dt;
GridView1.DataBind();


หรือ Insert, Update, Delete ก็ทำได้ (โดยทำกับ DataTable Dt)

ก็กระทำชำเรา Dt จนหนำใจก็แปลงจาก DataTable กับเป็นเป็น string อีกครั้งเพื่อเก็บลงฐานข้อมูล

Code (C#)
System.IO.TextWriter Wr = new System.IO.StringWriter();

Dt.WriteXml(Wr);

string xmlString = Wr.ToString();







Date : 2009-12-31 00:51:23 By : tungman
 


 

No. 2



โพสกระทู้ ( 14 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณคุณ tungman มากเลยนะครับ สำหรับคำแนะนำดีๆ
ภายหลังจาพยายามศึกษาเพิ่มเติม
รายงานความก้าวหน้าครับ ตอนนี้ผมทำให้แสดงที่ GridView ได้แล้วและ Edit ได้แล้วอ่าครับ
ด้วยโค้ดดังนี้อ่าคับ
Code (C#)
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {

        GridView1.DataSource = GetDataSource();
        GridView1.DataBind();
    }

    protected DataTable GetDataSource()
    {
        const string key = "MyDataSource";
        DataTable Dt = Session[key] as DataTable;
        informationDataContext z = new informationDataContext();
        DataSet Ds = new DataSet();
        if (Dt == null)
        {
        Dt = new DataTable();
        Ds = new DataSet();
        var query = from a in z.jubzs where  select a.xml;
        var xmlstring = from q in query select q.ToString();
        foreach (string h in xmlstring)
        {
            System.IO.TextReader Tr = new System.IO.StringReader(h);
            Ds.ReadXml(Tr);
        }
        Dt.Columns.Add("Name", typeof(string));
        Dt.Columns.Add("City", typeof(string));
        Dt.Columns.Add("Age", typeof(string));
        Dt = Ds.Tables[0];
        Session[key] = Dt;
        }
        return Dt;
    }
    protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender,
    GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
    protected void GridView1_RowUpdating(object sender,
    GridViewUpdateEventArgs e)
    {

        TextBox cityty = GridView1.Rows[e.RowIndex].FindControl("txtCity") as TextBox;
        TextBox ageage = GridView1.Rows[e.RowIndex].FindControl("txtAge") as TextBox;
        string newcity  = cityty.Text;
        string newAge = ageage.Text;
        DataTable Dt = GetDataSource();
        Dt.Rows[e.RowIndex][1] = newcity;
        Dt.Rows[e.RowIndex][2] = newAge;
        GridView1.EditIndex = -1;
        BindGrid();
    }
}



คือมันสามารถแสดงได้อ่ะครับ แต่ผมอยากให้มันแสดงค่า id ที่เป็น int ไปพร้อมๆกันใน GridView เลยอ่ะครับ
ผมทำตรงใส่ค่า id ไปพร้อมๆกับที่แปลง xml format เป็น string เข้าไปข้างใน dataset ไม่เป็นนะครับ
แล้วที่บอกว่าให้ทำ datatable เป็น string แล้วค่อยเก็บอ่าครับ คือไม่ทราบว่าทำให้เป็นในรูปแบบ xml ก่อนเก็บยังไงอ่าครับ

รบกวนด้วยนะครับ
Date : 2009-12-31 14:45:51 By : nutto54
 

 

No. 3



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


งงครับ ช่วยอธิบายที ผมไม่เข้าใจว่าจะเอา id ไปวางไว้ตรงไหนของ GridView

ที่เข้าใจตอนนี้คือ id แทนตาราง xml หนึ่งตาราง เช่น id=1 มีข้อมูลหนึ่งตาราง id=2 มีข้อมูลอีกหนึ่งตาราง

ซึ่งถ้าเป็นไปตามที่เข้าใจ เอา id ไปแปะไว้ที่ label เป็นชื่อของตารางนั้นดีกว่า
Date : 2009-12-31 17:48:55 By : tungman
 


 

No. 4



โพสกระทู้ ( 14 )
บทความ ( 0 )



สถานะออฟไลน์


Table จริงๆผมมีสอง Field นั่นคือ Id และ Information
ึคือจากโค้ดด้านบนอ่าครับ คือเอาข้อมูลจาก Field ที่สองนั่นคือ Information ที่เป็น xml type

แล้วไปใส่ใน GridView ได้แล้ว แสดงออกมา 3 column ตามข้อมูลใน xml

แต่ที่บอกว่าเอา id ไปด้วยอ่าครับ คืออยากให้ GridView มี 4 Column

Column แรกคือเป็น id ที่เป็นข้อมูลใน Field แรกอย่างที่บอก
Column อื่นอีก 3 column ให้เป็นข้อมูลตาม xml

ผมไม่แน่ใจว่าต้องจัดการที่ dataset หรือ datatable นะคับ
คือผมทำให้มันไปรวมกันก่อนไปผูกกับ GridView ไม่เปนอ่าครับ
งงป่าวอ่าครับ ผมกลัวอธิบายไม่ชัดเจน
รบกวนด้วยนะครับ
Date : 2009-12-31 18:01:05 By : nutto54
 


 

No. 5



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


Code (C#)
    protected DataTable GetDataSource()
    {
        const string key = "MyDataSource";
        DataTable Dt = Session[key] as DataTable;
        informationDataContext z = new informationDataContext();
        DataSet Ds = new DataSet();
        if (Dt == null)
        {
        Dt = new DataTable();
        Ds = new DataSet();
        var query = from a in z.jubzs where  select a.xml;
        var xmlstring = from q in query select q.ToString();
        foreach (string h in xmlstring)
        {
            System.IO.TextReader Tr = new System.IO.StringReader(h);
            Ds.ReadXml(Tr);
        }
        Dt.Columns.Add("Name", typeof(string));
        Dt.Columns.Add("City", typeof(string));
        Dt.Columns.Add("Age", typeof(string));
        Dt = Ds.Tables[0];
        }

        DataColumn Dc = new DataColumn();
        Dc.ColumnName = "ID";
        Dc.DataType = System.Type.GetType("System.Int32");

        Dt.Columns.Add(Dc);

        foreach (DataRow Dr in Dt.Rows)
        { 
           Dr["ID"] = *** YourID ***
        }

        Session[key] = Dt;

        return Dt;
    }

Date : 2010-01-01 12:38:51 By : tungman
 


 

No. 6



โพสกระทู้ ( 14 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณคับคุณ tungman เดี๋ยวลองทำดูก่อนนะครับ
Date : 2010-01-01 15:21:08 By : nutto54
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : อยากทราบวิธีการดึงข้อมูลใน sql2005 ที่เก็บข้อมูล type เป็น xml ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่