aspx页面上执行SQL查询 [英] Execute SQL Query on aspx page
问题描述
您好老乡程序员,
我真的坚持与simpliest东西 - 我不能找到一种方法来解决我的问题,而无需使用MVC。
I'm really stuck with the simpliest things - i cannot find a way to solve my problem without using mvc.
文件夹结构:
project
-> App_Data
----> mviedb.mdf
-> Default.aspx
----> Default.aspx.cs
-> Web.config
我mviedb数据库至今(表用户):
My mviedb Database so far (table users):
id | user | password
---+--------+------------
1 | daniel | 1
2 | test | test
我要怎么办:基本打印出什么是我的表,所以SELECT * FROM用户 - 但我真的不知道怎么办。我Default.aspx.cs文件:
What I want to do: basically print out what's in my table, so "select * from users" - yet i don't really know how. My Default.aspx.cs file:
namespace mvie {
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
string query = "select * from users";
if (Query != "") {
try {
SqlDataAdapter adp = new SqlDataAdapter(query, @"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\mviedb.mdf;User Instance=true");
DataTable dt = new DataTable();
adp.Fill(dt);
} catch (Exception ex) {
}
}
}
}
}
现在我需要的地方打印数据表。但是如何?我GOOGLE了一段时间,但没能找到没有MVC的解决方案。
now i need to print the DataTable somewhere. But how? I googled for a while but was not able to find a solution without mvc.
推荐答案
根据史蒂夫的评论,ASP.Net WebForms的鼓励您使用诸如<一个控制href=\"https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx\"相对=nofollow> GridView控件
或一<一href=\"https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater%28v=vs.110%29.aspx\"相对=nofollow> 转发
时以表格的方式重新presenting数据。那么,这会使得等价的HTML浏览器。
As per Steve's comment, ASP.Net WebForms encourages you to use controls such as GridView
or a Repeater
when representing data in tabular fashion. This will then render the equivalent Html to the browser.
例如,&LT内部,表=服务器&GT;
标记在的.aspx
文件添加控件:
For example, inside the <form runat="server">
tag in your .aspx
file add the controls:
<asp:DataGrid runat="server" ID="gridUsers"></asp:DataGrid>
<asp:Label runat="server" ID="lblError"></asp:Label>
而在你的code背后:
And in your code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var query = "select * from users";
try
{
// DataAdapters and DataTables are IDisposable
using (var adp = new SqlDataAdapter(query, @"data source=.\SQLEXPRESS;...;"))
using (var dt = new DataTable())
{
adp.Fill(dt);
// Bind the datatable to the grid
gridUsers.DataSource = dt;
gridUsers.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
}
将呈现显示您的用户的所有列的一个非常基本的表
表。
不过,如果可以的话,我建议你离开的WebForms(这是一种传统技术),而是前进到ASP.Net MVC,在那里你会,而不是获取用户数据控制器,并呈现用户表直接在视图。这样你在HTML渲染更精细的控制。
However, if you can, I would recommend that you leave WebForms (which is a legacy technology) and instead move ahead to ASP.Net MVC, where you will instead fetch the users data in a controller, and render the users table directly in a View. This way you have much finer control over the Html rendering.
这篇关于aspx页面上执行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!