?? 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>
/// 獲取數(shù)據(jù)訪問對象
/// </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: 在此處添加構(gòu)造函數(shù)邏輯
//
}
/// <summary>
/// 根據(jù)實體信息,查詢相應字段信息
/// </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>
/// 根據(jù)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>返回查詢結(jié)果的實體集合</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>
/// 根據(jù)實體信息生成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>
/// 插入一個新的實體數(shù)據(jù)到數(shù)據(jù)庫
/// </summary>
/// <param name="ent_Temp">要插入的實體數(shù)據(jù)</param>
/// <returns>返回受影響記錄的條數(shù)</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">更新的實體內(nèi)容</param>
/// <returns>返回受影響記錄的條數(shù)</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>
/// 根據(jù)SQL語句向數(shù)據(jù)庫中操作數(shù)據(jù)
/// </summary>
/// <param name="str_Sql">SQL語句</param>
/// <returns>返回受影響的記錄條數(shù)</returns>
public int ExcuteBySql(string str_Sql)
{
return this.dao_Temp.ExcuteBySql(str_Sql);
}
/// <summary>
/// 從數(shù)據(jù)庫中刪除實體數(shù)據(jù)
/// </summary>
/// <param name="ent_Temp">刪除的實體數(shù)據(jù)</param>
/// <returns>返回受影響的記錄條數(shù)</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>
// /// 刪除記錄集合(從數(shù)據(jù)庫中刪除多條記錄)
// /// </summary>
// /// <param name="entSet">實體集合</param>
// /// <returns>返回受影響的記錄的條數(shù)</returns>
// public int DeleteEntitySet(EntitySet entSet)
// {
// return 0;
// }
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -