简体   繁体   English

在c#ASP.Net中查找当天的当前日期

[英]Find current day of week in c# ASP.Net

I want to find current day of week in c# ASP.Net like: 我想在c#ASP.Net中找到当前的一周,如:

System.DateTime.Now.DayOfWeek.ToString(); 

This code works well in a console application, but when I try to store the value of this line in a string in ASP.Net website it returns nothing. 此代码在控制台应用程序中运行良好,但是当我尝试将此行的值存储在ASP.Net网站的字符串中时,它什么都不返回。

Can anyone help? 有人可以帮忙吗?

StringBuilder message = null;

protected void Page_Load(object sender, EventArgs e)
{
    var dayofweek = System.DateTime.Now.DayOfWeek.ToString();
    String conn = "Data Source=.;Initial Catalog=OCSI;Integrated Security=True";//conne
    SqlConnection sqlconne = new SqlConnection(conn);

    string selectSQL = "SELECT lec FROM schedule WHERE [day]='" + dayofweek +"'";
    SqlCommand cmd = new SqlCommand(selectSQL, sqlconne);
    SqlDataReader reader = null;
    try
    {
        sqlconne.Open();
        reader = cmd.ExecuteReader();
        message = new StringBuider(); 
        while (reader.Read())
        {
            message.Append(reader["lec"].ToString());
           //question what are you using message for ...?
            Label3.Text = dayofweek;
        }
        reader.Close();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Mesage);
    }
    finally
    {
        sqlconne.Close();
        // you neeed to Dispose of all other objects here too
        // StringBuilder Object
        // SqlDataReader Object
        //SqlCommand  Object.. 
        //Look into wrapping your Connection / Command Sql Dataobject around a using() {}
    }

I want to use current day in where clause SQL Query to get data relevant to current day from database table. 我想在where子句SQL Query中使用当前日期从数据库表中获取与当天相关的数据。

I used var as 我用var作为

 protected void Page_Load(object sender, EventArgs e)
 {
    String conn = "Data Source=.;Initial Catalog=OCSI;Integrated Security=True";//conne
    SqlConnection sqlconne = new SqlConnection(conn);

    var testDay = DateTime.Now.DayOfWeek.ToString();

    // string selectSQL = "SELECT lec FROM schedule WHERE [day]='" + dayofweek +"'";
    string selectSQL = string.Format("SELECT lec FROM schedule WHERE [day]= {0}", testDay);
    SqlCommand cmd = new SqlCommand(selectSQL, sqlconne);
    SqlDataReader reader;
    try
    {
        sqlconne.Open();
        reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            message += reader["lec"].ToString();

            Label3.Text = testDay;
        }
        reader.Close();
    }
    catch
    { 
    }
    finally
    {
        sqlconne.Close();
    }

It works fine for me. 这对我来说可以。

在此输入图像描述

Wep page is the value defined as a public , static, or property.. Wep页面是定义为public,static或property的值。

if you write 如果你写

var testDay = DateTime.Now.DayOfWeek.ToString();

the results will = "Friday"; 结果将=“星期五”;

作为整数值(系统将星期日设为0)=

int day = (int)DateTime.Now.DayOfWeek;

Also try changing your sql string to this 还尝试将sql字符串更改为此

string selectSQL = 
   string.Format("SELECT lec FROM schedule WHERE [day]= {0}", dayofweek);

if everything is working fine when you remove the where clause then change the Where clause to be something like this 如果删除where子句后一切正常,那么将Where子句更改为this

   string selectSQL = string.Format("SELECT lec FROM schedule WHERE dbo.schedule.day = {0} ", testDay);

I would also change the name of the field day to something like DayName or something if day is a Reserved word don't use it.. look at this link for Reserved Words in SQL Reserved SQL Words 我还会将字段日的名称更改为DayName之类的东西,如果day是保留字,则不要使用它。请查看此链接以获取SQL 保留SQL字中的保留字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM