/// <summary>
/// define the gloable parameter to save the rel obj data
/// </summary>
public static Dictionary<string, string> deepRef = new Dictionary<string, string>();
/// <summary>
/// 通过表的字段 获取相关的引用字段依赖的对象 直到没有依赖为止
/// </summary>
/// <param name="headField">表字段列表</param>
/// <returns></returns>
public static Dictionary<string,string> getRelRef(List<IniItemsTableItem> headField)
{
HashSet<string> refset = new HashSet<string>();
// HashSet<string> refset_result = new HashSet<string>();
foreach (var item in headField)
{
if (!item.controlType.StartsWith("hz_"))// is not the field of reference
{
continue;
}
string[] fieldarr = item.dataIndex.Split('_');//the constructor is :[tabble]_[field]
refset.Add(fieldarr[0]);//the first prefix
}
dataOperate dao = new dataOperate();
dao.DBServer = "info";
SqlConnection conn = dao.createCon();
try
{
if (refset.Count > 0)
{
if (conn.State != ConnectionState.Open)
conn.Open();//open connection
deepRef = new Dictionary<string, string>();//clear the relation obj data
getRef(conn, refset);
}
return deepRef;
}
catch (Exception)
{
throw;
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
/// <summary>
/// get the relation obj
/// </summary>
/// <param name="conn"></param>
/// <param name="ref_field"></param>
/// <returns></returns>
public static HashSet<string> getRef(SqlConnection conn, HashSet<string> ref_field)
{
HashSet<string> refset = new HashSet<string>();
string refstr = string.Join("','", ref_field);
if (refstr != "")
{
refstr = "'" + refstr + "'";
string sql = "SELECT dbo.GetSplitOfIndex(b.[field],'_',1) as refcode,a.[code] FROM ( select replace(code,'_','') as uncode,* from [SevenWOLDev].[dbo].[sys_tbl_view] ) as a inner join [SevenWOLDev].[dbo].[sys_fld_view] as b on a.uncode=dbo.GetSplitOfIndex(b.[field],'_',1) where replace([table],'_','') in (" + refstr + ") and uitype='ref'";
//get dataset relation obj
DataSet ds = dataOperate.getDataset(conn, sql);
if (ds != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
//rel ref exists
for (int k = 0; k < dt.Rows.Count; k++)
{
string vt = dt.Rows[k].ItemArray[0].ToString();
string vv = dt.Rows[k].ItemArray[1].ToString();
refset.Add(vt);//save current ref
if(!deepRef.ContainsKey(vt))
deepRef.Add(vt, vv);// save all ref
}
if (refset.Count > 0)// get the ref successfully
{
//recursion get
getRef(conn, refset);
}
}
}
}
else
{
//no ref
}
return refset;
}
|