有时在做通用性设计时,会用到视图的别名,但是要想获取该别名对于的列名,sql中没有直接的语句。
这里给出间接方法,供参考。
/// <summary>
/// 获取视图的设计信息
/// </summary>
/// <param name="databaseName"></param>
/// <param name="viewName"></param>
/// <returns></returns>
public DataTable GetViewDesignInformation(string viewName)
{
string sql = "sp_helptext '" + viewName + "' \r\n";
return this.ExecuteDataTable(sql);
}
/// <summary>
/// 获取别名
/// </summary>
/// <param name="_viewName"></param>
/// <param name="column"></param>
/// <returns></returns>
private string GetAliasFromColumn(string _viewName,string column)
{
string alias="";
foreach (string mes in GetValidAliasDesignInformation(_viewName))
{
if (mes.Contains(column))
{
alias= System.Text.RegularExpressions.Regex.Replace(@mes, @"(.*\[)(.*)(.*\])","$2");
}
}
return alias;
}
/// <summary>
/// 获取列名
/// </summary>
/// <param name="_viewName"></param>
/// <param name="alias"></param>
/// <returns></returns>
private string GetColumnFromAlias(string _viewName,string alias)
{
string column = "";
foreach (string mes in GetValidAliasDesignInformation(_viewName))
{
if (mes.Contains(alias))
{
column = System.Text.RegularExpressions.Regex.Replace(@mes, @"(.*\')(.*)(.*\')", "$2");
}
}
return column;
}