数据集更新查询如何工作?
我有一个数据集指向 SQL 数据库中的视图,还有一个使用该数据集来填充自身的网格视图。 我当然希望行可编辑,所以我向 gridview 添加了编辑和删除功能 -
现在使用更新查询的标准方法(不是自定义按钮)...出于某种原因,它没有自动生成更新语句(可能是因为数据集指向一个视图而不是我猜测的表)所以我必须自己创建它。 没问题 - 我选择数据集,选择属性,然后选择 UpdateQuery。
这是我的查询:
UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)
参数源设置为“Form”,我列出了每个参数的名称。
以下是 aspx 页面中的相关代码:
<asp:SqlDataSource ID="dsHardware" runat="server"
ConnectionString="<%$ ConnectionStrings:RequestFormsConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [vHardware]"
UpdateCommand="UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)"
InsertCommand="INSERT INTO [vHardware] ([HardwareID], [TypeID], [Description], [Name], [Cost], [Weight], [Image], [TypeDesc], [TypeName], [TypeTypeID]) VALUES (@HardwareID, @TypeID, @Description, @Name, @Cost, @Weight, @Image, @TypeDesc, @TypeName, @TypeTypeID)"
DeleteCommand="DELETE From tblHardware WHERE HardwareID = @HardwareID">
<UpdateParameters>
<asp:FormParameter FormField="TypeID" Name="TypeID" />
<asp:FormParameter FormField="Name" Name="Name" />
<asp:FormParameter FormField="Description" Name="Description" />
<asp:FormParameter FormField="Cost" Name="Cost" />
<asp:FormParameter FormField="Weight" Name="Weight" />
<asp:FormParameter FormField="Image" Name="Image" />
<asp:FormParameter FormField="HardwareID" Name="HardwareID" />
</UpdateParameters>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="HardwareID,TypeTypeID"
DataSourceID="dsHardware">
<Columns>
<asp:BoundField DataField="HardwareID" HeaderText="HardwareID" InsertVisible="False"
ReadOnly="True" SortExpression="HardwareID" />
<asp:BoundField DataField="TypeID" HeaderText="TypeID" SortExpression="TypeID" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
<asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
<asp:BoundField DataField="TypeDesc" HeaderText="TypeDesc" SortExpression="TypeDesc" />
<asp:BoundField DataField="TypeName" HeaderText="TypeName" SortExpression="TypeName" />
<asp:BoundField DataField="TypeTypeID" HeaderText="TypeTypeID" InsertVisible="False"
ReadOnly="True" SortExpression="TypeTypeID" />
</Columns>
</asp:GridView>
那么这一切是如何工作的呢? 抱歉,我是 ASP.NET 新手 - 以前的经典 ASP 开发人员。
编辑:抱歉,我应该更清楚。 尝试更新时,我收到错误“需要定义@Hardware
”。 不知道为什么,因为它对我来说看起来很明确。 另外,我的 .cs 文件中没有代码 - 我认为我需要那里的东西,但不确定到底是什么。
I have a dataset that points to a View in my SQL database, and a gridview that uses this dataset to populate itself.
I of course want the rows editable, so I added Edit and Delete functionality to the gridview - standard method (not custom buttons)
working with the Update Query now... For some reason it didn't auto-generate the update statement (Probably cause the dataset points to a view rather than a table i'm guessing) so I have to create it myself. No problem - I select the dataset, choose properties, and choose UpdateQuery.
Here is my query:
UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)
The parameter source is set to "Form" and I have the name of each parameter listed.
Here is the relevant code from the aspx page:
<asp:SqlDataSource ID="dsHardware" runat="server"
ConnectionString="<%$ ConnectionStrings:RequestFormsConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [vHardware]"
UpdateCommand="UPDATE dbo.tblHardware SET TypeID = @TypeID, Name = @Name, Description = @Description, Cost = @Cost, Weight = @Weight, Image = @Image WHERE (HardwareID = @HardwareID)"
InsertCommand="INSERT INTO [vHardware] ([HardwareID], [TypeID], [Description], [Name], [Cost], [Weight], [Image], [TypeDesc], [TypeName], [TypeTypeID]) VALUES (@HardwareID, @TypeID, @Description, @Name, @Cost, @Weight, @Image, @TypeDesc, @TypeName, @TypeTypeID)"
DeleteCommand="DELETE From tblHardware WHERE HardwareID = @HardwareID">
<UpdateParameters>
<asp:FormParameter FormField="TypeID" Name="TypeID" />
<asp:FormParameter FormField="Name" Name="Name" />
<asp:FormParameter FormField="Description" Name="Description" />
<asp:FormParameter FormField="Cost" Name="Cost" />
<asp:FormParameter FormField="Weight" Name="Weight" />
<asp:FormParameter FormField="Image" Name="Image" />
<asp:FormParameter FormField="HardwareID" Name="HardwareID" />
</UpdateParameters>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="HardwareID,TypeTypeID"
DataSourceID="dsHardware">
<Columns>
<asp:BoundField DataField="HardwareID" HeaderText="HardwareID" InsertVisible="False"
ReadOnly="True" SortExpression="HardwareID" />
<asp:BoundField DataField="TypeID" HeaderText="TypeID" SortExpression="TypeID" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
<asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
<asp:BoundField DataField="TypeDesc" HeaderText="TypeDesc" SortExpression="TypeDesc" />
<asp:BoundField DataField="TypeName" HeaderText="TypeName" SortExpression="TypeName" />
<asp:BoundField DataField="TypeTypeID" HeaderText="TypeTypeID" InsertVisible="False"
ReadOnly="True" SortExpression="TypeTypeID" />
</Columns>
</asp:GridView>
So how is this all supposed to work? Apologies I am new to ASP.NET - previous classic ASP developer.
EDIT: Sorry I should be more clear. When attempting an Update, I receive the error "@Hardware needs to be defined
". Not sure why as it looks plenty defined to me.
Also, there is NO code in my .cs file - I assume I need something there, but not sure what exactly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里只是一个大胆的猜测,可能是我做错了,但它确实有效 - 尝试将 HardwareID 参数绑定到 gridview?
有点儿。
不过可能会抱怨别的事情。
[编辑]哦,那应该放在你的“UpdateParameters”和“DeleteParameters”中。
[再次编辑] 如果您打算检查数据,则只需要在 .cs 中添加代码。
Just a wild guess here, might be me doing it wrong but it DOES work - try binding the HardwareID parameter to the gridview?
Kindof.
Might complain about something else though.
[edit] Oh, that should go inside your "UpdateParameters" and "DeleteParameters".
[Edit, once more] You will only need code in your .cs if you plan on checking the data.
上面的代码没有关闭asp:SQLDataSource?
或者只是在复制粘贴中剪掉的?
The above code doesn't close the asp:SQLDataSource?
Or was that just snipped out in the copy paste?
好吧,我猜想一些错误的条目(我不知道它们做了什么)以某种方式进入了我的代码。
也就是说,这一行:
...重新制作了 gridview 以使用标准方法。 奇迹般有效!
如果有人正在阅读本文,我如何验证数据?
Ok I guess some erraneous entries (that I have no idea what they do) entered into my code somehow.
Namely, this line:
...re-made the gridview to use standard methods as well. Works like a charm!
If anyone is reading this, how can I validate the data?