📄 entityservice.cs
字号:
using System;
using System.Xml;
using System.Collections;
using System.Data;
namespace EntityMapping
{
/// <summary>
/// EntityMapping 的摘要说明。
/// </summary>
public class EntityService
{
private EntityMapping.Entity ent_ReturnEntity = new Entity();
private DataAccessObject dao_Temp;
private EntityMapping.EntitySet eSet = new EntitySet();
/// <summary>
/// 获取数据访问对象
/// </summary>
public EntityService()
{
try
{
if(this.dao_Temp == null)
{
System.Xml.XmlDocument xml_Doc = new XmlDocument();
//加载XML文件到文档对象中
xml_Doc.Load("d:\\MapConfig.xml");
//获取相应的链接字符串
System.Xml.XmlNodeList xmlNS= xml_Doc.GetElementsByTagName("DataLink");
string str_Con =xmlNS.Item(0).ChildNodes[0].Attributes["Link"].Value.ToString();
this.dao_Temp = new DataAccessObject(EntityMapping.DataType.Oracle,str_Con);
}
}
catch(Exception exp)
{
throw new Exception(exp.ToString());
}
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据实体信息,查询相应字段信息
/// </summary>
/// <param name="ent_Temp">实体</param>
/// <returns>返回查询得到的实体</returns>
public Entity GetEntity(Entity ent_Temp)
{
string str_Sql = "Select * from " + ent_Temp.TableName ;
if(ent_Temp.Attribute.Count > 0)
{
str_Sql = str_Sql + " where ";
IDictionaryEnumerator de_Temp = ent_Temp.Attribute.GetEnumerator();
//遍历整个实体的属性,以组合成相应的查询语句
while(de_Temp.MoveNext())
{
str_Sql = str_Sql + " " + de_Temp.Key.ToString() + de_Temp.Value.ToString();
}
}
this.GetEntityBySql(str_Sql);
return null;
}
/// <summary>
/// 根据SQL语句查询实体
/// </summary>
/// <param name="str_Sql">Sql语句</param>
/// <returns>返回查询得到的实体</returns>
public Entity GetEntityBySql(string str_Sql)
{
DataSet ds_Temp = new DataSet();
try
{
ds_Temp = this.dao_Temp.QueryBySql(str_Sql);
}
catch(Exception exp)
{
throw new Exception(exp.ToString());
}
if(ds_Temp.Tables[0].Rows.Count > 0)
ent_ReturnEntity.FillData(ds_Temp.Tables[0],0);
return ent_ReturnEntity;
}
/// <summary>
/// 查询得到实体集
/// </summary>
/// <param name="str_Sql">查询的SQL语句</param>
/// <returns>返回查询结果的实体集合</returns>
public EntitySet GetEntitySetBySql(string str_Sql)
{
DataSet ds_Temp = new DataSet();
try
{
ds_Temp = this.dao_Temp.QueryBySql(str_Sql);
}
catch(Exception exp)
{
throw new Exception(exp.ToString());
}
this.eSet.DataSource = ds_Temp;
this.eSet.MappingTableName = ds_Temp.Tables[0].TableName;
return eSet;
}
/// <summary>
/// 根据实体信息生成SQL语句查询获得实体集合
/// </summary>
/// <param name="ent_Temp"></param>
/// <returns></returns>
public EntitySet GetEntitySet(Entity ent_Temp)
{
string str_Sql = "Select * from " + ent_Temp.TableName ;
if(ent_Temp.Attribute.Count > 0)
{
str_Sql = str_Sql + " where ";
IDictionaryEnumerator de_Temp = ent_Temp.Attribute.GetEnumerator();
while(de_Temp.MoveNext())
{
str_Sql = str_Sql + " " + de_Temp.Key.ToString() + de_Temp.Value.ToString();
}
}
return GetEntitySetBySql(str_Sql);
}
/// <summary>
/// 插入一个新的实体数据到数据库
/// </summary>
/// <param name="ent_Temp">要插入的实体数据</param>
/// <returns>返回受影响记录的条数</returns>
public int InsertNewEntity(Entity ent_Temp)
{
string str_Sql = "insert into " + ent_Temp.TableName ;
if(ent_Temp.Attribute.Count > 0)
{
str_Sql = str_Sql + " ";
IDictionaryEnumerator de_Temp = ent_Temp.Attribute.GetEnumerator();
string Field = "(" ,Value = "(";
while(de_Temp.MoveNext())
{
Field = Field + de_Temp.Key.ToString() + ",";
Value = Value + de_Temp.Value.ToString() + ",";
}
Field = Field.Substring(0,Field.LastIndexOf(","));
Value = Value.Substring(0,Value.LastIndexOf(","));
Field = Field + ")";
Value = Value + ")";
str_Sql = str_Sql + Field + " values" + Value;
}
this.ExcuteBySql(str_Sql);
return 0;
}
/// <summary>
/// 更新一个实体
/// </summary>
/// <param name="ent_Temp">更新的实体内容</param>
/// <returns>返回受影响记录的条数</returns>
public int UpdateEntity(Entity ent_Temp)
{
string str_Sql = "Update " + ent_Temp.TableName ;
string str_Where = "where 1=1 " ;
if(this.dao_Temp.DBType == EntityMapping.DataType.Oracle)
{
string str_PK = "select col.column_name from user_constraints con, user_cons_columns col"
+ " where con.constraint_name = col.constraint_name and con.constraint_type='P'"
+ " and col.table_name ='" + ent_Temp.TableName.ToUpper() + "'";
System.Data.DataSet ds_Temp = this.dao_Temp.QueryBySql(str_PK);
if(ds_Temp.Tables[0].Rows.Count > 0)
{
for(int i = 0 ; i < ds_Temp.Tables[0].Rows.Count ; i++)
{
str_PK = ds_Temp.Tables[0].Rows[0]["column_name"].ToString();
if((!ent_Temp.Attribute.ContainsKey(str_PK)) & (ent_Temp.Attribute[str_PK] == null))
{
throw new Exception("更新记录时,必须指明主键字段的值");
}
else
{
str_Where = str_Where + " and " + str_PK + "=" + ent_Temp.Attribute[str_PK];
ent_Temp.Attribute.Remove(str_PK);
}
}
}
}
if(ent_Temp.Attribute.Count > 0)
{
str_Sql = str_Sql + " ";
IDictionaryEnumerator de_Temp = ent_Temp.Attribute.GetEnumerator();
str_Sql = str_Sql + "set ";
while(de_Temp.MoveNext())
{
str_Sql = str_Sql + de_Temp.Key.ToString() + "=" + de_Temp.Value.ToString();
}
}
str_Sql = str_Sql + " " + str_Where;
return this.ExcuteBySql(str_Sql);
}
/// <summary>
/// 根据SQL语句向数据库中操作数据
/// </summary>
/// <param name="str_Sql">SQL语句</param>
/// <returns>返回受影响的记录条数</returns>
public int ExcuteBySql(string str_Sql)
{
return this.dao_Temp.ExcuteBySql(str_Sql);
}
/// <summary>
/// 从数据库中删除实体数据
/// </summary>
/// <param name="ent_Temp">删除的实体数据</param>
/// <returns>返回受影响的记录条数</returns>
public int DeleteEntity(Entity ent_Temp)
{
string str_Sql = "delete " + ent_Temp.TableName ;
string str_Where = "where 1=1 " ;
if(this.dao_Temp.DBType == EntityMapping.DataType.Oracle)
{
string str_PK = "select col.column_name from user_constraints con, user_cons_columns col"
+ " where con.constraint_name = col.constraint_name and con.constraint_type='P'"
+ " and col.table_name ='" + ent_Temp.TableName.ToUpper() + "'";
System.Data.DataSet ds_Temp = this.dao_Temp.QueryBySql(str_PK);
if(ds_Temp.Tables[0].Rows.Count > 0)
{
for(int i = 0 ; i < ds_Temp.Tables[0].Rows.Count ; i++)
{
str_PK = ds_Temp.Tables[0].Rows[0]["column_name"].ToString();
if((!ent_Temp.Attribute.ContainsKey(str_PK)) & (ent_Temp.Attribute[str_PK] == null))
{
throw new Exception("删除记录时,必须指明主键字段的值");
}
else
{
str_Where = str_Where + " and " + str_PK + "=" + ent_Temp.Attribute[str_PK];
ent_Temp.Attribute.Remove(str_PK);
}
}
}
}
str_Sql = str_Sql + " " + str_Where;
return this.ExcuteBySql(str_Sql);
}
// /// <summary>
// /// 删除记录集合(从数据库中删除多条记录)
// /// </summary>
// /// <param name="entSet">实体集合</param>
// /// <returns>返回受影响的记录的条数</returns>
// public int DeleteEntitySet(EntitySet entSet)
// {
// return 0;
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -