我正在製作一個Web應用程序,並希望知道從sql查詢中填充列表框的最佳方式,然後在選擇列表框中的某個值時填充文本框。看起來很慢。目前,我打開一個連接(從XML文件)和查詢,將SSN放入列表框中。然後,在selectedindexchanged上重新打開並重新查詢(相同的查詢)以獲取與該SSN相關聯的名稱,並將這些結果放入文本框中。有沒有更好(更快)的方法來做到這一點?下面是我目前擁有的代碼:根據總數據大小我應該如何正確地從Web應用程序查詢SQL?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private SqlConnection Sqlconnection;//= new SqlConnection(ConfigurationManager.ConnectionStrings["xxxConnectionString"].ToString());
private SqlCommand command;
private string sqlQuery = "";
private SqlDataReader reader;
private static string [email protected]"C:\Documents and Settings\xxx\My Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Config.xml";
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Get connection info from config.xml
/// </summary>
private void ReadConnection()
{
string conn = String.Empty;
//Create xml document
XmlDocument xmlDoc = new XmlDocument();
//Load the config file (hard-coded for now)
xmlDoc.Load(configFile);
XmlNodeList connection = xmlDoc.GetElementsByTagName("Connection");
conn = connection[0].InnerText;
Sqlconnection = new SqlConnection(conn);
//return conn;
}
/// <summary>
/// Get query from config.xml
/// </summary>
/// <returns></returns>
private string ReadQuery()
{
if (string.IsNullOrEmpty(sqlQuery))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFile);
XmlNodeList query = xmlDoc.GetElementsByTagName("Query");
sqlQuery = query[0].InnerText;
}
return sqlQuery;
}
/// <summary>
/// Populates textboxes on listBox index changed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
ReadConnection();
SqlCommand command = new SqlCommand("SELECT TOP (100) S.PlanID, S.EmployerID, S.VendorID, S.SSN, D.First, D.Middle, D.Last, D.State, S.NumLoans, S.TypeAcct, S.ERBalance, S.YTDEEContrib FROM SparkData AS S WITH(NOLOCK) INNER JOIN Demographics AS D WITH(NOLOCK) ON S.SSN = D.SSN ORDER BY S.SSN", Sqlconnection);
command = new SqlCommand(ReadQuery(), Sqlconnection);
Sqlconnection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//Assign to textbox here
if (reader["SSN"].ToString() == ListBox2.SelectedValue)
{
TextBox1.Text = reader["First"].ToString();
TextBox2.Text = reader["Middle"].ToString();
TextBox3.Text = reader["Last"].ToString();
}
}
}
}
catch (SqlException ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
Sqlconnection.Close();
SqlConnection.ClearPool(Sqlconnection);
}
}
/// <summary>
/// Connect to sql and populate listbox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
try
{
ReadConnection();
command = new SqlCommand(ReadQuery(), Sqlconnection);
Sqlconnection.Open();
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//Assign SSNs to listbox
ListBox2.Items.Add(reader["SSN"].ToString());
}
}
}
catch (SqlException ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
Sqlconnection.Close();
SqlConnection.ClearPool(Sqlconnection);
}
}
}
}
和
<%@ Page Title="xxx Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about xxx, Inc. <a href="http://www.tsacg.com/" title="xxx Website">www.xxx.com</a>
<br />
Here is the <a href="https://xxx-sql38.xxx.com/"
title="xxx, Inc Intranet Portal">xxx Intranet Portal</a>.
</p>
<div align="center">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Connect"
ToolTip="Click to populate the SSN ListBox"/>
</div>
<div align="center">
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" EnableViewState="true"
onselectedindexchanged="ListBox2_SelectedIndexChanged" Width="200px">
</asp:ListBox>
</div>
<div align="center">
<asp:TextBox ID="TextBox1" Width="125" runat="server">First Name</asp:TextBox>
<asp:TextBox ID="TextBox2" Width="25" runat="server">MI</asp:TextBox>
<asp:TextBox ID="TextBox3" Width="125" runat="server">Last Name</asp:TextBox>
</div>
<%--<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxDataConnectionString %>"--%>
<%--SelectCommand="SELECT TOP (100) S.PlanID, S.EmployerID, S.VendorID, S.SSN, D.First, D.Middle, D.Last, D.State, S.NumLoans, S.TypeAcct, S.ERBalance, S.YTDEEContrib FROM SparkData AS S WITH (NOLOCK) INNER JOIN Demographics AS D WITH (NOLOCK) ON S.SSN = D.SSN ORDER BY S.SSN">
</asp:SqlDataSource>--%>
<%--<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="SSN" DataValueField="SSN" Width="200px" EnableViewState="true" AutoPostBack="true"
onSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>--%>
</asp:Content>
我如何緩存客戶端的所有組合?你有一些代碼或例子嗎? –
每次我在列表框中選擇不同的項目時,需要4或5秒。我做了一個桌面應用程序來做到這一點,我把整個數據集放入列表框,然後只顯示SSN。這讓我已經擁有了名字的數據。我可以和我如何在Web應用程序中執行此操作? –
這並不是一件快速的事情,但是這裏有一些閱讀材料可以從多個角度來涵蓋主題:http://msdn.microsoft.com/en-us/library/hh404101.aspx - 也可以搜索* *「緩存數據客戶端JavaScript」**其他資源 – jefflunt