C# 具有多个条件的语句在c中未返回预期行为#
我在ASP.NETC#和MySQL数据库中工作 在我的数据库表的C# 具有多个条件的语句在c中未返回预期行为#,c#,mysql,asp.net,C#,Mysql,Asp.net,我在ASP.NETC#和MySQL数据库中工作 在我的数据库表的doDate字段中,我可以有三个值: 空的 0000-00-00 2016-03-08 当字段doDate的值为: 0000-00-00 2016-03-08 禁用了文本框txtdoDate,我尝试了以下方法: txtdoDate1 = dr["doDate"] == DBNull.Value ? "" : dr["doDate"] == "0000-00-00" ? "0000-00-00" : Convert.ToDat
doDate
字段中,我可以有三个值:
txtdoDate
,我尝试了以下方法:
txtdoDate1 = dr["doDate"] == DBNull.Value ? "" : dr["doDate"] == "0000-00-00" ? "0000-00-00" : Convert.ToDateTime(dr["doDate"]).ToString("dd/MM/yyyy");
if (txtdoDate1.ToString() != "")
{
txtdoDate.Text = txtdoDate1.ToString();
txtdoDate.Enabled = false;
}
else
{
txtdoDate.Enabled = true;
}
但当字段doDate
的值为:
0000-00-00
文本框已启用且为空
你能帮我解决这个问题吗
提前谢谢
编辑#1
protected void loadsRecord()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
sql = @" SELECT ..... ; ";
using (OdbcCommand command =
new OdbcCommand(sql, cn))
{
try
{
command.Connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
txtdoDate1 = dr["doDate"] == DBNull.Value ? "" : (dr["doDate"] == "0000-00-00" ? "0000-00-00" : Convert.ToDateTime(dr["doDate"]).ToString("dd/MM/yyyy"));
if (txtdoDate1.ToString() != "")
{
txtdoDate.Text = txtdoDate1.ToString();
txtdoDate.Enabled = false;
}
else
{
txtdoDate.Enabled = true;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
command.Connection.Close();
}
}
}
}
像这样使用它:
txtdoDate1 = dr["doDate"] == DBNull.Value ? "" : (dr["doDate"] == "0000-00-00" ? "0000-00-00" : Convert.ToDateTime(dr["doDate"]).ToString("dd/MM/yyyy"));
将第二个子句包含在偏执陈述中,这是正确的行为,因为您使用的是以下
if
语句
if (txtdoDate1.ToString() != "")
{
txtdoDate.Text = txtdoDate1.ToString();
txtdoDate.Enabled = false;
}
else
{
txtdoDate.Enabled = true;
}
当dr[“doDate”]=“0000-00-00”
时,存储在txtdoDate
中的值是“0000-00-00”
而不是txtdodata1.ToString()!=“”
。您必须修改if
语句中的条件,如下所示:
if (txtdoDate1.ToString() != "" && txtdoDate1.ToString() != "0000-00-00")
doDate列的数据类型是什么?@Steve谢谢,doDate列的数据类型是
date
我有点困惑。这在从数据库加载数据时无法工作。值“0000-00-00”不是有效的NET DateTime值,加载此值时应出错。你能展示一下从数据库加载这条记录的代码吗?@Steve:请看我第一个问题中的编辑1。谢谢你,但我的回复中没有更改。