SqlDataSource.Select()?我怎么用这个?(ASP.net)

时间:2022-01-11 14:41:41

I'm trying to retrieve values using VB.NET from a SQL database. How do I use SqlDataSource.Select()? Is there a way to move the value to a variable that I can use for other things?

我尝试用VB检索值。NET来自SQL数据库。如何使用SqlDataSource.Select()?是否有一种方法可以将值移动到一个我可以用来做其他事情的变量?

I know its kind of scattered and vague but that is the best I can do. I basically need to set a labels text to a value in a table.

我知道它有点分散和模糊,但这是我能做的最好的。我基本上需要将标签文本设置为表中的值。

3 个解决方案

#1


4  

This puts the result query in to a DataTable.

这将结果查询放到一个DataTable中。

DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;

foreach (DataRow dr in dt.Rows)
{
    // Do something here IE grab the value of the first column
    value = dr[0];
}

#2


4  

Repying to last question in comment:

回答最后一个问题:

YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")

#3


1  

I was getting crazy trying to do this simple operation:

做这个简单的手术让我抓狂:

retrieving data from sqldatasource and put it into variables that I can manipulate.

从sqldatasource检索数据并将其放入我可以操作的变量中。

At the end, Here the working behind code to do this for VB.NET:

最后,下面是VB.NET代码背后的工作:

Dim DV As New DataView()


Dim DataTable As New DataTable()

Dim SqlDataSource1 As New SqlDataSource()

Dim VALUE As String



SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString


SqlDataSource1.SelectCommand = "SELECT * from Table"


DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)

DataTable = DV.ToTable()



For Each riga As DataRow In DataTable.Rows
    VALUE = riga("table_name").ToString


Next

the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.

for each,在本例中只获取第一个值,但是您可以从datatable中获取任何值并将其放入vector或其他字符串中,因此您可以控制来自sqldatasource的数据。

ENJOY

享受

#1


4  

This puts the result query in to a DataTable.

这将结果查询放到一个DataTable中。

DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());
DataTable groupsTable = view.ToTable();
String value;

foreach (DataRow dr in dt.Rows)
{
    // Do something here IE grab the value of the first column
    value = dr[0];
}

#2


4  

Repying to last question in comment:

回答最后一个问题:

YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")

#3


1  

I was getting crazy trying to do this simple operation:

做这个简单的手术让我抓狂:

retrieving data from sqldatasource and put it into variables that I can manipulate.

从sqldatasource检索数据并将其放入我可以操作的变量中。

At the end, Here the working behind code to do this for VB.NET:

最后,下面是VB.NET代码背后的工作:

Dim DV As New DataView()


Dim DataTable As New DataTable()

Dim SqlDataSource1 As New SqlDataSource()

Dim VALUE As String



SqlDataSource1.ID = "SqlDataSource1"
Me.Page.Controls.Add(SqlDataSource1)
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Connection_name").ConnectionString


SqlDataSource1.SelectCommand = "SELECT * from Table"


DV = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)

DataTable = DV.ToTable()



For Each riga As DataRow In DataTable.Rows
    VALUE = riga("table_name").ToString


Next

the for each, in this case gets only the first value but you can get any value from datatable and put it into vector, or other strings, so you can control data coming from sqldatasource.

for each,在本例中只获取第一个值,但是您可以从datatable中获取任何值并将其放入vector或其他字符串中,因此您可以控制来自sqldatasource的数据。

ENJOY

享受