2013-12-17 110 views
1

我有這個ASP頁面。我試圖顯示metalquery的結果,列出所有訂單號。當我現在這樣做時,它只顯示第一個訂單號。有沒有辦法顯示查詢中的所有訂單號碼?ASP查詢返回

<%@ Page Language="C#" Debug="true" %> 
<%@ Import Namespace="System.IO" %> 
<%@ Import Namespace="System.Data.Odbc" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head> 
<!-- webkit5 includes --!> 
<!-- <meta content="yes" name="apple-mobile-web-app-capable" /> --!> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" /> 
<link href="css/style.css" rel="stylesheet" media="screen" type="text/css" /> 
<script src="javascript/functions.js" type="text/javascript"></script> 
<title>Open Inkjet Orders</title> 
<meta content="keyword1,keyword2,keyword3" name="keywords" /> 
<meta content="Description of your page" name="description" /> 
</head> 

<body> 
<div id="topbar" class="transparent"> 
    <div id="leftnav"> 
     <a href="Home.aspx"> 
      <img alt="home" src="images/home.png"> 
     </a> 
     <a href="OpenOrders.aspx">Open Orders</a> 
    </div> 
</div> 

<div id="content"> 
<% 
OdbcConnection cn; 
OdbcCommand cmd; 

String canvasQuery = "select count(distinct(info.orderid)) from orderinfo info, orderiteminfo item where info.orderstatus = 'Open' and info.orderid = item.orderid and item.deptcode in (35) and info.duedate = DATE(NOW())"; 
String wallWrapQuery = "select count(distinct(info.orderid)) from orderinfo info, orderiteminfo item where info.orderstatus = 'Open' and info.orderid = item.orderid and item.deptcode in (41) and info.duedate = DATE(NOW())"; 
String fineartQuery = "select count(distinct(info.orderid)) from orderinfo info, orderiteminfo item where info.orderstatus = 'Open' and info.orderid = item.orderid and item.deptcode in (03) and info.duedate = DATE(NOW())"; 
String metalQuery = "select distinct(info.orderid) from orderinfo info, orderiteminfo item where info.orderstatus = 'Open' and info.orderid = item.orderid and item.deptcode in (48) and info.duedate = DATE(NOW())"; 

int canvasOpen, wallWrapOpen, fineartOpen, metalOpen; 


cn= new OdbcConnection("dsn=xxxx;UID=xxxx;PWD=xxxx"); 

cmd=new OdbcCommand(canvasQuery,cn); 
cn.Open(); 
OdbcDataReader DbReader = cmd.ExecuteReader(); 

DbReader.Read(); 
canvasOpen = DbReader.GetInt32(0); 

cmd=new OdbcCommand(wallWrapQuery,cn); 
DbReader = cmd.ExecuteReader(); 
DbReader.Read(); 
wallWrapOpen = DbReader.GetInt32(0); 

cmd=new OdbcCommand(fineartQuery,cn); 
DbReader = cmd.ExecuteReader(); 
DbReader.Read(); 
fineartOpen = DbReader.GetInt32(0); 

cmd=new OdbcCommand(metalQuery,cn); 
DbReader = cmd.ExecuteReader(); 
DbReader.Read(); 
metalOpen = DbReader.GetInt32(0); 

DbReader.Close(); 
cn.Close(); 

%> 
    <span class="graytitle">Due Inkjet Orders</span> 
     <ul class="pageitem"> 
      <li class="menu"> 

        <span class="name">Canvas Dept: <%=canvasOpen%></span> 

      </li> 
      <li class="menu"> 
       <span class="name">Wall Cling Dept: <%=wallWrapOpen%></span> 
      </li> 
      <li class="menu"> 
        <span class="name">Fine Art Dept: <%=fineartOpen%></span> 
      </li> 
      <li class="menu"> 
        <span class="name">Metal Dept: <%=metalOpen%></span> 
      </li> 
      <li> 
      </li> 
     </ul> 
</div> 
</body> 

</html> 
+0

您考慮循環dbReader中的記錄而不是僅顯示第一個記錄? – JohnFx

+0

您只顯示1個值,因爲這是您從代碼中獲得的所有內容。我建議使用中繼器或某種ASP.NET控件,可以通過數據綁定顯示查詢結果。 –

回答

1

您必須一直調用Read()方法,直到它返回false。

每個Read()調用只返回一條記錄。

試試這個:

while(DbReader.Read()) 
{ 
    metalOpen = DbReader.GetInt32(0); 

%> 
    <span class="graytitle">Due Inkjet Orders</span> 
     <ul class="pageitem"> 
      <li class="menu"> 
       <span class="name">Canvas Dept: <%=canvasOpen%></span> 
      </li> 
      <li class="menu"> 
       <span class="name">Wall Cling Dept: <%=wallWrapOpen%></span> 
      </li> 
      <li class="menu"> 
       <span class="name">Fine Art Dept: <%=fineartOpen%></span> 
      </li> 
      <li class="menu"> 
       <span class="name">Metal Dept: <%=metalOpen%></span> 
      </li> 
      <li> 
     </li> 
     </ul> 
    </span> 
%> 
} 
DbReader.Close(); 
cn.Close(); 
0

您需要添加一個循環讀取多個記錄(以及改變metalOpen到List<int>存儲多個值:

List<int> metalOpen = new List<int>(); 
cmd=new OdbcCommand(metalQuery,cn); 
DbReader = cmd.ExecuteReader(); 
while(DbReader.Read()) 
{ 
    metalOpen.Add(DbReader.GetInt32(0)); 
} 

然後決定如何diplay他們 - 最簡單的方法是顯示逗號分隔列表:

 <li class="menu"> 
       <span class="name">Metal Dept: <%=string.Join(", ",metalOpen)%></span> 
     </li> 
1

以下查詢,在開始你的代碼塊中,被指定可以返回零,一個或多個記錄的查詢 - 更確切地說是orderid領域:

String metalQuery = "select distinct(info.orderid) from orderinfo info, orderiteminfo item where info.orderstatus = 'Open' and info.orderid = item.orderid and item.deptcode in (48) and info.duedate = DATE(NOW())"; 

而這個塊執行對源數據庫的查詢:

cmd=new OdbcCommand(metalQuery,cn); 
DbReader = cmd.ExecuteReader(); 

現在您需要遍歷每個結果。您可以通過使用.Read()方法來完成此操作。整潔的事情是,只要ResultSet上有記錄,就可以使用.Read()

然後,您可以封裝一個在塊上的.Read()迭代:

while(DbReader.Read()) 
{ 
    //Here goes your code 
} 

在你的情況下,它可能會是這樣的:

while(DbReader.Read()) 
{ 
    metalOpen = DbReader.GetInt32(0); 
%> 
    <span class="name">Metal Dept: <%=metalOpen%></span> 
<% 
} 

當然,輸出根據需要進行調整。