C# 从c中的sql查询中打印列名及其别名
我有一个复合SQL查询。我只需要提取列名及其别名。这些名称应与每列名称并排打印。 我可以在不连接数据库的情况下解决上述问题吗 查询是:C# 从c中的sql查询中打印列名及其别名,c#,sql,C#,Sql,我有一个复合SQL查询。我只需要提取列名及其别名。这些名称应与每列名称并排打印。 我可以在不连接数据库的情况下解决上述问题吗 查询是: SELECT ISNULL(DISTINCT COALESCE(ActivityName, 'BLANK'), 'NULLVALUE') AS ActName, ActivityPK Primary, ActCode Code, StartDt as ‘Start Date’, (SELECT ECODE FROM ActBCodes WHERE act.Act
SELECT ISNULL(DISTINCT COALESCE(ActivityName, 'BLANK'), 'NULLVALUE') AS ActName, ActivityPK Primary, ActCode Code, StartDt as ‘Start Date’,
(SELECT ECODE FROM ActBCodes WHERE act.ActBCode_FK=BCodePK) ECode FROM TBL_TMX_Activity act
输出应该是
ISNULL(DISTINCT COALESCE(ActivityName, 'BLANK'), 'NULLVALUE') ActName
ActivityPK Primary
ActCode Code
StartDt StartDate
(SELECT ECODE FROM ActBCodes WHERE act.ActBCode_FK=BCodePK) ECode
是的,您可以编写自己的SQL解析器,将SQL的不同部分拆分。我警告你,这可能会让你精疲力尽 您可以使用ANTLR,这是一个解析器/词法分析器工具。它是用来识别“语言”的,比如SQL。正如您所看到的,已经有一些SQL语法了。您可以使用它来解析SQL并获得所需的结果
一个可能更容易为您编写但更容易出错的解决方案是使用正则表达式。如果您想让它可靠,我建议使用ANTLR。我想我在问题中提到过它。不管怎样,查询是:选择ISNULLDISTINCT COALESCEActivityName、'BLANK'、'NULLVALUE'作为ActName、ActivityPK Primary、ActCode Code、StartDt作为'Start Date',选择ActBCodes中的ECODE,其中act.ActBCode_FK=BCodePK ECODE FROM TBL_TMX_Activity actyup,我试图编写自己的sql解析器,但它只适用于此查询,而不是一般查询。因此,我想知道,如果我们通过连接到数据库来执行此操作,是否有任何sql查询可以获取这些详细信息。
using System;
using System.Collections.Generic;
using System.Text;
namespace columm_names
{
public class column
{
public static void Main()
{
string text = "SELECT ISNULL(DISTINCT COALESCE(ActivityName, 'BLANK'), 'NULLVALUE') AS ActName, ActivityPK Primary, ActCode Code, StartDt as ‘Start Date’, (SELECT ECODE FROM ActBCodes WHERE act.ActBCode_FK=BCodePK) ECode FROM TBL_TMX_Activity act";
string s1,s2,text2;
string[] last_str,str;
int first = text.IndexOf("SELECT") + "SELECT".Length;
int last = text.IndexOf("AS");
int count=0;
int index=0;
int x = 1;int i = 0;
char[] seperators = { ' ' };
List<string> column_name = new List<string>();
List<string> alias = new List<string>();
while (text.Length!=0 && last >0)
{
text2 = sub_string(first,last,text);
if (x == 1)
{
column_name.Add(text2);
index = text.IndexOf(text2) + text2.Length+2;text = text.Remove(0, index);
index = text.IndexOf(",");
text2 = text.Substring(0, index);
alias.Add(text2); x = 0;
}
else
{
if (text2.IndexOf("as") > 0)
{
s1 = sub_string(first,text.IndexOf("as"),text);
column_name.Add(s1);
s2 = sub_string (text.IndexOf ("as") + 3, text.IndexOf (","), text);
alias.Add(s2);
}
else
{
str = text2.Split(seperators);
column_name.Add(str[0]);
alias.Add(str[1]);
}
}
index = text.IndexOf(text2) + text2.Length + 1;text = text.Remove(0, index);
first =1;
last = text.IndexOf(",");
if (last < 0)
{
last_str = text.Split(seperators);
text2="";
while(i<last_str.Length)
{
if(count == 2){break;}
else {if(last_str[i]=="FROM"){count++;index =i;}}
i++;
}
for(i=0;i<index-1;i++){text2+=" "+last_str[i];}
column_name.Add(text2);
alias.Add(last_str[index-1]);break;
}
}
Console.WriteLine ("Column Names"); display (column_name);
Console.WriteLine("\n Alias");display (alias);
}
static string sub_string(int x,int y,string z)
{
string r =z.Substring(x, y-x);
return r;
}
static void display(List<string> lst)
{ string[] arr = lst.ToArray();
foreach(string s in arr){Console.WriteLine (s);Console.ReadLine ();}
}
}
}