如何在不使用SqlDataSource的情况下更新数据库 [英] How to update database without using SqlDataSource

查看:106
本文介绍了如何在不使用SqlDataSource的情况下更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想访问定义更新查询的方法,我如何在aspx页面上访问该方法&更新网格视图中的数据而不直接使用sql数据源下的aspx页面上的查询?

If I want to access the method in which we define update query, how can I access that method on aspx page & update the data in a grid view without direct use of an query on aspx page under sql data source?

推荐答案

你需要做的是实现一个处理的方法 GridView.RowUpdating事件。在这种方法中,您将不得不从正在更新的行中检索数据,并使用此数据来更新您的数据库。


我不知道你被困在什么地方因此,如果您提供更多信息,我可以为您提供更多有关该主题的帮助。


-Frinny
What you would have to do is implement a method that handles the GridView.RowUpdating Event. In this method you will have to retrieve the data from the row that is updating and use this data to update your database.

I''m not sure what you''re stuck on so if you provide more information I can give you more help on the topic.

-Frinny


@Frinavale


对不起我使用的titile frinny ......

实际上我做了一个数据访问层类,buiness逻辑层类&表示层(.aspx页面)。现在我想在网格视图中更新数据。

在大多数书籍中,一般直接更新查询是使用update commnad属性& ;;在sql数据源下的aspx页面上编写的。在网格视图中进行更新。

这是代码
@Frinavale
sorry frinny for my titile i use......
Actually I made a data acces layer class, buiness logic layer class & presentation layer(.aspx page). Now I want to update the data in a grid view.
In most of the books generally direct update query is written on aspx page under sql data source using update commnad property & do updation in grid view.
Here is the code
展开 | 选择 | Wrap | 行号


我从来没有实际上之前使用过SqlDataSource。我喜欢更好地控制我的数据库连接。


无论如何,这并不难。删除你的SqlDataSource ...你不需要它。您根本不需要使用SqlDataSource。 GridViews可以绑定到DataTables,DataViews,Arrays of Objects ......等等。


因此,你需要在某个时候填充你将用作数据源的对象在您的页面生命周期中。如果需要,可以在页面加载事件中执行此操作。


您可以缓存您将用作数据源的对象。这样您就不必每页回发都重新填充数据源。这节省了时间,但这取决于您可以容忍旧数据的时间长短。正在显示。


无论如何,一旦你填充了数据源(或从缓存中检索它),你就将GridView.DataSource属性设置为你正在使用的对象......然后调用GridView.DataBind方法将GridView绑定到数据源。我在Page PreRender事件中执行此操作有很多原因....例如,如果您在页面加载事件中绑定每个页面请求,您将无法获取用户在编辑时输入的值(因为这些值将是用你想要的数据写的。


在你的GridViews OnRowUpdating事件中,你将调用你的数据访问层的方法进行更新....


这就是它。


-Frinny
I''ve never actually used the SqlDataSource before. I like to be in more control of my database connections.

Anyways, it''s not that hard to do. Delete your SqlDataSource...you don''t need it. You don''t need to use a SqlDataSource at all. GridViews can bind to DataTables, DataViews, Arrays of Objects ...and more.

So, you need to populate the object that you are going to use as your data source at some point in your page life cycle. You could do this in your Page Load event if you want to.

You could cache the object that you are going to use as a data source too. This way you don''t have to repopulate the data source every page postback. This saves time but it depends on how long you can tolerate "old data" being displayed.

Anyways, once you''ve populated your data source (or retrieved it from cache) you set the GridView.DataSource property to the object you are using...then call the GridView.DataBind method to bind the GridView to the data source. I do this in the Page PreRender event for a bunch of reasons....for example if you bind in the Page Load event every page request you will have problems with getting the values that the user entered while editing (because these values will be over written with the data that you''ve bound to).

In your GridViews OnRowUpdating event you will call your data access layer''s methods to do the updating....

That''s pretty much it.

-Frinny


这篇关于如何在不使用SqlDataSource的情况下更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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