gpt4 book ai didi

c# - 在 C# ASP.Net 中查找当前星期几

转载 作者:行者123 更新时间:2023-11-30 14:01:11 26 4
gpt4 key购买 nike

我想在 c# ASP.Net 中找到当前星期几:

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

此代码在控制台应用程序中运行良好,但当我尝试将此行的值存储在 ASP.Net 网站中的字符串中时,它没有返回任何内容。

有人能帮忙吗?

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() {}
}

我想在 where 子句 SQL 查询中使用当天从数据库表中获取与当天相关的数据。

我用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();
}

最佳答案

它对我来说很好。

enter image description here

关于c# - 在 C# ASP.Net 中查找当前星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8856145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com