首页 新闻 会员 周边 捐助

如何手动编写一个强类型数据集????

0
悬赏园豆:5 [待解决问题]

VS2010中,我们可以使用IDE很好的生成.xsd强类型数据集
--------------------------
不过在此我想手动的通过写代码的方式去创建一个强类型的数据集
根据.XSD文件生成的.designer.cs文件
我改编了下:不过提示运行时出错!

//报异常,无法将System.Data.DataRow转换成ProjectDataRow???
代码如下!!!

namespace Shanyl.Data
{
//数据集
public class CustomDataSet : System.Data.DataSet
{
public ProjectDataTable Project;

public CustomDataSet()
{
this.DataSetName = "CustomDataSet";
this.Prefix = "";
this.Namespace = "http://tempuri.org/DataSet1.xsd";
this.EnforceConstraints = true;
this.SchemaSerializationMode = global::System.Data.SchemaSerializationMode.IncludeSchema;
this.Project = new ProjectDataTable();
base.Tables.Add(this.Project);
}
//自定义表
public class ProjectDataTable : System.Data.DataTable
{
#region 定义表中的行

internal System.Data.DataColumn ProjectID;
internal System.Data.DataColumn ProjectName;
internal System.Data.DataColumn ProjectDescription;
internal System.Data.DataColumn IsDeleted;
internal System.Data.DataColumn DateCreated;

#endregion

//初始化行,并添加到本table中
public ProjectDataTable()
: base("Project")
{
this.ProjectID = new System.Data.DataColumn("ProjectID", typeof(int), null, System.Data.MappingType.Element);
this.ProjectName = new System.Data.DataColumn("ProjectName", typeof(string), null, System.Data.MappingType.Element);
this.IsDeleted = new System.Data.DataColumn("IsDeleted", typeof(bool), null, System.Data.MappingType.Element);
this.ProjectDescription = new System.Data.DataColumn("ProjectDescription", typeof(string), null, System.Data.MappingType.Element);
this.DateCreated = new System.Data.DataColumn("DateCreated", typeof(DateTime), null, System.Data.MappingType.Element);

this.Columns.Add(ProjectID);
this.Columns.Add(ProjectName);
this.Columns.Add(ProjectDescription);
this.Columns.Add(IsDeleted);
this.Columns.Add(DateCreated);
}


//索引,提供一个强类型的索引器
public ProjectDataRow this[int index]
{
//报异常,无法将System.Data.DataRow转换成ProjectDataRow???
get { return ((ProjectDataRow)(this.Rows[index])); }
set { this[index] = value; }
}

}

public class ProjectDataRow : System.Data.DataRow
{
//行需要关联表
private ProjectDataTable tableProject;
public ProjectDataRow(System.Data.DataRowBuilder rb)
: base(rb)
{
//datarow知道自己是属于那个表的
this.tableProject = (ProjectDataTable)this.Table;
}

#region 提供属性

public int ProductID
{
get { return (int)(this[this.tableProject.ProjectID]); }
set { this[this.tableProject.ProjectID] = value; }
}

public string ProjectName
{
get { return this[this.tableProject.ProjectName].ToString(); }
set { this[this.tableProject.ProjectName] = value; }
}

public string ProjectDescription
{
get { return this[this.tableProject.ProjectDescription].ToString(); }
set { this[this.tableProject.ProjectDescription] = value; }
}

public bool IsDeleted
{
get { return (bool)(this[this.tableProject.IsDeleted]); }
set { this[this.tableProject.IsDeleted] = value; }
}

public DateTime DateCreated
{
get { return (DateTime)(this[this.tableProject.DateCreated]); }
set { this[this.tableProject.DateCreated] = value; }
}

#endregion
}
}
}

< >
分享
所有回答(1)
0

比如DataRow,你要调用 这个Row["标签"]  这一列,必须 每次都要:

DataRow dr=datatable.Rows[0];

dr["标签"] 很麻烦

 

你定义一个类继承 DataRow

public class SqlSoucre:DataRow

{

  public string 标签

    {

      get{return Convert.Tostring(this["标签"]);}

      set{this["标签"]=value;}

    }

}

 

 

然后你调用的时候:

SqlSource source=new SqlSource();

source.标签=XXXX;即可

 

这就是原生的,.net的强类型其实是把 数据源的结构复制进来了。

Anleb | 园豆:208 (菜鸟二级) | 2012-02-26 17:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册