简体   繁体   English

如何从 Visual Studio 中的 GridView 更新 Access 数据库?

[英]How do I update an an Access database from a GridView in Visual Studio?

I'm trying to update an access database when using a Grid View in Visual Studio 2010 but not having any success.我正在尝试在 Visual Studio 2010 中使用网格视图时更新访问数据库,但没有任何成功。 Let me try and explain what I have.让我试着解释一下我所拥有的。

I have an access database with a table "tblConfirmedworkhours" which has fields "dateworked" & "confirmed".我有一个包含“tblConfirmedworkhours”表的访问数据库,其中包含“dateworked”和“confirmed”字段。 I can display the filtered table on my webpage with the edit/update links but the table won't update.我可以使用编辑/更新链接在我的网页上显示过滤后的表格,但表格不会更新。

Option 1: I would like (if possible) is to not have to click on the edit & update buttons, just edit the data on the screen (I'm thinking of some kind of similar thing to a continuous form is MS Access and then hit some kind of save button (which (again if possible) would run an append query already created and stored in my access database "qryToHistory".选项 1:我希望(如果可能的话)不必单击编辑和更新按钮,只需编辑屏幕上的数据(我正在考虑某种与连续表单类似的东西是 MS Access,然后点击某种保存按钮((如果可能的话)将运行一个已经创建并存储在我的访问数据库“qryToHistory”中的附加查询。

Option 2: Be able to use the edit/update buttons to change the data in the table.选项 2:能够使用编辑/更新按钮来更改表中的数据。

Here is my current code:这是我当前的代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="AccessDataSource1"  Width="983px" 
    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
   <asp:BoundField DataField="WorkHourID" Visible="false" 
        HeaderText="Timesheet ID" />
    <asp:BoundField DataField="EmpName" HeaderText="Employee" 
        SortExpression="EmpName" />
    <asp:BoundField DataField="dateworked" 
        HeaderText="Date" SortExpression="dateworked" ApplyFormatInEditMode="True">
    <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:CheckBoxField DataField="confirmed" HeaderText="Confirmed" 
        SortExpression="confirmed" Text="This is OK">
    <ItemStyle HorizontalAlign="Center" />
    </asp:CheckBoxField>
</Columns>
<EditRowStyle Width="500px" Wrap="True" />
<EmptyDataRowStyle Width="5000px" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
DataFile="~/PayrollDirect.accdb" 
SelectCommand="SELECT [WorkHourID], [EmpName], [dateworked], [confirmed] FROM [tblConfirmedworkhours] WHERE (([CompanyID] = ?) AND ([confirmed] = ?)) ORDER BY [dateworked]"
UpdateCommand="UPDATE tblConfirmedworkhours SET dateworked=dateworked, confirmed=confirmed where WorkHourID=WorkHourID">
<SelectParameters>
    <asp:SessionParameter DefaultValue="0" Name="CompanyID" SessionField="UserID" Type="Int32" />
    <asp:Parameter DefaultValue="false" Name="confirmed" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
    <asp:Parameter Name="dateworked"/>
    <asp:Parameter Name="confirmed"/>
</UpdateParameters>
</asp:AccessDataSource>

Is Option 1 a possibility, if so how.选项 1 是否有可能,如果有的话。

If option 1 is not possible, how can I sort the problem of updating my table as in option 2.如果选项 1 不可行,我该如何像选项 2 一样对更新我的表的问题进行排序。

I'm still learning Visual Studio so any and all help is gratefully appreciated.我仍在学习 Visual Studio,因此非常感谢任何帮助。

I don't see an update command in your datasource.我在您的数据源中没有看到更新命令。 Are you trying to update it via the aspx page, or do you have a code behind page?您是想通过 aspx 页面更新它,还是有代码隐藏页面?

Try adding an UpdateCommand in the asp:accessDataSource.尝试在 asp:accessDataSource 中添加一个 UpdateCommand。 I suspect that at present, you are asking the tabel to update, when you haven't told it that a) It is allowed to and b) How it should update我怀疑目前,您正在要求表格更新,而您还没有告诉它 a) 允许更新 b) 应该如何更新

After edit.编辑后。

this is from the top of my head.这是来自我的头顶。 But try adding in something like the following in. (Similar to the select)但是尝试添加如下内容。(类似于选择)

  UpdateCommand="UPDATE tblConfirmedworkhours  SET dateWorked=@dateWorked, confirmed =@confirmed where EmpName=@empName">
  <UpdateParameters>
    <asp:Parameter Name="dateWorked" />
    <asp:Parameter Name="confirmed" />
  </UpdateParameters>

I don't have VS on this PC so I can't give an exact answer at the moment sorry.我在这台电脑上没有 VS,所以我现在无法给出确切的答案,抱歉。 However, I think you may need to also have the employee number in your queries, as you may have 2 employees with the same name, and you need a unique reference to distinguish between them.但是,我认为您可能还需要在查询中包含员工编号,因为您可能有 2 名同名员工,并且您需要一个唯一的参考来区分他们。

I had a lot of trouble with this as well and discovered something that is not coming out on these posts: the main reason for me is that I had a lastupdated datetime column, which is required, and is set with a default value in the database.我也遇到了很多麻烦,并发现了这些帖子中没有出现的内容:对我来说,主要原因是我有一个 lastupdated 日期时间列,这是必需的,并且在数据库中设置了默认值. I assumed that the database would handle its update for me, so I had it as a hidden column in the GridView.我假设数据库会为我处理它的更新,所以我把它作为 GridView 中的一个隐藏列。

After hours of frustration, I started afresh and tested every step of my actions.经过数小时的挫折,我重新开始并测试我的每一步行动。 Then I discovered that hiding a required field is all that causes this problem!然后我发现隐藏必填字段是导致此问题的全部原因! You may also notice that the date format might be off, depending on your settings, so you will also need to supply it in the correct format, if required.您可能还会注意到日期格式可能已关闭,具体取决于您的设置,因此如果需要,您还需要以正确的格式提供它。

By the way, it is not a unique Access thing;顺便说一下,它不是一个独特的 Access 东西; it happens even with Ms SQL server, and I guess will happen with other databases as well.它甚至会发生在 Ms SQL 服务器上,我想其他数据库也会发生。

I guess that is also the reason several questions about this report 'sometimes it works, sometimes it doesn't', or 'it works for one table and not another'.我想这也是关于这份报告的几个问题“有时有效,有时无效”或“它适用于一张桌子而不适用于另一张桌子”的原因。

If you don't want the user to change the values of some of your columns, then set them to templates and change their edit/add properties to read-only.如果您不希望用户更改某些列的值,请将它们设置为模板并将其编辑/添加属性更改为只读。

There are also other ways to handle this, eg, supply its value in code behind when it is hidden.还有其他方法可以解决这个问题,例如,在隐藏时在代码后面提供它的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Visual Studio:如何在不激活其他字段验证器的情况下更新GridView? - C# Visual Studio: How do I update GridView without activating validators for other fields? 如何从Visual Studio中的数据库获得自动完成功能? - How do I get an auto completion from a database in visual studio ? 如何在 Visual Studio 中附加数据库 - How do I attach a database in Visual Studio 如何使用Visual Studio在C#应用程序中发布Access数据库? - How do I publish an Access database with my C# application using Visual Studio? 如何连接到 Visual Studio 中的访问数据库 - How can i connect to an access database in Visual Studio 如何从 Visual Studio 2019 更新 Azure 数据库? - How to update an Azure Database from Visual Studio 2019? 我的更新查询不起作用。 我正在使用Visual Studio 2015,并尝试更新访问数据库 - My update query is not working. I'm using Visual Studio 2015, and am trying to update an access database Visual Studio 2010:如何通过编辑器扩展在项目中创建和/或更新特定文件? - Visual Studio 2010: How do I create and/or update a specific file within a Project from an Editor Extension? C#Visual Studio:如何使用LINQ使用DataGridView从Edit更新数据库? - C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ? 在Visual Studio中取消应用程序后如何更新应用程序 - How do i update an application after pusblishing it in visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM