开发学院软件开发C语言 C# DataSet和DataTable详解 阅读

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"

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

上一页  5 6 7 8 9 10 

Tags:DataSet DataTable 详解

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接