1

私のデータセット形式は次のようになります

EMPNAME      FRMDATE      TODATE
ANU          01-10-2012   01-20-2012 
HARI         01-05-2012   02-05-2012

01-17-2012次に、特定の従業員について、テキストボックスから入力を取得します。

私の質問は、i / p日付がデータセットのこれら2つの列(FRMDATE、TODATE)の間にあるかどうかを確認する方法です。

4

3 に答える 3

0

以下の方法が役立つと思います。日付の比較に関する追加の資料については、次の2つのスレッドを参照してください。

linqまたはlambdaを使用して日付を比較する

datetimeインスタンスが他の2つのdatetimeオブジェクトの間にあるかどうかを確認します

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;


public bool IsDateInRange(string date, string employeeId)
{
    DateTime dateToCompare = DateTime.MinValue;
    bool isInRange = false;

    if (!String.IsNullOrEmpty(date) && !String.IsNullOrEmpty(employeeId) &&
        DateTime.TryParse(date, out dateToCompare))
    {
        DataTable table = new DataTable();
        string connectionString = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "SELECT TOP 1 * FROM EmployeeDates WHERE EMPNAME = @EmpName";
                command.Parameters.AddWithValue("@EmpName", employeeId);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(table);

                DateTime fomDate = (DateTime)table.Rows[0]["FRMDATE"];
                DateTime toDate = (DateTime)table.Rows[0]["TODATE"];

                //DateTime.Ticks converts a date into long
                //Now you can simply compare whether the input date falls between the required range
                if (dateToCompare.Ticks >= fomDate.Ticks && dateToCompare.Ticks <= toDate.Ticks)
                {
                    isInRange = true;
                }
                connection.Close();
            }
        }
    }
    return isInRange;
}
于 2012-02-08T07:09:24.680 に答える
0

これを試して

DataRow []_dr= ds.Tables[0].Select( inputDate +">= FRMDATE AND "+inputDate +" <= TODATE");
于 2012-02-03T09:12:53.303 に答える