Asp.Net使用DataTable查询数据库数据
2012-09-14 19:14
537 查看
DAL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public static class ArticleService
{
/*************************************************************************************/
//datatable方法
public static DataTable GetDataTable()
{
string strsql = "select * from t_Article";
try
{
DataTable dt = DBHelper.GetDataTable(strsql);
return dt;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
//public static void a()
//{
// SqlConnection conn = new SqlConnection("server=AA296D9D5D2B411;database=accp;uid=sa;pwd=accp;");
// string strsql = "select * from test where name=" + name;
// SqlDataAdapter sda = new SqlDataAdapter(strsql, conn);
// DataTable dt = new DataTable();
// conn.Open();
// sda.Fill(dt);
// Label1.Text = dt.Rows[0]["age"].ToString();
//}
}
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using DAL;
using System.Data;
namespace BLL
{
public class ArticleManager
{
//用DataTable查询
public static DataTable GetDataTable()
{
return ArticleService.GetDataTable();
}
}
}
MODEL层
using System;
using System.Collections.Generic;
using System.Text;
namespace MODEL
{
public class Article
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
private DateTime time;
public DateTime Time
{
get { return time; }
set { time = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
}
}
WEB层
show.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="show.aspx.cs" Inherits="show" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
</asp:GridView>
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</body>
</html>
show.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using MODEL;
using System.Collections.Generic;
using System.Data.SqlClient;
public partial class show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
/*************************************************************************************/
//用DataTable获得数据集(普通的显示)
//DataTable dt = ArticleManager.GetDataTable();
//Label1.Text = dt.Rows[0]["t_ID"].ToString();
//Label2.Text = dt.Rows[0]["t_Title"].ToString();
//Label3.Text = dt.Rows[0]["t_Content"].ToString();
//用DataTable获得数据集(普通的显示)
//DataTable d = ArticleManager.GetDataTable();
//foreach (DataRow row in d.Rows)
//{
// string id = row["t_ID"].ToString();
// string title = row["t_Title"].ToString();
// string content = row["t_Content"].ToString();
// string datae = row["t_Date"].ToString();
// Label lbl1 = new Label();
// Label lbl2 = new Label();
// lbl1.Text = id;
// lbl2.Text = title;
// Label1.Text += lbl1.Text + "<br>";
// Label2.Text += lbl2.Text + "<br>";
//}
//GridView控件用DateTable绑定
//DisplayDateTable();
/*************************************************************************************/
}
}
}
//DataTable绑定
public void DisplayDateTable()
{
DataTable dt = ArticleManager.GetDataTable();
GridView1.DataSource = dt;
GridView1.DataBind();
}
//分页时调用
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//DataSet绑定
GridView1.PageIndex = e.NewPageIndex;
DisplayDateTable();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public static class ArticleService
{
/*************************************************************************************/
//datatable方法
public static DataTable GetDataTable()
{
string strsql = "select * from t_Article";
try
{
DataTable dt = DBHelper.GetDataTable(strsql);
return dt;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
//public static void a()
//{
// SqlConnection conn = new SqlConnection("server=AA296D9D5D2B411;database=accp;uid=sa;pwd=accp;");
// string strsql = "select * from test where name=" + name;
// SqlDataAdapter sda = new SqlDataAdapter(strsql, conn);
// DataTable dt = new DataTable();
// conn.Open();
// sda.Fill(dt);
// Label1.Text = dt.Rows[0]["age"].ToString();
//}
}
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using DAL;
using System.Data;
namespace BLL
{
public class ArticleManager
{
//用DataTable查询
public static DataTable GetDataTable()
{
return ArticleService.GetDataTable();
}
}
}
MODEL层
using System;
using System.Collections.Generic;
using System.Text;
namespace MODEL
{
public class Article
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
private DateTime time;
public DateTime Time
{
get { return time; }
set { time = value; }
}
private string url;
public string Url
{
get { return url; }
set { url = value; }
}
}
}
WEB层
show.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="show.aspx.cs" Inherits="show" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
</asp:GridView>
<div>
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</body>
</html>
show.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using MODEL;
using System.Collections.Generic;
using System.Data.SqlClient;
public partial class show : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
/*************************************************************************************/
//用DataTable获得数据集(普通的显示)
//DataTable dt = ArticleManager.GetDataTable();
//Label1.Text = dt.Rows[0]["t_ID"].ToString();
//Label2.Text = dt.Rows[0]["t_Title"].ToString();
//Label3.Text = dt.Rows[0]["t_Content"].ToString();
//用DataTable获得数据集(普通的显示)
//DataTable d = ArticleManager.GetDataTable();
//foreach (DataRow row in d.Rows)
//{
// string id = row["t_ID"].ToString();
// string title = row["t_Title"].ToString();
// string content = row["t_Content"].ToString();
// string datae = row["t_Date"].ToString();
// Label lbl1 = new Label();
// Label lbl2 = new Label();
// lbl1.Text = id;
// lbl2.Text = title;
// Label1.Text += lbl1.Text + "<br>";
// Label2.Text += lbl2.Text + "<br>";
//}
//GridView控件用DateTable绑定
//DisplayDateTable();
/*************************************************************************************/
}
}
}
//DataTable绑定
public void DisplayDateTable()
{
DataTable dt = ArticleManager.GetDataTable();
GridView1.DataSource = dt;
GridView1.DataBind();
}
//分页时调用
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//DataSet绑定
GridView1.PageIndex = e.NewPageIndex;
DisplayDateTable();
}
}
相关文章推荐
- asp.net查询数据库时提示使用的sql server版本不支持数据类型date
- Asp.Net使用DataSet查询数据库数据
- ASP.NET数据库使用精典-----读取数据库中数据
- 如何使用ASP.NET查询和显示Excel数据
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- 转自:http://m.blog.csdn.net/article/details?id=6554168 在使用order by语句进行查询结果排序时,不同的数据库对于被排序字段数据行为null的情况
- asp.net 中使用EasyUI Datagrid 加载动态数据分页查询
- Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据
- 在asp.net中使用加密数据库联接字符串保证数据安全
- Asp.Net使用Easy UI DataGrid 实现行过滤[行模糊查询数据]
- 如何使用 ASP.NET、ADO.NET 和 Visual C# .NET 查询和显示 Excel 数据
- 如何使用 ASP.NET查询和显示 Excel 数据
- Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据
- 如何使用 ASP.NET查询和显示 Excel 数据
- SolpartMenu的使用:(二)、在ASP.NET页面中使用SolpartMenu控件之动态的加载数据库中的数据来生成菜单
- ASP.NET数据库使用精典-----读取数据库中数据
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(9):服务器端和客户端数据类型的自动转换:DataTable和DataSet
- Asp.net ajax 使用CascadingDropDown以数据库数据建立三级联下拉框
- ASP.NET:使用DataTable对象保存数据