C# asp:SqlDataSource 到 DataSet 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp:SqlDataSource to DataSet Items
提问by flavour404
I have an asp:SqlDataSource ID="SqlDataSource1" tool on my aspx page, what I want to do in the c# sharp code behind is transfer the records returned by the datasource and put them into a DataSet, so that I can add paging to my page, how do I do that, my attempts so far have failed?!
我的aspx页面上有一个asp:SqlDataSource ID="SqlDataSource1"工具,我想在后面的c#sharp代码中做的是将数据源返回的记录传输到DataSet中,这样我就可以添加分页到我的页面,我该怎么做,到目前为止我的尝试都失败了?!
My attempts so far have been along the lines of:
到目前为止,我的尝试大致如下:
DataSet Items = new DataSet(); Items = SqlDataSource1.Data();
数据集项 = 新数据集();项目 = SqlDataSource1.Data();
But the error I am getting is that the SqlDataSource1 control is not accessible in this context and so obviously the intellisense is not picking it up, so the 'Data()' bit is complete fiction on my part...
但是我得到的错误是在这种情况下无法访问 SqlDataSource1 控件,因此显然智能感知没有接收到它,所以“Data()”位对我来说完全是虚构的......
Thanks, R
谢谢,R
回答by Faheem
flavour404, You should not get that error if you have set up the control properly. I just tested your scenario and it works with out the error you have mentioned.
flavor404,如果您正确设置了控件,则不应出现该错误。我刚刚测试了你的场景,它没有你提到的错误。
SqlDataSource1 does not have any Data method, you might be looking for Select() method and it does not return DataSet. If you set SqlDataSource.DataSourceMode property to 'DataSet' you would get DataView object. See the sample below
SqlDataSource1 没有任何 Data 方法,您可能正在寻找 Select() 方法并且它不返回 DataSet。如果您将 SqlDataSource.DataSourceMode 属性设置为 'DataSet',您将获得 DataView 对象。请参阅下面的示例
<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [Readings]"></asp:SqlDataSource>
DataView testView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
Read MSDN for details:
阅读 MSDN 了解详情:
http://msdn.microsoft.com/en-us/library/dz12d98w.aspx
http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx
http://msdn.microsoft.com/en-us/library/dz12d98w.aspx
http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx
Hope this helps!
希望这可以帮助!
回答by Nation
This Should work! :)
这应该工作!:)
DataView view = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable table = view.ToTable();
DataSet ds = new DataSet();
ds.Tables.Add(table);