我对 asp.net 和对 GridView 比较陌生,而且我很难完成以下任务:我的数据库中有三个表:
Bookings
--------------------------------------------------------
id | start_date | end_date | user_id | pet_id
----|---------------|--------------|-----------|--------
1 | 01/04/2011 | 01/04/2011 | 1 | 2
--------------------------------------------------------
Pets
---------------------------------------------------------
id | name | species | owner_id | caretaker_id
-----|-------------|----------|------------|-------------
1 | samplename | dog | 1 | 2
---------------------------------------------------------
Users
------------------------------------------------------------------------
id | email | first_name | last_name | password
------|----------------------|-------------|--------------|-------------
1 | test@test.gmail.com | Name | Surname | passwordhere
------------------------------------------------------------------------
我试图做的是使用数据网格来显示预订表中的信息,使其可编辑,以便用户可以编辑他的宠物的预订。使用自动 sql 语句的初始尝试运行良好,SELECT * from [bookings]
返回了 bookings 表,我能够删除和更新行。我决定使用新的选择查询,以便用户可以清楚地看到他的宠物的名字和他作为主人的名字,而不是数字。我使用了以下选择查询,效果很好:
select start_date, end_date, usr.first_name AS 'Owner', pets.name AS 'Pet name' from
bookings AS bookings left join users AS usr ON bookings.user_id=usr.id left join
pets AS pets ON pets.id=bookings.pet_id
Output
----------------------------------------------------------
start_date | end_date | Owner | Pet name |
---------------|-------------|-------------|-------------|
01/04/2011 | 01/04/2011 | Name | samplename |
----------------------------------------------------------
现在问题来了:从我将 GridView 中的选择查询更新到提到的那个时,删除和更新语句都不起作用。当然我会提供自定义的,但无论我尝试什么,它总是返回一个错误incorrect syntax near nvarchar
。我也不确定 pet_id 列是整数还是应该是字符串,因为它在新的连接选择中显示宠物的名称而不是它的 id?
我不知道我在这里做错了什么。我很感激任何帮助。下面是 GridView 的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="start_date" HeaderText="start_date" SortExpression="start_date" />
<asp:BoundField DataField="end_date" HeaderText="end_date" SortExpression="end_date" />
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
<asp:BoundField DataField="Pet name" HeaderText="Pet name" SortExpression="Pet name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pph_database %>" SelectCommand="select start_date, end_date, usr.first_name AS 'Owner', pets.name AS 'Pet name' from bookings AS bookings left join users AS usr ON bookings.user_id=usr.id left join pets AS pets ON pets.id=bookings.pet_id" UpdateCommand="UPDATE [bookings] SET [start_date] = @start_date, [end_date] = @end_date, [user_id] = @user_id, [pet_id] = @pet_id WHERE [id] = @id" DeleteCommand="DELETE FROM [bookings] WHERE [id] = @id" InsertCommand="INSERT INTO [bookings] ([start_date], [end_date], [user_id], [pet_id]) VALUES (@start_date, @end_date, @user_id, @pet_id)">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter DbType="Date" Name="start_date" />
<asp:Parameter DbType="Date" Name="end_date" />
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="pet_id" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="Date" Name="start_date" />
<asp:Parameter DbType="Date" Name="end_date" />
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="pet_id" Type="Int32" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>