使ASP.NET中的数据库操作变得简单_Asp.Net开发技巧
时间:2010-09-16 来源:蔚蓝的大海
ASP.NET中一般都是使用SQL Server作为后台数据库。一般的ASP.NET数据库操作示例程序都是使用单独的数据访问,就是说每个页面都写连接到数据库,存取数据,关闭数据库的代码。这种方式带来了一些弊端,一个就是如果你的数据库改变了,你必须一个页面一个页面的去更改数据库连接代码。
第二个弊端就是代码冗余,很多代码都是重复的,不必要的。
因此,我试图通过一种一致的数据库操作类来实现ASP.NET种的数据访问。
我们就拿一般网站上都会有的新闻发布系统来做例子,它需要一个文章数据库,我们把这个数据库命名为 News_Articles。新闻发布系统涉及到 发布新闻,展示文章,管理文章等。
一篇文章一般都会有标题,作者,发表时间,内容,另外我们需要把它们编号。我们把它写成一个类,叫 Article 类,代码如下:
//Article.cs
using System;
namespace News_Articles.Data
{
///
/// Summary description for Article.
///
public class Article
{
private int _id; //文章编号
private string _author; //文章的作者
private string _topic; //文章的标题
private DateTime _postTime; //文章的发表时间
private string _content; //文章内容
public int ID
{
get { return _id;}
set { _id = value;}
}
public string Author
{
get { return _author; }
set { _author = value; }
}
public string Topic
{
get { return _topic; }
set { _topic = value; }
}
public string Content
{
get { return _content; }
set { _content = value; }
}
public DateTime PostTime
{
get { return _postTime; }
set { _postTime = value; }
}
}
}
然后我们写一个文章集合类 ArticleCollection
代码如下
程序代码
//ArticleCollection.cs
using System[color=#0000ff];
using System.Collections;
namespace News_Articles.Data
{
///
/// 文章的集合类,继承于 ArrayList
///
public class ArticleCollection : ArrayList
{
public ArticleCollection() : base()
{
}
public ArticleCollection(ICollection c) : base(c)
{
}
}
}[/color]
这个类相当于一个ASP.NET中的DataSet(其实两者很不一样),很简单,主要的目的是把将很多篇文章集合,以便在ASP.NET页面中给DataGrid或者DataList作为数据源,以显示文章。
现在我们可以实现对News_Articles数据库的操作了,我说过,这是一个数据库操作类。不妨命名为 ArticleDb。实现如下:
程序代码
//ArticleDb.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace News_Articles.Data
{
/**////
/// 数据库操作类,实现文章数据库的读取,插入,更新,删除
///
public class ArticleDb
{
private SqlConnection _conn; //SQL Server 数据库连接
private string _articledb = "News_Articles"; //SQL Server 文章数据库表
/**////
/// 类的初始化,设置数据库连接
///
public ArticleDb()
{
_conn = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
}
/**////
/// 打开数据库连接
///
public void Open()
{
if(_conn.State == ConnectionState.Closed)
_conn.Open();
}
/**////
/// 关闭数据库连接
///
public void Close()
{
if(_conn.State == ConnectionState.Open)
_conn.Close();
}
/**////
/// 读取数据库中所有的 文章
///
/// ArticleCollection
public ArticleCollection GetArticles()
{
ArticleCollection articles = new ArticleCollection();
string sql = "Select * FROM " + _articledb;
SqlCommand cmd = new SqlCommand(sql,_conn);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Article art = PopulateArticle(dr);
articles.Add(art);
}
dr.Close();
return articles;
}
**////
/// 给定一个文章编号, 读取数据库中的一篇文章
///
/// Article
public Article GetArticle(int articleId)
{
string sql = "Select * FROM " + _articledb + "Where ID='" + articleId + "'";
SqlCommand cmd = new SqlCommand(sql,_conn);
SqlDataReader dr = cmd.ExecuteReader();
Article article = PopulateArticle(dr);
dr.Close();
return article;
}
[1] [2] 下一页
best value computer speakersfoxconn motherboard manualxbox live card gamestophow to change ps3 from hdmi to avwindows xp remove uninstallhome wireless lanmonster cable hdmiedimax ew 7318usg sterownikiartist union sims 2northern tool partswireless pci card windows 7dish network international channel listwhite gold body jewelrylinksys wireless g driver wusb54gchow to edit sims 2 picturesnvidia pricenikon d90 photographycomputer cpu speedinternal tv tuner card laptopcomcast whoisdell 1394 connectiondvi splitcablevision channel directoryvision works reviewsat&t corporate calling cardcpu reviewscreative webcam pro ex video softwareuse dazzle capture cardmac usb problemsinstalling video card pcati video cardtactical fiber optic cable assembly2009 vrscf v rod muscleport usb 2.0 hubrca computer monitormicro usb connector dimensionsremove server active directorydish vs charterorbit satellite tvutility recovery systemsbelkin powerline av reviewblue ribbon cableam antenna radio shackserial ata vs eidebest buy high speed internetinstalling pci cardsusb 2.0 satamonitor with bnc connectorfios converterintel pro 10 100 lan