C# WPF SQL SELECT 内连接

发布于 2024-08-14 18:08:23 字数 3363 浏览 6 评论 0原文

我正在尝试加入用户表以检索用户登录名。 我希望有两个 INNER 连接,一个用于 CreatedByUser_loginnameModifiedByUser_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-08-21 18:08:23

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

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

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

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

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.

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

You are still using User in the join clause,

INNER JOIN [User] AS U1 ON U1.User_ID = [Company].Company_CreatedByUser_ID
旧城空念 2024-08-21 18:08:23

固定

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()));

Fixed

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;

and

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()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文