.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

datarow? Options · View
lpr
Posted: Tuesday, September 16, 2008 1:52:11 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 9/2/2008
Posts: 3

ประกาศ datarow แล้วทำไมไม่มี properety Item

เช่น

DataRow dr;

dr. ( ไม่มี Item)

แก้ใงครับ

ionic
Posted: Tuesday, September 16, 2008 2:30:37 PM

Rank: มือสมัครเล่น
Groups: Member

Joined: 9/5/2008
Posts: 28
Location: BKK

DataRow dr;
object dataCol_1 = dr[ 0 ];
object dataCol_2 = dr[ 1 ];

ใช้ indexer ระบุคอลัมน์ Index ที่ต้องการครับ แล้วมันจะคืนค่าเป็น Object เสร็จแล้วค่อย cast type ตามชนิดของตัวเอง  แต่ต้องระวังว่าค่าที่ได้อาจจะเป็นค่า null (DBNull.Value) เช่น

string strCol_1 = Convert.ToString( dataCol_1 );  //ตรงนี้ไม่ต้องเช็คเพระา string เป็น reference type สามารถเก็บค่า null ได้

// ส่วนตัวแปร int เป็นตัวแปรชนิด Value type ไม่สามารถเก็บค่า null ได้ เราจึงต้องใช้ operator Nullable (?) ช่วย เพื่อให้สามารถเก็บค่า null ได้
int? iCol2 = (dataCol2 == null || dataCol_2 == DBNull.Value) ? null : Convert.ToInt32(dataCol_2);

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by