DataSet多表查询操作

.源码及说明:

 

Code

 

 

.使用方法:

1.在项目中添加引用

2.程序中添加命名空间,如:using Andy.DataSetHelper;

3.定义对象,并实例化,如:DataSetHelper dsHelper = new DataSetHelper(ref ds);   ds为需要操作的DataSet对象

4.调用查询方法,如:dsHelper.SelectJoinInto("查询结果表名", ds.Tables["源表名(子表)"], "查询结果的目标字段", "查询条件", "排序字段");  各参数的说明见前文。

5调用完成后,在ds中将有一张名称为“查询结果表名”的DataTable

.使用用例:

////////定义变量,并初始化

DataSet  ds = new DataSet();

DataSetHelper dsHelper = new DataSetHelper(ref ds);

GridView dataGrid1 = new GridView();

//////////创建第一张表Employees

DataTable dt = new DataTable("Employees");

dt.Columns.Add("EmployeeID",Type.GetType("System.Int32") );

dt.Columns.Add("FirstName", Type.GetType("System.String"));

dt.Columns.Add("LastName", Type.GetType("System.String"));

dt.Columns.Add("BirthDate", Type.GetType("System.DateTime"));

dt.Columns.Add("JobTitle", Type.GetType("System.String"));

dt.Columns.Add("DepartmentID", Type.GetType("System.Int32"));

dt.Rows.Add(new object[] {1, "Tommy", "Hill", new DateTime(1970, 12, 31), "Manager", 42});

dt.Rows.Add(new object[] {2, "Brooke", "Sheals", new DateTime(1977, 12, 31), "Manager", 23});

dt.Rows.Add(new object[] {3, "Bill", "Blast", new DateTime(1982, 5, 6), "Sales Clerk", 42});

ds.Tables.Add(dt); ///////添加到ds

//////////创建第二张表Departments

dt = new DataTable("Departments");

dt.Columns.Add("DepartmentID", Type.GetType("System.Int32"));

dt.Columns.Add("DepartmentName", Type.GetType("System.String"));

dt.Rows.Add(new object[] {15, "Men's Clothing"});

dt.Rows.Add(new object[] {23, "Women's Clothing"});

dt.Rows.Add(new object[] {42, "Children's Clothing"});

ds.Tables.Add(dt); ///////添加到ds

//////////添加关系

ds.Relations.Add("DepartmentEmployee", ds.Tables["Departments"].Columns["DepartmentID"], ds.Tables["Employees"].Columns["DepartmentID"]);

///////////////////条用方法

dsHelper.SelectJoinInto("EmpDept2", ds.Tables["Employees"], "FirstName,LastName,BirthDate BDate,DepartmentEmployee.DepartmentName Department", "JobTitle='Manager'", "DepartmentID");

////////////////////绑定结果

dataGrid1.SetDataBinding(ds, "EmpDept2");

posted @ 2008-11-17 20:33 蓝善根(Andy.Lan) 阅读(...) 评论(...) 编辑 收藏