SQL SqlDataSource.Select()? 我如何使用它?(ASP.net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5516464/
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
SqlDataSource.Select()? How do I use this? (ASP.net)
提问by Freesn?w
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.
我知道它的那种分散和模糊,但这是我能做的最好的。我基本上需要将标签文本设置为表中的值。
采纳答案by Hna
This puts the result query in to a 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];
}
回答by Tim
Repying to last question in comment:
回复评论中的最后一个问题:
YourTable.Rows(index)(index)
YourTable.Rows(index)("columnname")
回答by Giorgio C.
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
,在这种情况下只获得了第一个值,但是你可以从数据表中的任何值,并把它变成载体,或其他字符串,这样你就可以控制数据从SqlDataSource的到来。
ENJOY
请享用