Simple ASP.NET Database Query
I have loaded my database with one table under "Data Connections" in the "Server Explorer" pane.
What is the standard / best-practices way to handle a simple query in a VB ASPX page?
My left <div>
would be a set of form el开发者_如何学Pythonements to filter rows, and when the button is clicked, the main <div>
would show the columns I want for the rows returned.
Note: Answers in C# are okay too, I'll just translate.
<%
using(SqlConnection conn = new SqlConnection(someConnectionString))
{
SqlCommand command = new SqlCommand("select * from table", conn);
DataTable results = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
conn.Open();
adapter.Fill(results, command);
}
// You can work with the rows in the DataTable here
%>
Would work if you're trying to do everything in the page code.
I would suggest using the Code-Behind file and working with code that way. It makes things easier to understand when your code is in a seperate file leaving markup in one spot and code in the other.
Presuming web forms you would have a data control, such as a gridview or repeater, bound to a datasource (such as SqlDataSource or ObjectDataSource etc). You would then bind the parameters of your filter controls as control parameters of your datasource. You then need a button to fire a postback and that's basically it.
See Filtering Data Using Data Source Controls for more detail.
There are lots of approaches to this and what is "best" depends on your scenario. This might be a good starting point for you to try doing this with a gridview:
Populating and Editing a DataGrid in ASP.NET
精彩评论