想知道一下有关MSDN里面的一些例子的效率问题,帮我看看,谢谢!

努力偷懒 2006-10-29 12:39:46
在“ObjectDataSource 类”的页面中有一个例子,代码如下:
namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations one can perform on a NorthwindEmployee object.
//
public class EmployeeLogic {

// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees () {
ArrayList al = new ArrayList();

ConnectionStringSettings cts =
ConfigurationManager.ConnectionStrings["NorthwindConnection"];

SqlDataSource sds
= new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM
Employees");

try {

IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty);

// Iterate through the Enumeration and create a
// NorthwindEmployee object for each ID.
foreach (DataRowView row in IDs) {
string id = row["EmployeeID"].ToString();
NorthwindEmployee nwe = new NorthwindEmployee(id);
// Add the NorthwindEmployee object to the collection.
al.Add(nwe);
}
}
finally {
// If anything strange happens, clean up.
sds.Dispose();
}

return al;
}
public static NorthwindEmployee GetEmployee(object anID) {
return new NorthwindEmployee(anID);
}

public static void UpdateEmployeeInfo(NorthwindEmployee ne) {
bool retval = ne.Save();
if (! retval) { throw new NorthwindDataException("UpdateEmployee
failed."); }
}

public static void DeleteEmployee(NorthwindEmployee ne) { }

}

public class NorthwindEmployee {

public NorthwindEmployee () {
ID = DBNull.Value;
lastName = "";
firstName = "";
title="";
titleOfCourtesy = "";
reportsTo = -1;
}

public NorthwindEmployee (object anID) {
this.ID = anID;

ConnectionStringSettings cts =
ConfigurationManager.ConnectionStrings["NorthwindConnection"];

SqlConnection conn = new SqlConnection (cts.ConnectionString);
SqlCommand sc =
new SqlCommand(" SELECT
FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " +
" FROM Employees " +
" WHERE EmployeeID = @empId",
conn);
// Add the employee ID parameter and set its value.
sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value =
Int32.Parse(anID.ToString());
SqlDataReader sdr = null;

try {
conn.Open();
sdr = sc.ExecuteReader();

// This is not a while loop. It only loops once.
if (sdr != null && sdr.Read()) {
// The IEnumerable contains DataRowView objects.
this.firstName = sdr["FirstName"].ToString();
this.lastName = sdr["LastName"].ToString();
this.title = sdr["Title"].ToString();
this.titleOfCourtesy = sdr["TitleOfCourtesy"].ToString();
if (! sdr.IsDBNull(4)) {
this.reportsTo = sdr.GetInt32(4);
}
}
else {
throw new NorthwindDataException("Data not loaded for employee
id.");
}
}
finally {
try {
if (sdr != null) sdr.Close();
conn.Close();
}
catch (SqlException) {
// Log an event in the Application Event Log.
throw;
}
}
}

private object ID;

private string lastName;
public string LastName {
get { return lastName; }
set { lastName = value; }
}

private string firstName;
public string FirstName {
get { return firstName; }
set { firstName = value; }
}

private string title;
public String Title {
get { return title; }
set { title = value; }
}

private string titleOfCourtesy;
public string Courtesy {
get { return titleOfCourtesy; }
set { titleOfCourtesy = value; }
}

private int reportsTo;
public int Supervisor {
get { return reportsTo; }
set { reportsTo = value; }
}

public bool Save () {
return true;
}
}

internal class NorthwindDataException: Exception {
public NorthwindDataException(string msg) : base (msg) { }
}
}

我看了一下代码,觉得它的流程是这样的:先把employees表中的标识全部取出来,然后遍历循环从数据库中获取每一个标识的数据来创建出对象。它为何不一下子把所有数据库取完,然后再用其它方法来创建它的对象呢?这样它就不用对数据库进行太多次的访问和搜索了啊。不知道我的想法和例子中的思路,哪个效率更高呢?数据量少导没什么,如果上千万条记录的话,又会有什么结果呢?
--
人生难得一傻!
...全文
141 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
努力偷懒 2007-01-01
顶一下,如果真是访问了一次数据库,只读取出一条记录,那是每一个对象做的,现在的问题是它创建多个对象,就看对象的多少来判断它访问了多少次数据库了吧?
  • 打赏
  • 举报
回复
lxhvc 2006-10-29
SqlDataReader 的效率是最高的。它只是访问了一次数据库服务,只是一次只读取检索出的一条记录而已。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.7w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2006-10-29 12:39
社区公告

让您成为最强悍的C#开发者