如何从Visual Studio中的GridView更新Access数据库? [英] How do I update an an Access database from a GridView in Visual Studio?

查看:855
本文介绍了如何从Visual Studio中的GridView更新Access数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Visual Studio 2010中使用Grid View时更新访问数据库,但没有取得任何成功。让我试着解释我拥有的东西。



我有一个访问数据库,其中有一个表tblConfirmedworkhours,其中有dateworked& 证实。我可以通过编辑/更新链接在我的网页上显示已过滤的表格,但表格不会更新。



选项1:我想(如果可能)不必点击编辑&更新按钮,只是编辑屏幕上的数据(我正在考虑某种类似的东西,连续的形式是MS Access,然后点击某种保存按钮(这(如果可能的话)再次运行已创建的追加查询并存储在我的访问数据库qryToHistory中。

选项2:能够使用编辑/更新按钮来更改表中的数据。



以下是我目前的代码:

 < asp:GridView ID =GridView1 runat =serverAutoGenerateColumns =False
DataSourceID =AccessDataSource1Width =983px
AutoGenerateDeleteButton =TrueAutoGenerateEditButton =True>
< Columns>
< asp:BoundField DataField =WorkHourIDVisible =false
HeaderText =Timesheet ID/>
< asp:BoundField DataField =EmpNameHeaderText =Employee
SortExpression =EmpName/>
< asp:BoundField DataField =dateworked
HeaderText =DateSortExpre ssion =dateworkedApplyFormatInEditMode =True>
< ItemStyle Horizo​​ntalAlign =Center/>
< / asp:BoundField>
< asp:CheckBoxField DataField =confirmedHeaderText =Confirmed
SortExpression =confirmedText =This is OK>
< ItemStyle Horizo​​ntalAlign =Center/>
< / asp:CheckBoxField>
< /列>
< EditRowStyle Width =500pxWrap =True/>
< EmptyDataRowStyle Width =5000px/>
< / asp:GridView>
< asp:AccessDataSource ID =AccessDataSource1runat =server
DataFile =〜/ PayrollDirect.accdb
SelectCommand =SELECT [WorkHourID],[EmpName],[dateworked ] [确认] FROM [tblConfirmedworkhours] WHERE(([CompanyID] =?)AND([confirmed] =?))ORDER BY [dateworked]
UpdateCommand =UPDATE tblConfirmedworkhours SET dateworked = dateworked,confirm = confirm其中WorkHourID = WorkHourID>
< SelectParameters>
< asp:SessionParameter DefaultValue =0Name =CompanyIDSessionField =UserIDType =Int32/>
< asp:Parameter DefaultValue =falseName =confirmedType =Boolean/>
< / SelectParameters>
< UpdateParameters>
< asp:参数名称=dateworked/>
< asp:参数名称=确认/>
< / UpdateParameters>
< / asp:AccessDataSource>

选项1是否可能,如果是这样。



如果选项1不可行,我怎么才能排序更新我的表的问题,如选项2中所示。



我还在学习Visual Studio,所以任何并感谢所有帮助。

解决方案

我也遇到了很多麻烦,并且发现了一些不出来的东西在这些帖子上:我的主要原因是我有一个lastupdated日期时间列,这是必需的,并在数据库中设置一个默认值。我假设数据库会为我处理它的更新,所以我把它作为GridView中的一个隐藏列。



经过几个小时的挫折,我重新开始并测试每一个我的行动的一步。然后我发现隐藏一个必填字段是造成这个问题的原因!您可能还注意到日期格式可能已关闭,具体取决于您的设置,因此如果需要,您还需要以正确的格式提供日期格式。

由方式,它不是一个独特的访问的事情;它甚至发生在Ms SQL服务器上,我猜也会发生在其他数据库中。



我想这也是有关这个报告的几个问题的原因,有时它不',或'它适用于一个表,而不是另一个'。



如果您不希望用户更改某些表的值然后将它们设置为模板并将其编辑/添加属性更改为只读。



还有其他方法可以处理这个问题,例如,提供其值隐藏后面的代码。


I'm trying to update an access database when using a Grid View in Visual Studio 2010 but not having any success. Let me try and explain what I have.

I have an access database with a table "tblConfirmedworkhours" which has fields "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".

Option 2: Be able to use the edit/update buttons to change the data in the table.

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.

If option 1 is not possible, how can I sort the problem of updating my table as in option 2.

I'm still learning Visual Studio so any and all help is gratefully appreciated.

解决方案

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. I assumed that the database would handle its update for me, so I had it as a hidden column in the 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; it happens even with Ms SQL server, and I guess will happen with other databases as well.

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.

这篇关于如何从Visual Studio中的GridView更新Access数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆