0

我试图加入一个用户表来检索用户的登录名。我希望有两个 INNER 加入CreatedByUser_loginname一个ModifiedByUser_loginname

但目前我只是试图让 SQL 查询字符串语法正确。但是,当我使用 AS 'name' 更改 INNER JOIN 的名称时,执行 sqlreader() 方法时会引发异常。如果我删除 AS ...它可以工作,但是我无法将第二个 INNER JOIN 添加到同一个 User 表中以获取 modifiedbyuser 登录名的名称。

这是有问题的代码

这不起作用

String sqlComStr = "SELECT Company_ID, Company_Name, Company_Address1, Company_Address2, Company_Address3, Company_Suburb, Company_City, " +
    "Company_State, Company_PostCode, Company_Phone, Company_Fax, Company_WebsiteUrl, Company_Status, Company_ModifiedByUser_ID, Company_CreatedByUser_ID, " +
    " [U1].User_LoginName " +
    "FROM Company INNER JOIN [User] AS U1 ON [User].User_ID = [Company].Company_CreatedByUser_ID WHERE [Company].isDeleted = 0" + companyFilter;

这确实

String sqlComStr = "SELECT Company_ID, Company_Name, Company_Address1, Company_Address2, Company_Address3, Company_Suburb, Company_City, " +
    "Company_State, Company_PostCode, Company_Phone, Company_Fax, Company_WebsiteUrl, Company_Status, Company_ModifiedByUser_ID, Company_CreatedByUser_ID, " +
    " User_LoginName " +
    "FROM Company INNER JOIN [User] ON [User].User_ID = [Company].Company_CreatedByUser_ID WHERE [Company].isDeleted = 0" + companyFilter;

……

String sqlComStr = "SELECT Company_ID, Company_Name, Company_Address1,    Company_Address2, Company_Address3, Company_Suburb, Company_City, " +
    "Company_State, Company_PostCode, Company_Phone, Company_Fax, Company_WebsiteUrl, Company_Status, Company_ModifiedByUser_ID, Company_CreatedByUser_ID, " +
    " [U1].User_LoginName " +
    "FROM Company INNER JOIN [User] AS U1 ON [User].User_ID = [Company].Company_CreatedByUser_ID WHERE [Company].isDeleted = 0" + companyFilter;

sqlcmd.CommandText = sqlComStr;
sqlCon.ConnectionString = "Data Source='PTSSHM\\SQLEXPRESS'; Initial Catalog='PROACTIVE'; Integrated Security=True;";
//sqlCon.ConnectionString = App.appSqlConnectionString;
sqlcmd.Connection = sqlCon;

try
{
    sqlCon.Open();

    SqlDataReader sqlReader = sqlcmd.ExecuteReader(CommandBehavior.Default);
    // If no Exception thrown at this point clear the quote items
    companys.Clear();

    while (sqlReader.Read())
    // Read each row and create a new quite item object int he quoteitems list
    {
        companys.Add(new Company((int)sqlReader["Company_ID"],
            (string)sqlReader["Company_Name"].ToString(),
            (string)sqlReader["Company_Address1"].ToString(),
            (string)sqlReader["Company_Address2"].ToString(), 
            (string)sqlReader["Company_Address3"].ToString(),
            (string)sqlReader["Company_Suburb"].ToString(),
            (string)sqlReader["Company_City"].ToString(),
            (string)sqlReader["Company_State"].ToString(),       
            (string)sqlReader["Company_PostCode"].ToString(),
            (string)sqlReader["Company_Phone"].ToString(),
            (string)sqlReader["Company_Fax"].ToString(),
            (string)sqlReader["Company_WebsiteUrl"].ToString(),
            (string)sqlReader["Company_Status"].ToString(),
            (int)sqlReader["Company_ModifiedByUser_ID"],
            (int)sqlReader["Company_CreatedByUser_ID"],
            (string)sqlReader["[U1].User_LoginName"].ToString(), "d"));
    }
}
4

2 回答 2

2

如果您为表名(例如 User as U1)使用别名,则查询的名称 [user] 不再存在,您必须在所有后续引用中使用 U1。

INNER JOIN [User] AS U1 ON [User].User_ID = [Company].Company_CreatedByUser_ID

您仍在连接子句中使用用户,

INNER JOIN [User] AS U1 ON U1.User_ID = [Company].Company_CreatedByUser_ID
于 2009-12-13T12:17:10.650 回答
1

固定的

String sqlComStr = "SELECT Company_ID, Company_Name, Company_Address1, Company_Address2, Company_Address3, Company_Suburb, Company_City, " +
            "Company_State, Company_PostCode, Company_Phone, Company_Fax, Company_WebsiteUrl, Company_Status, Company_ModifiedByUser_ID, Company_CreatedByUser_ID, " +
            " [CreateUser].User_LoginName AS CreateUser, [ModUser].User_LoginName AS ModUser  " +
            "FROM Company INNER JOIN [User] As [CreateUser] ON [CreateUser].User_ID = [Company].Company_CreatedByUser_ID INNER JOIN [User] AS [ModUser] ON [ModUser].User_ID = [Company].Company_ModifiedByUser_ID WHERE [Company].isDeleted = 0" + companyFilter;

companys.Add(new Company((int)sqlReader["Company_ID"], (string)sqlReader["Company_Name"].ToString(), (string)sqlReader["Company_Address1"].ToString(),
                    (string)sqlReader["Company_Address2"].ToString(), (string)sqlReader["Company_Address3"].ToString(), (string)sqlReader["Company_Suburb"].ToString(),
                    (string)sqlReader["Company_City"].ToString(), (string)sqlReader["Company_State"].ToString(), (string)sqlReader["Company_PostCode"].ToString(),
                    (string)sqlReader["Company_Phone"].ToString(), (string)sqlReader["Company_Fax"].ToString(), (string)sqlReader["Company_WebsiteUrl"].ToString(),
                    (string)sqlReader["Company_Status"].ToString(), (int)sqlReader["Company_ModifiedByUser_ID"], (int)sqlReader["Company_CreatedByUser_ID"],
                    (string)sqlReader["CreateUser"].ToString(), (string)sqlReader["ModUser"].ToString()));
于 2009-12-13T12:26:09.710 回答