我创建了一个接收不同参数的 SQL 查询。当我将参数传递给Region-ID
我时出现错误:
System.Data.SqlClient.SqlException:不明确的列名 'REGION_ID'
当我将数据传递给其他参数时,它的工作没有问题。
我为 sql 查询构建了一个字符串:
StringBuilder constrains = new StringBuilder();
AppendConstrain(constrains, "RAUMKLASSE_ID", RAUMKLASSE_ID);
AppendConstrain(constrains, "STADT_ID", STADT_ID);
AppendConstrain(constrains, "GEBAEUDE_ID", GEBAEUDE_ID);
AppendConstrain(constrains, "REGION_ID", REGION_ID);
AppendConstrain(constrains, "RAUMATTRIBUTE_ID", RAUMATTRIBUTE_ID);
AppendConstrain2(constrains, "r.REGION_ID", "reg.ID");
AppendConstrain2(constrains, "r.STADT_ID", "st.ID");
AppendConstrain2(constrains, "r.ETAGE_ID", "et.ID");
这是我的 SQL 查询:
StringBuilder query =
new StringBuilder("SELECT DISTINCT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID,reg.NAME AS REGNAME, st.NAME AS STNAME, et.BEZEICHNUNG as ETBEZEICHNUNG FROM RAZUORDNUNG rz right join RAUMATTRIBUTE ra ON rz.RAUMATTRIBUTE_ID = ra.ID right join RAUM r ON rz.RAUM_ID = r.ID, REGION reg, STADT st, ETAGE et");
不知道问题出在哪里?
编辑:
这是附加字符串函数:
private static void AppendConstrain(StringBuilder query, string name, string value)
{
if (String.IsNullOrEmpty(value))
return;
if (query.Length > 0)
query.Append(" AND ");
query.AppendFormat("{0} IN ({1})", name, value);
}
private static void AppendConstrain2(StringBuilder query, string name, string name2)
{
if (String.IsNullOrEmpty(name2))
return;
if (query.Length > 0)
query.Append(" AND ");
query.AppendFormat("{0} = ({1})", name, name2);
}