c# - 从数据库中检索数据的最快方法

标签 c# asp.net sql .net sql-server-2008

我正在使用 C# 和 Sql Server 2008 开发一个 ASP.NET 项目。

我有三个表:

Users DataFields DataField Values

每个用户的每个数据字段都有一个特定的值,这个值存储在 DataFieldsValues 中。

现在我想显示如下所示的报告:

enter image description here

我已经创建了对象 User , 和 DataField .在 DataField 对象中,有方法 string GetValue(User user) ,我在其中获取特定用户的字段值。

然后我有用户列表 List<User> users和数据字段列表 List<DataField> fields然后我执行以下操作:

string html = string.Empty;
html += "<table>";
html += "<tr><th>Username</th>";
foreach (DataField f in fields)
{
   html += "<th>" + f.Name + "</th>";
}
html += "</tr>"

foreach (User u in users)
{
   html += "<tr><td>" + u.Username + "</td>"
   foreach (DataField f in fields)
   {
      html += "<td>" + f.GetValue(u) + "</td>";
   }
   html += "</tr>"
}
Response.Write(html);

这工作正常,但它极度慢,我说的是 20 个用户和 10 个数据字段。在性能方面有没有更好的方法来实现这一目标?

编辑:对于类中的每个参数,我使用以下方法检索值:

public static string GetDataFromDB(string query)
{
    string return_value = string.Empty;
    SqlConnection sql_conn;
    sql_conn = new SqlConnection(ConfigurationManager.ConnectionStrings["XXXX"].ToString());
    sql_conn.Open();
    SqlCommand com = new SqlCommand(query, sql_conn);
    //if (com.ExecuteScalar() != null)
    try
    {
        return_value = com.ExecuteScalar().ToString();
    }
    catch (Exception x)
    {
    }
    sql_conn.Close();
    return return_value;
} 

例如:

public User(int _Id)
{
this.Id = _Id
this.Username = DBAccess.GetDataFromDB("select Username from Users where Id=" + this.Id)
 //...
}

最佳答案

这里有 2 条有用的建议。第一个建议是什么会显着提高你的表现。第二个建议也会有所帮助,但在您的情况下可能不会使您的应用更快。

建议1

您调用方法 GetDataFromDB(string query)常常。这很糟糕,因为您每次都创建一个新的 SqlConnection 和 SqlCommand。这需要时间和资源。此外,如果有任何网络延迟,则会乘以您调用的电话数量。所以这只是个坏主意。

我建议您调用该方法一次并让它填充一个集合,如 Dictionary<int, string>以便您可以从用户 ID 键快速查找您的用户名值。

像这样:

// In the DataField class, have this code.
// This method will query the database for all usernames and user ids and
// return a Dictionary<int, string> where the key is the Id and the value is the 
// username. Make this a global variable within the DataField class.
Dictionary<int, string> usernameDict = GetDataFromDB("select id, username from Users");

// Then in the GetValue(int userId) method, do this:
public string GetValue(int userId)
{
    // Add some error handling and whatnot. 
    // And a better name for this method is GetUsername(int userId)
    return this.usernameDict[userId];
}

建议2

这是您可以改进的另一种方法,尽管在这种情况下略有改进 — 使用 StringBuilder类(class)。有显着的性能提升(这里是概述:http://support.microsoft.com/kb/306822)。

SringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>Username</th>");
foreach (DataField f in fields)
{
    sb.Append("<th>" + f.Name + "</th>");
}

// Then, when you need the string
string html = sb.ToString();

如果您需要更多说明,请告诉我,但您所要求的是非常可行的。我们可以解决这个问题!

如果您进行这 2 项简单的更改,您将获得出色的性能。我保证。

关于c# - 从数据库中检索数据的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399116/

相关文章:

asp.net - 如何将 CSS 应用于在 ASP.NET 中创建的 pdf 文件

asp.net - 使用 Orchard 小部件访问 'Hello, World'

sql - Azure 逻辑应用、SQL 更新行 v2

c# - C# WinForm 中 MessageBox 的连续弹出

c# - ASP.NET WebForms 确认

Asp.net Web 表单(非 MVC)azure 事件目录身份验证

sql - Oracle中没有一个函数错误

php - 更新配置文件 php

c# - 如果是 JSON.NET,我应该为可选字段设置任何属性吗

c# - 在球体中设置玩家的边界