2

ASP ファイルの先頭に次のコードがあります。

<%  
Set rstest = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM Division;" 
rstest.Open sql, db
%>

私が持っている同じASPの本体部分で

<table width="200" border="1">
 <tr>
    <th>Date/Time</th>
    <th>Officer</th>
    <th>Comments</th>
  </tr>
 <tr>
    <td><% = Date_Field %></td>
    <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
    <td><% = Comments %></td>
 </tr>
 <tr>
     <td><% = Date_Field %></td>
 <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
 <td><% = Comments %></td>
 </tr>
 </table>

テーブルに 5 つの一意のレコードがあるにもかかわらず、何らかの理由で重複レコードが 1 つしか表示されません。どうしてこれなの?

4

3 に答える 3

4

以下のコードを試してください。これはうまくいくはずだと思います。

<%  
    Set rstest = Server.CreateObject("ADODB.Recordset")
    sql = "SELECT * FROM Division;" 
    rstest.Open sql, db
%>
    <table width="200" border="1">
        <tr>
            <th>Date/Time</th>
            <th>Officer</th>
            <th>Comments</th>
        </tr>
<%
    if rstest.EOF then
    response.write "No Records Found!"

    Do While NOT rstest.Eof
%>
    <tr>
        <td><% = Date_Field %></td>
        <td><% = First_Name %>&nbsp;<% = Last_Name %></td>
        <td><% = Comments %></td>
    </tr>
<%
    rstest.MoveNext()
    Loop
    End If
    rstest.Close
    Set rstest=nothing
    db.Close
    Set db=nothing
%>  
    </table>
于 2013-08-01T23:00:31.250 に答える
1

ありがとう!これは、正しい解決策を探し回って一日を過ごした後、本当に役に立ちました。ヘッダー、コンテンツ、フッター領域に移動できるように分割した方法が気に入っています。

私のために働いたわずかなバリエーションがあります。これには完全な接続文字列が含まれていることに注意してください。また、少し掘り下げたのは、機能させるためにテーブル名を括弧で囲む必要があることを発見したことです。

<% 
'declare the variables 
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL

'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=localhost;UID=xxxx;" & _ 
"PWD=xxxx;DATABASE=xxxx"

'declare the SQL statement that will query the database
SQL = "SELECT * FROM [xxxx]"

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString

'Open the recordset object executing the SQL statement and return records 
Recordset.Open SQL,Connection
%>

<%
'first of all determine whether there are any records 
If Recordset.EOF Then 
Response.Write("No Users Found.") 
Else 
'if there are records then loop through the fields 
Do While NOT Recordset.Eof  
%>


<div class="row">
    <div class="col-xs-4">USERNAME</div>
    <div class="col-xs-4">PASSWORD</div>
    <div class="col-xs-4">STATUS</div>
<% 
Response.write "<div class='col-xs-4'>"    
Response.write Recordset("userName")
Response.write "</div><div class='col-xs-4'>"    
Response.write Recordset("psword")
Response.write "</div><div class='col-xs-4'>"    
Response.write Recordset("stat")
Response.write "</div>"    
Recordset.MoveNext     
Loop
End If
%>
</div>

<%
'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
于 2016-04-29T14:34:47.197 に答える
0

コードは、レコードセットの最初のレコードのデータのみを表示します。ループをコーディングして残りの部分を取得し、そのコードでテーブル行を作成する必要があります。これらのテーブル行を 1 つの変数に入れ、その変数を使用して、以下の代替方法と同じ方法で一度にすべての行を埋めます。

または、Sql でテーブル行を作成することもできます。

<%  
Set rstest = Server.CreateObject("ADODB.Recordset")
sql = "SELECT '<tr><td>' + Date_Field + '</td><td>' + First_Name + ' ' + Last_Name + '</td><td>' + Comments + '</td></tr>' AS 'Officer_Rows' FROM Division;" 
rstest.Open sql, db
%>

次のように行セット全体を取得します。

<table width="200" border="1">
<tr>
    <th>Date/Time</th>
    <th>Officer</th>
    <th>Comments</th>
</tr>
<% = Officer_Rows %>
</table>

データベース呼び出しから HTML マークアップを取得することは、非常に悪い考えであることを指摘しておく必要があります。@ user704988 の答えは、私のものよりもはるかに優れています。

于 2013-08-01T23:26:57.907 に答える