C# WPF SQL SELECT 内连接
我正在尝试加入用户表以检索用户登录名。 我希望有两个 INNER 连接,一个用于 CreatedByUser_loginname
和 ModifiedByUser_loginname
但目前我只是想获得正确的 SQL 查询字符串语法。 但是,当我使用 AS“名称”更改 INNER JOIN 的名称时,执行 sqlreader() 方法时会抛出异常。如果我删除 AS ...它可以工作,但是我无法将第二个 INNER JOIN 添加到同一个用户表中以获取修改后的用户登录名的名称。
这是有问题的代码
这不起作用
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"));
}
}
Im trying to join a user table to retrieve the users login name.
I wish to have TWO INNER joins one for CreatedByUser_loginname
and ModifiedByUser_loginname
But at the moment Im just trying to get the SQL query string syntax right.
BUT, When I change the name of the INNER JOIN with an AS 'name' I get an exception thrown when sqlreader() method is executed. If I remove the AS ... It works, but then I'm unable to add the second INNER JOIN to the same User table to get the name of the modifiedbyuser login name.
Here's the offending code
This doesnt work
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;
This does
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"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用表名的别名(例如 User 作为 U1),则查询中不再存在名称 [user],您必须在对其的所有后续引用中使用 U1。
您仍在连接子句中使用 User,
If you use an alias for a table name such as User as U1, then the name [user] no longer exists for the query, you must use U1 on all of the subsequent references to it.
You are still using User in the join clause,
固定
和
Fixed
and