Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用日历选择两个日期之间的所有记录_C#_Sql - Fatal编程技术网

C# 使用日历选择两个日期之间的所有记录

C# 使用日历选择两个日期之间的所有记录,c#,sql,C#,Sql,我想按两个日期搜索记录,这两个日期将由用户通过日历选择。以下是我编写查询的方式: public List<Staff> GetStaff(DateTime DateOne, DateTime DateTwo, string staffColourCode) { List<Staff> l= new List<Staff>(); SqlConnection conn = new connection etc... SqlCommand c

我想按两个日期搜索记录,这两个日期将由用户通过日历选择。以下是我编写查询的方式:

public List<Staff> GetStaff(DateTime DateOne, DateTime DateTwo, string staffColourCode)
{
    List<Staff> l= new List<Staff>();
    SqlConnection conn = new connection etc...
    SqlCommand command = new SqlCommand("SELECT * FROM TableName WHERE CAST(Time AS date) BETWEEN @Time and @Time", conn);
    command.Parameters.AddWithValue("@Time", DateOne);


    conn.Open();
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Staff s= new Staff();
        s.Name= reader["Name"].ToString();
        etc...

        l.Add(s);
    }
    conn.Close();
    return l;
}
public List GetStaff(DateTime DateOne、DateTime DateTwo、string staffcolorcode)
{
列表l=新列表();
SqlConnection conn=新连接等。。。
SqlCommand=newSQLCOMMAND(“从@Time和@Time之间的表名(时间为日期)中选择*”,conn);
command.Parameters.AddWithValue(“@Time”,DateOne);
conn.Open();
SqlDataReader=command.ExecuteReader();
while(reader.Read())
{
职员s=新职员();
s、 Name=reader[“Name”].ToString();
等
l、 添加(s);
}
康涅狄格州关闭();
返回l;
}

在我的数据库中,如果我将时间替换为某个日期,那么它会起作用,但我如何编写一个查询,允许用户选择两个日期,并根据DateOne和DateII搜索具有用户选择的任何颜色代码(例如绿色、黄色等)的员工列表

如上所述,您只使用了一个参数

public List<Staff> GetStaff(DateTime DateOne, DateTime DateTwo,string color)
{
    //Some connection stuff
    SqlCommand command = new SqlCommand(@"SELECT * FROM TableName 
                                          WHERE CAST(Time AS date)> @Time 
                                          and CAST(Time AS date)< @Time2
                                          and staffColorCode = @color", conn);
    command.Parameters.AddWithValue("Time", DateOne);
    command.Parameters.AddWithValue("Time2", DateTwo);
    command.Parameters.AddWithValue("color", color);

    //retrieve data
}
public List GetStaff(DateTime DateOne、DateTime DateTwo、字符串颜色)
{
//一些连接的东西
SqlCommand=newsqlcommand(@“从表名中选择*)
演员位置(时间为日期)>@Time
和演员(时间为日期)<@Time2
和staffColorCode=@color”,康涅狄格州);
command.Parameters.AddWithValue(“Time”,DateOne);
command.Parameters.AddWithValue(“Time2”,DateTwo);
command.Parameters.AddWithValue(“color”,color);
//检索数据
}

添加了其他参数

您需要两个不同的参数:

SqlCommand command = new SqlCommand("SELECT * FROM TableName " + 
                                    "WHERE CAST(Time AS date) BETWEEN @DateOne and @DateTwo", conn);
command.Parameters.AddWithValue("@DateOne", DateOne);
command.Parameters.AddWithValue("@DateTwo", DateTwo);

您目前正在@Time和@Time之间使用
-当然您需要两个不同的参数,一个用于开始,一个用于结束…在@fromDate和@toDate之间使用
我还没有测试,但我想这就是我想要的。最后一个问题,我如何添加另一个WHERE单词,以便选择数据从何处和到何处,职员颜色代码是用户选择的任何颜色。使用
介于
之间。如果您想排除
Time=@Time
Time=@Time2
中的条目,请仅使用此
WHERE
子句。只需在语句中添加另一个and子句,请参见修改后的答案@ThorstenDittmar your correct,但我更喜欢这样写,因为它允许您在需要时更轻松地注释出一行,同时还显示了如何和语句可以链接