我试图加入一个用户表来检索用户的登录名。我希望有两个 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"));
}
}