Convert SqlDataSource to DataTable and DataView

后端 未结 2 1690
渐次进展
渐次进展 2020-12-19 06:50

I have the following SqlDataSource and I want to convert it to DataView and read a column from it:



        
相关标签:
2条回答
  • 2020-12-19 07:52

    Let me say you have an SqlDataSource named as SqlDataSource1

    DataSourceSelectArguments args = new DataSourceSelectArguments();
    DataView view = (DataView)SqlDataSource1.Select(args);
    DataTable dt = view.ToTable();
    

    Now you can read a column easily from this dt(DataTable) e.g.

    int columnNumber = 0;
    for(int i=0;i<dt.Rows.Count;i++)
    {
        MessageBox.Show(dt.Rows[i][columnNumber].ToString());
    }
    

    Reference

    0 讨论(0)
  • 2020-12-19 07:52

    The SqlDataSource has a Select method which you can call to get back a DataView from your SqlDataSource - see the MSDN documentation on that.

    SqlDataSource dataSource = new SqlDataSource(connectionString, selectSql);
    DataView view = (DataView)dataSource.Select(args);
    
    DataTable table = view.ToTable();
    

    Is that what you're looking for??

    0 讨论(0)
提交回复
热议问题