You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于时间过滤数据求助:Oracle+ASP.NET Web Forms实现方案

Oracle + ASP.NET Web Forms 时间过滤功能实现方案

一、前端处理(HTML + jQuery)

假设你的页面使用原生日期选择器,以下是示例代码:

<div class="filter-group">
    <label>开始时间:</label>
    <input type="date" id="startDate" required />
    <label>结束时间:</label>
    <input type="date" id="endDate" required />
    <button id="filterBtn">过滤数据</button>
</div>
<div id="dataContainer"></div>

通过jQuery实现AJAX请求,将时间参数传递到后台:

$(function() {
    $("#filterBtn").on("click", function() {
        const startVal = $("#startDate").val();
        const endVal = $("#endDate").val();

        if (!startVal || !endVal) {
            alert("请选择完整的时间范围");
            return;
        }

        $.ajax({
            url: "DataPage.aspx/GetFilteredRecords",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ startDate: startVal, endDate: endVal }),
            dataType: "json",
            success: function(res) {
                let renderHtml = "";
                res.d.forEach(item => {
                    renderHtml += `<div class="record-item">ID: ${item.Id} | 时间: ${item.OutTime}</div>`;
                });
                $("#dataContainer").html(renderHtml);
            },
            error: function(xhr) {
                console.error("请求失败: " + xhr.responseText);
            }
        });
    });
});

二、ASP.NET 后台逻辑

在后台页面(如DataPage.aspx.cs)中实现WebMethod,处理参数并查询Oracle数据库:

using System;
using System.Collections.Generic;
using System.Web.Services;
using Oracle.ManagedDataAccess.Client;

public partial class DataPage : System.Web.UI.Page
{
    [WebMethod]
    public static List<RecordModel> GetFilteredRecords(string startDate, string endDate)
    {
        var records = new List<RecordModel>();
        const string connStr = "Data Source=ORCL;User Id=your_user;Password=your_pwd;";
        
        // 转换前端日期为DateTime类型
        var start = DateTime.Parse(startDate);
        // 结束时间加1天,确保包含当天所有时段的数据
        var end = DateTime.Parse(endDate).AddDays(1);

        const string sql = @"SELECT id, outtime 
                             FROM your_target_table 
                             WHERE outtime >= :StartDate 
                               AND outtime < :EndDate";

        using (var conn = new OracleConnection(connStr))
        {
            using (var cmd = new OracleCommand(sql, conn))
            {
                // 绑定参数并指定Oracle数据类型
                cmd.Parameters.Add(":StartDate", OracleDbType.Date).Value = start;
                cmd.Parameters.Add(":EndDate", OracleDbType.Date).Value = end;

                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        records.Add(new RecordModel
                        {
                            Id = reader.GetInt32(0),
                            OutTime = reader.GetDateTime(1).ToString("yyyy-MM-dd HH:mm:ss")
                        });
                    }
                }
            }
        }
        return records;
    }
}

// 数据实体类
public class RecordModel
{
    public int Id { get; set; }
    public string OutTime { get; set; }
}

三、Oracle 查询核心注意事项

  • 参数化查询:必须使用参数绑定(:参数名),绝对禁止拼接SQL字符串,避免注入风险。
  • Date类型匹配:直接传递DateTime类型参数,不要让Oracle做隐式字符串转Date,避免因时区、格式导致的查询错误。
  • 时间范围覆盖:用>= 开始时间 AND < 结束时间+1天的逻辑,确保包含结束当天23:59:59的所有数据,避免边界遗漏。

四、常见问题排查

  1. 日期解析错误:确保前端传递的日期格式为yyyy-MM-dd,后台DateTime.Parse可直接解析该格式。
  2. 驱动兼容性:使用Oracle.ManagedDataAccess.Client需通过NuGet安装对应版本,确保与Oracle服务器版本兼容。
  3. WebMethod限制:必须标记[WebMethod]且方法为静态,AJAX请求路径需与页面路径一致。
  4. 数据库权限:确认数据库账号拥有目标表的查询权限。

内容的提问来源于stack exchange,提问作者Sharif Ahmed Alif

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:58:27