简体   繁体   中英

SQL: ALIAS, INNER JOIN not working

I'm doing a school project where i need to do following in asp.net:

Make a list of photoalbums with the number of images in each album.

I keep getting an error when i try to open the page in my browser (Chrome/Windows):

Server Error in '/' Application.

Incorrect syntax near 'Album'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'Album'.

I suspect it has something to do with the SelectCommand and INNER JOIN.

My database has 2 tables: "Album" and "Billeder" (which means images in danish).

The table "Album" has following columns:

  • Id
  • navn (name)
  • oprettetDen (Date created)
  • redigeretDen (Date Edited)

The table "Billeder" has the following columns:

  • Id
  • imgnavn (image name)
  • thumbnavn (thumbnail image name)
  • alt (image text)
  • oprettetDen (Date created)
  • redigeretDen (Date Edited)
  • fkAlbumId (Foreign key Id for "Album" -table)

Here is my code:

<asp:SqlDataSource ID="SqlDataAlbums" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT Billeder.imgnavn AS billede Album.navn AS albums FROM [Billeder] INNER JOIN Album ON Album.Id = Billeder.fkAlbumId"></asp:SqlDataSource>
<table class="table table-striped">
    <tr>
        <th>Navn</th>
        <th>Antal billeder i Albummet</th>
    </tr>
    <asp:Repeater ID="RepeaterAlbums" DataSourceID="SqlDataAlbums" runat="server">
        <ItemTemplate>
            <tr>
                <td><%# Eval ("albums") %></td>
                <td></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table> 

Missing a comma between Billeder.imgnavn AS billede and Album.navn AS albums

SELECT Billeder.imgnavn AS billede, Album.navn AS albums 
FROM [Billeder] INNER JOIN Album ON Album.Id = Billeder.fkAlbumId

Your SELECT statement is missing a comma:

SELECT Billeder.imgnavn AS billede Album.navn

should be

SELECT Billeder.imgnavn AS billede, Album.navn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM