C# DataSet和DataTable详解
2009-04-21 08:26:15 来源:WEB开发网DataRow dr=ds.Tables["Customers"].Rows.Find("aaa");
if(dr.IsNull("ContactName");
..
else
dr["ContactName"]=DBNull.Value
//
这里判断ContactName列是否为空,如果不是则为其赋空值,呵,很无厘头的做法,这里只为演示为列赋空值的做法。
using System.Data;
using System;
using System.Windows.Forms;
class DataT{
static DataTable dt;// = new DataTable();
static DataSet ds;
static void method1(){
dt = new DataTable("Name");
ds = new DataSet();
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Sex", typeof(string)));
dt.Columns.Add(new DataColumn("Addr", typeof(string)));
}
static void add(int id,string name,string sex,string addr){
DataRow dr = dt.NewRow();
dr["id"] = id;
dr["Name"] = name;
dr["Sex"] = sex;
dr["Addr"] = addr;
dt.Rows.Add(dr);
}
static void Main(){
DataT DT = new DataT();
method1();
add(100,"Join","Male","北京");
add(101,"Lily","feMale","北京");
add(102,"JIM","Male","北京");
ds.Tables.Add(dt);
foreach(DataRow dr in dt.Rows){
//MessageBox.Show(dr["ID"].ToString() + " " + dr["Name"].ToString() + " " + dr["Sex"].ToString() + " " +
// dr["Addr"].ToString(),"Message");
Console.WriteLine(dr["ID"].ToString() + " " + dr["Name"].ToString() + " " + dr["Sex"].ToString() + " " +
dr["Addr"].ToString());
}
try{
foreach(DataTable dt2 in ds.Tables)
foreach(DataRow dr in dt2.Rows)
Console.WriteLine(dr["ID"].ToString() + " " + dr["Name"].ToString() + " " + dr["Sex"].ToString() + " " +
dr["Addr"].ToString());
}catch(Exception ex){
Console.WriteLine("dkfjksdjfk");
}
}
}
更多精彩
赞助商链接