您的位置:首页 > 其它

怎么搞一个ORM出来用用呢???迷茫中

2005-09-15 12:25 323 查看
已有的结构如下,后面的不知道如何下手了::::::::::::::CRUD,分页,定位,查找等功能怎么做呢?
郁闷中????
哪位大侠指点一下!!!!!!!!
using System;
using System.Diagnostics ;
using System.Collections ;
namespace dhzWeb
{
[Table("orderItem")]
public class ORM_orderitem
{
private int _lstId=0;
[Column(437576597,"orderItem","int","lstId",4,0,2,10,0,true,false,"",true,false,"","")]
public int lstId
{
get { return _lstId;}
set { _lstId=value;}
}
private int _proid=0;0
[Column(437576597,"orderItem","int","proid",4,0,3,10,0,false,false,"",false,true,"proTab","proid")]
public int proid
{
get { return _proid;}
set { _proid=value;}
}
private int _prosl=0;
[Column(437576597,"orderItem","int","prosl",4,0,4,10,0,false,false,"",false,false,"","")]
public int prosl
{
get { return _prosl;}
set { _prosl=value;}
}
private decimal _propri=0.0M;
[Column(437576597,"orderItem","money","propri",8,4,5,19,4,false,false,"",false,false,"","")]
public decimal propri
{
get { return _propri;}
set { _propri=value;}
}
private DateTime _lstdt=DateTime.Now;
[Column(437576597,"orderItem","datetime","lstdt",8,3,6,23,3,false,false,"",false,false,"","")]
public DateTime lstdt
{
get { return _lstdt;}
set { _lstdt=value;}
}
private string _desp="";
[Column(437576597,"orderItem","ntext","desp",16,0,7,1073741823,0,false,true,"",false,false,"","")]
public string desp
{
get { return _desp;}
set { _desp=value;}
}
}
[Table("orders")]
public class ORM_orders
{
private DateTime _orderDt=DateTime.Now;
[Column(405576483,"orders","datetime","orderDt",8,3,2,23,3,false,false,"",false,false,"","")]
public DateTime orderDt
{
get { return _orderDt;}
set { _orderDt=value;}
}
private byte _orderSt=0;
[Column(405576483,"orders","tinyint","orderSt",1,0,3,3,0,false,false,"",false,false,"","")]
public byte orderSt
{
get { return _orderSt;}
set { _orderSt=value;}
}
private int _userId=0;
[Column(405576483,"orders","int","userId",4,0,4,10,0,false,false,"",false,true,"user","userid")]
public int userId
{
get { return _userId;}
set { _userId=value;}
}
}
[Table("proTab")]
public class ORM_protab
{
private string _proName="";
[Column(373576369,"proTab","varchar","proName",50,0,2,50,0,false,false,"名称",false,false,"","")]
public string proName
{
get { return _proName;}
set { _proName=value;}
}
private int _proSL=0;
[Column(373576369,"proTab","int","proSL",4,0,3,10,0,false,false,"数量",false,false,"","")]
public int proSL
{
get { return _proSL;}
set { _proSL=value;}
}
private decimal _proPrice=0.0M;
[Column(373576369,"proTab","money","proPrice",8,4,4,19,4,false,false,"价格",false,false,"","")]
public decimal proPrice
{
get { return _proPrice;}
set { _proPrice=value;}
}
private bool _proSt=false;
[Column(373576369,"proTab","bit","proSt",1,0,5,1,0,false,false,"状态",false,false,"","")]
public bool proSt
{
get { return _proSt;}
set { _proSt=value;}
}
private DateTime _proDt=DateTime.Now;
[Column(373576369,"proTab","datetime","proDt",8,3,6,23,3,false,false,"上传时间",false,false,"","")]
public DateTime proDt
{
get { return _proDt;}
set { _proDt=value;}
}
private string _proDesp="";
[Column(373576369,"proTab","ntext","proDesp",16,0,7,1073741823,0,false,true,"说明",false,false,"","")]
public string proDesp
{
get { return _proDesp;}
set { _proDesp=value;}
}
}
[Table("user")]
public class ORM_user
{
private string _username="";
[Column(469576711,"user","varchar","username",50,0,2,50,0,false,true,"",false,false,"","")]
public string username
{
get { return _username;}
set { _username=value;}
}
private string _userpwd="";
[Column(469576711,"user","varchar","userpwd",50,0,3,50,0,false,true,"",false,false,"","")]
public string userpwd
{
get { return _userpwd;}
set { _userpwd=value;}
}
private string _userEmail="";
[Column(469576711,"user","varchar","userEmail",50,0,4,50,0,false,true,"",false,false,"","")]
public string userEmail
{
get { return _userEmail;}
set { _userEmail=value;}
}
}
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class TableAttribute:System.Attribute
{
public TableAttribute(string tabName)
{
this.tabname=tabName;
}
private string tabname;
public string TabName
{
get{ return this.tabname;}
set { this.tabname=value;}
}
}
/////////////////////////---------------以上均为自动生成的------------------/////////////////////////////
生成代码如下:
System.IO.TextReader tr=System.IO.File.OpenText(Server.MapPath("sql.txt"));
IDataReader dr=dhzWeb.dhzSqlCnn.ExecSql(tr.ReadToEnd ());
tr.Close ();
int ks=dr.FieldCount ;
TableRow tabrow=new TableRow();
for(int i=0;i<ks;i++)
{
TableCell tc=new TableCell();
Literal li=new Literal();
li.Text=dr.GetName(i);
tc.Controls.Add(li);
tabrow.Cells.Add(tc);
}
if(System.IO.File.Exists(Server.MapPath("ORM.cs")))
System.IO.File.Delete(Server.MapPath("ORM.cs"));
System.IO.StreamWriter sw=System.IO.File.CreateText(Server.MapPath("ORM.cs"));
sw.WriteLine("using System;/r/n");
sw.WriteLine("namespace ORM.cs/r/n");
sw.WriteLine("{/r/n");
string preTabName="";
string curTabName="";
int colCnt=dr.FieldCount ;
System.Collections.Hashtable hstab=new Hashtable();

while(dr.Read ())
{

//取表名
curTabName=dr[0].ToString ();
if(hstab.ContainsKey(curTabName))
{
/* ColumnAttribute(Int64 id,string tabName,string dbType ,string colName,int length ,int xscale,
int colid,int isprec,int isScal,bool isident ,bool allownull,string desp,
bool isPrimaryKey,bool isRefKey ,string refTab ,string refTabCol)*/
string headstr="[Column({0},/"{1}/",/"{2}/",/"{3}/",{4},{5},"
+"{6},{7},{8},{9},{10},/"{11}/","
+"{12},{13},/"{14}/",/"{15}/")]";
object[] args=new object[]{
dr[0],
dr[1],
dr[2],
dr[3],
dr[4],
dr[5],
dr[6],
dr[7],
dr[8],
dr[9].ToString ()=="0"?"false":"true",
dr[10].ToString ()=="0"?"false":"true",
dr[11],
dr[12].ToString ()=="0"?"false":"true",
dr[13].ToString ()=="0"?"false":"true",
dr[14],
dr[15]
};
byte[] img=new byte[]{0};
string nettp=dhzWeb.dhzSqlCnn.tpConvert()[args[2].ToString ()].ToString ();
string defstr=dhzWeb.dhzSqlCnn.vpConvert()[args[2].ToString ()].ToString ();
string fdname="_"+args[3].ToString ();

sw.WriteLine("/t/t"+string.Format("private {0} {1}={2};",nettp,fdname,defstr));
sw.WriteLine("/t/t"+string.Format(headstr,args));
sw.WriteLine("/t/t"+" public "+nettp+" "+args[3].ToString ()+" {");
sw.WriteLine("/t/t/t"+ " get { return "+fdname+";}");
sw.WriteLine("/t/t/t"+ " set { "+fdname+"=value;}");
sw.WriteLine("/t/t"+" }");

}
else
{
if(hstab.Count>0)
{
sw.WriteLine("/t}/r/n");//结束前一个
}
hstab.Add(curTabName,dr[1].ToString ());
sw.WriteLine("[Table(/""+dr[1].ToString ()+"/")]");
sw.WriteLine("/t public class ORM_"+dr[1].ToString ().ToLower ()+"{/r/n");
}

}
dr.Close();
sw.WriteLine("/t} /r/n");
sw.WriteLine("}/r/n");
sw.Flush ();
sw.Close ();

////////////////////////////其中用SQL取表属性的语名存放在sql.txt中,其内空如下://///////////////////
select tab.id,--表编号
tabName=tab.name,--表名
dbType=type.name , --数据类型
colName=col.name,--列名
col.length,--宽度
col.xscale ,--小数位数
col.colid ,--列序数
isprec=isnull(columnproperty(tab.id,col.name,'Precision'),0),--精度
isScal=isnull(columnproperty(tab.id,col.name,'scale'),0),--小数位数
isident=COLUMNPROPERTY ( tab.id , col.name , 'IsIdentity' ) ,--是否自增
allownull=COLUMNPROPERTY ( tab.id , col.name , 'AllowsNull' ) ,--允许为空
desp=isnull((select g.value from sysproperties g where g.id=tab.id and g.smallid=col.colid),''),--说明信息
isPrimaryKey= case sign((select count(*) From(
select pkname= pk.name
from sysobjects pk,sysindexkeys idx
where pk.xtype='PK' and tab.xtype='u'
and pk.parent_obj=tab.id
and idx.id=tab.id

and col.colid=idx.colid
)
AS T)
) when 1 then 1 else 0 end ,--是否主键
isRefKey=case sign(
( select count(*) from (
select sonId=fkeyId ,parTab=obj2.name ,ref.fkey
from sysobjects obj ,sysforeignkeys ref,sysobjects obj2,syscolumns col,syscolumns col1
where obj.id=ref.fkeyid
and col.colid=fkey and col.id=ref.fkeyid
and obj2.id=ref.rkeyid
and col1.colid=rkey and col1.id=ref.rkeyid

) as T2 Where T2.sonId=tab.Id and T2.fkey=col.colid)
) when 1 then 1 else 0 end,--取值是否要引用其它表
refTab= isnull( ( select parTab from (
select sonId=fkeyId ,parTab=obj2.name ,ref.fkey
from sysobjects obj ,sysforeignkeys ref,sysobjects obj2,syscolumns col,syscolumns col1
where obj.id=ref.fkeyid
and col.colid=fkey and col.id=ref.fkeyid
and obj2.id=ref.rkeyid
and col1.colid=rkey and col1.id=ref.rkeyid

) as T2 Where T2.sonId=tab.Id and T2.fkey=col.colid),''),---引用表名
refTabCol=isnull((select parTabCol from (
select sonId=fkeyId ,parTabCol=col1.name,ref.fkey
from sysobjects obj ,sysforeignkeys ref,sysobjects obj2,syscolumns col,syscolumns col1
where obj.id=ref.fkeyid
and col.colid=fkey and col.id=ref.fkeyid
and obj2.id=ref.rkeyid
and col1.colid=rkey and col1.id=ref.rkeyid

) as T2 Where T2.sonId=tab.Id and T2.fkey=col.colid),'')--引有列名

from syscolumns col ,sysobjects tab ,systypes type
where tab.xtype='U' and col.id=tab.id
and type.xusertype=col.xusertype
and tab.name<>'dtproperties'

order by tab.name,col.colid
///////////////////////---------------另有两个辅助的方法如下:--------------------////////////////////////
public sealed class dhzSqlCnn
{
public static SqlConnection SqlCnn()
{
return new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["DBConnString"]);
}
public static IDataReader ExecSql(string sql)
{
SqlConnection cnn=dhzSqlCnn.SqlCnn ();
cnn.Open ();
SqlCommand cmd=cnn.CreateCommand ();
cmd.CommandText=sql;
SqlDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection );
return dr;
}
private static Hashtable dic;
/// <summary>
/// SQL数据类型与.net数据类型的转化
/// </summary>
/// <returns></returns>
public static Hashtable tpConvert()
{
if(dhzWeb.dhzSqlCnn.dic==null)
{
dhzWeb.dhzSqlCnn.dic=new Hashtable ();
dhzWeb.dhzSqlCnn.dic.Add("bigint","Int64");
dhzWeb.dhzSqlCnn.dic.Add("binary","byte[]");
dhzWeb.dhzSqlCnn.dic.Add("bit","bool");
dhzWeb.dhzSqlCnn.dic.Add("char","string");
dhzWeb.dhzSqlCnn.dic.Add("nchar","string");
dhzWeb.dhzSqlCnn.dic.Add("varchar","string");
dhzWeb.dhzSqlCnn.dic.Add("nvarchar","string");
dhzWeb.dhzSqlCnn.dic.Add("datetime","DateTime");
dhzWeb.dhzSqlCnn.dic.Add("int","int");
dhzWeb.dhzSqlCnn.dic.Add("float","double");
dhzWeb.dhzSqlCnn.dic.Add("money","decimal");
dhzWeb.dhzSqlCnn.dic.Add("image","byte[]");
dhzWeb.dhzSqlCnn.dic.Add("real","System.Single");
dhzWeb.dhzSqlCnn.dic.Add("smalldatetime","datetime");
dhzWeb.dhzSqlCnn.dic.Add("smallint","short");
dhzWeb.dhzSqlCnn.dic.Add("smallmoney","decimal");
dhzWeb.dhzSqlCnn.dic.Add("text","string");
dhzWeb.dhzSqlCnn.dic.Add("ntext","string");
dhzWeb.dhzSqlCnn.dic.Add("timestamp ","byte[]");
dhzWeb.dhzSqlCnn.dic.Add("tinyint","byte");
dhzWeb.dhzSqlCnn.dic.Add("uniqueidentifier ","Guid");
dhzWeb.dhzSqlCnn.dic.Add("varbinary ","byte[]");
dhzWeb.dhzSqlCnn.dic.Add("variant ","Object");
dhzWeb.dhzSqlCnn.dic.Add("decimal","decimal");
}
return dhzWeb.dhzSqlCnn.dic ;
}
private static Hashtable vic;
/// <summary>
///SQL数据类型对应的默认值
/// </summary>
/// <returns></returns>
public static Hashtable vpConvert()
{
if(dhzWeb.dhzSqlCnn.vic==null)
{
dhzWeb.dhzSqlCnn.vic=new Hashtable ();
dhzWeb.dhzSqlCnn.vic.Add("bigint","new byte[]{0}");
dhzWeb.dhzSqlCnn.vic.Add("binary","new byte[]{0}");
dhzWeb.dhzSqlCnn.vic.Add("bit","false");
dhzWeb.dhzSqlCnn.vic.Add("char","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("nchar","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("varchar","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("nvarchar","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("datetime","DateTime.Now");
dhzWeb.dhzSqlCnn.vic.Add("int","0");
dhzWeb.dhzSqlCnn.vic.Add("float","0.0");
dhzWeb.dhzSqlCnn.vic.Add("money","0.0M");
dhzWeb.dhzSqlCnn.vic.Add("image","new byte[]{0}");
dhzWeb.dhzSqlCnn.vic.Add("real","0.0f");
dhzWeb.dhzSqlCnn.vic.Add("smalldatetime","DateTime.Now");
dhzWeb.dhzSqlCnn.vic.Add("smallint","0");
dhzWeb.dhzSqlCnn.vic.Add("smallmoney","0.0M");
dhzWeb.dhzSqlCnn.vic.Add("text","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("ntext","/"/"");
dhzWeb.dhzSqlCnn.vic.Add("timestamp ","new byte[]{0}");
dhzWeb.dhzSqlCnn.vic.Add("tinyint","0");

dhzWeb.dhzSqlCnn.vic.Add("uniqueidentifier ","Guid.NewGuid()");
dhzWeb.dhzSqlCnn.vic.Add("varbinary ","new byte[]");
dhzWeb.dhzSqlCnn.vic.Add("variant ","new object()");
dhzWeb.dhzSqlCnn.vic.Add("decimal","0.0M");
}
return dhzWeb.dhzSqlCnn.vic ;
}
private static Hashtable tic;
/// <summary>
/// SQL数据类型对应的ADO.net的数据类型
/// </summary>
/// <returns></returns>
public static Hashtable adConvert()
{
if(dhzWeb.dhzSqlCnn.tic==null)
{
dhzWeb.dhzSqlCnn.tic=new Hashtable ();
dhzWeb.dhzSqlCnn.tic.Add("bigint", SqlDbType.BigInt);
dhzWeb.dhzSqlCnn.tic.Add("binary",SqlDbType.Binary);
dhzWeb.dhzSqlCnn.tic.Add("bit",SqlDbType.Bit);
dhzWeb.dhzSqlCnn.tic.Add("char",SqlDbType.Char);
dhzWeb.dhzSqlCnn.tic.Add("nchar",SqlDbType.NChar);
dhzWeb.dhzSqlCnn.tic.Add("varchar",SqlDbType.VarChar);
dhzWeb.dhzSqlCnn.tic.Add("nvarchar",SqlDbType.NVarChar );
dhzWeb.dhzSqlCnn.tic.Add("datetime",SqlDbType.DateTime);
dhzWeb.dhzSqlCnn.tic.Add("int",SqlDbType.Int );
dhzWeb.dhzSqlCnn.tic.Add("float",SqlDbType.Float );
dhzWeb.dhzSqlCnn.tic.Add("money",SqlDbType.Money);
dhzWeb.dhzSqlCnn.tic.Add("image",SqlDbType.Image );
dhzWeb.dhzSqlCnn.tic.Add("real",SqlDbType.Real );
dhzWeb.dhzSqlCnn.tic.Add("smalldatetime",SqlDbType.SmallDateTime);
dhzWeb.dhzSqlCnn.tic.Add("smallint",SqlDbType.SmallInt);
dhzWeb.dhzSqlCnn.tic.Add("smallmoney",SqlDbType.SmallMoney);
dhzWeb.dhzSqlCnn.tic.Add("text",SqlDbType.Text);
dhzWeb.dhzSqlCnn.tic.Add("ntext",SqlDbType.NText);
dhzWeb.dhzSqlCnn.tic.Add("timestamp ",SqlDbType.Timestamp);
dhzWeb.dhzSqlCnn.tic.Add("tinyint",SqlDbType.TinyInt);
dhzWeb.dhzSqlCnn.tic.Add("uniqueidentifier ",SqlDbType.UniqueIdentifier);
dhzWeb.dhzSqlCnn.tic.Add("varbinary ",SqlDbType.VarBinary);
dhzWeb.dhzSqlCnn.tic.Add("variant ",SqlDbType.Variant);
dhzWeb.dhzSqlCnn.tic.Add("decimal",SqlDbType.Decimal);
}
return dhzWeb.dhzSqlCnn.dic ;
}
private static Hashtable sufTab;
public static Hashtable sufEnc()
{
if(dhzWeb.dhzSqlCnn.sufTab==null)
{
dhzWeb.dhzSqlCnn.sufTab=new Hashtable();
dhzWeb.dhzSqlCnn.sufTab.Add("long","l");
dhzWeb.dhzSqlCnn.sufTab.Add("decimal","M");
dhzWeb.dhzSqlCnn.sufTab.Add("float","f");
dhzWeb.dhzSqlCnn.sufTab.Add("double","D");

}
return dhzWeb.dhzSqlCnn.sufTab;
}
}
//////////////////////////////////////////////////----------列属性定义类---------------/////////////////////////
/*id tabName dbType colName length xscale colid isprec isScal isident allownull desp isPrimaryKey isRefKey refTab refTabCol */
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class ColumnAttribute:System.Attribute
{
/// <summary>
/// 列属性
/// </summary>
/// <param name="id">表ID</param>
/// <param name="tabName">表名</param>
/// <param name="dbType">sql数据类型</param>
/// <param name="colName">列名</param>
/// <param name="length">宽度</param>
/// <param name="xscale">小数位数</param>
/// <param name="colid">列ID</param>
/// <param name="isprec">精度</param>
/// <param name="isScal">小数位数</param>
/// <param name="isident">是否自增</param>
/// <param name="allownull">允许空</param>
/// <param name="desp">描述</param>
/// <param name="isPrimaryKey">主键否</param>
/// <param name="isRefKey">外键</param>
/// <param name="refTab">外键表</param>
/// <param name="refTabCol">外键列</param>
/// <param name="defval">默认值</param>
public ColumnAttribute(Int64 id,string tabName,string dbType ,string colName,int length ,int xscale,
int colid,int isprec,int isScal,bool isident ,bool allownull,string desp,
bool isPrimaryKey,bool isRefKey ,string refTab ,string refTabCol)
{
this.id=id;
this.tabName=tabName;
this.dbType=dbType;
this.colName=colName;
this.length=length;
this.xscale=xscale;
this.colid=colid;
this.isprec=isprec;
this.isScal=isScal;
this.isident=isident;
this.allownull=allownull;
this.desp=desp;
this.isPrimaryKey=isPrimaryKey;
this.isRefKey=isRefKey ;
this.refTab=refTab ;
this.refTabCol=refTabCol;
}
private Int64 id;
public Int64 ID
{
get{ return id;}

}
private string tabName;
public String TabName
{
get{ return this.tabName ;}
set{ this.tabName=value;}
}
private string dbType ;
public String SqlType
{
get{ return this.dbType ;}
set{ this.dbType=value;}
}
private string colName;
public string ColName
{
get{ return this.colName;}
set{ this.colName=value;}
}
private int length ;
public int Length
{
get{ return this.length;}
set{ this.length=value;}
}
private int xscale;
public int XScale
{
get{ return this.xscale ;}
set{ this.xscale=value;}
}
private int colid;
public int ColId
{
get{ return this.colid ;}
set{ this.colid=value;}
}
private int isprec;
public int IsPrec
{
get{ return this.isprec ;}
set{ this.isprec=value;}
}
private int isScal;
public int IsScal
{
get{ return this.isScal;}
set{ this.isScal=value;}
}
private bool isident ;
public bool IsIdent
{
get{ return this.isident;}
set{ this.isident=value;}
}
private bool allownull;
public bool AllowNull
{
get{ return this.allownull ;}
set{ this.allownull=value;}
}
private string desp;
public string Desp
{
get{ return this.desp ;}
set{ this.desp=value;}
}
private bool isPrimaryKey;
public bool IsPrimaryKey
{
get{ return this.isPrimaryKey;}
set{ this.isPrimaryKey=value;}
}
private bool isRefKey ;
public bool IsRefkey
{
get{ return this.isRefKey;}
set{ this.isRefKey=value;}
}
private string refTab ;
public string RefTab
{
get{ return this.refTab ;}
set{ this.refTab=value;}
}
private string refTabCol ;
public string RefTabCol
{
get{ return this.refTabCol;}
set{ this.refTabCol=value;}
}

}
}
////////////////////////////
可是后面的CRUD,分页等功能怎么搞呢??????????????????
???????????????????
接口??
反射是一定要的。。。。。。。。
public static object Create(Type rowType,object[] pkArgs)
{

object item=Activator.CreateInstance(rowType,false);
//IDataReader dr=Get
ORM.TableAttribute[] tabn=rowType.GetCustomAttributes(typeof(ORM.TableAttribute),false) as ORM.TableAttribute[];
if(tabn==null)
throw new Exception("没有实现Table属性");
string sqltab=tabn[0].TabName ;
IDataReader dr=null;//=getDataReader by tabName and primarykey lst;
foreach(MemberInfo mi in rowType.GetMembers())
{
Console.WriteLine("Membinfo!"+mi.Name);

foreach(ColumnAttribute attr in mi.GetCustomAttributes(typeof(ORM.ColumnAttribute),false))
{
if(mi.MemberType==MemberTypes.Property)
{
Console.WriteLine("p:"+attr.ColName);
Console.WriteLine("p:"+attr.Length);
Console.WriteLine("p:"+attr.ColId);
((PropertyInfo)mi).SetValue(item,dr[attr.ColName],null);

}

}
}

return item ;
}
哪位指点一下??
QQ:33349170
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐