C# 如果从Sql读取后的maskedTextBox=null

C# 如果从Sql读取后的maskedTextBox=null,c#,sql,sql-server,winforms,C#,Sql,Sql Server,Winforms,您好,我尝试从Sql DB中读取日期,如下所示: SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni); spojeni.Open(); SqlDataReader read= comm.ExecuteReader(); if (read.Read()) { object nulldate =

您好,我尝试从Sql DB中读取日期,如下所示:

SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
spojeni.Open();
SqlDataReader read= comm.ExecuteReader();

if (read.Read())
{
    object nulldate = (maskedTextBox2.Text = read.GetDateTime(24).ToShortDateString());
    if (nulldate == null)
    {
        maskedTextBox2.Text = "__.__.____";
    }

} 
SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
但问题是,当值为null时,我需要让maskedTextBox为空。我总是遇到这样的异常:“不能对空值调用此方法或属性”

当特定列的值被读取为NULL时,如何避免maskedTextBox为空

maskedTextBox上的掩码为00/00/0000

非常感谢您回答我的低质量问题。

像这样的问题应该可以解决问题

maskedTextBox2.Text = read.GetDateTime(24) != null 
    ? precti.GetDateTime(24).ToShortDateString()
    : "__.__.____";
编辑

上述代码不正确,因为如果数据为
DbNull
,则
GetDateTime
不起作用。您有两个选项

1) 使用字段名而不是位置

maskedTextBox2.Text = read["FieldName"] != null 
    ? ((DateTime)read.GetDateTime(24)).ToShortDateString()
    : "__.__.____";
2) 在分配前请求DBNull

maskedTextBox2.Text = !reader.IsDBNull(24)
    ? reader.GetDateTime(24).ToShortDateString()
    : "__.__.____";
像这样的东西应该能奏效

maskedTextBox2.Text = read.GetDateTime(24) != null 
    ? precti.GetDateTime(24).ToShortDateString()
    : "__.__.____";
编辑

上述代码不正确,因为如果数据为
DbNull
,则
GetDateTime
不起作用。您有两个选项

1) 使用字段名而不是位置

maskedTextBox2.Text = read["FieldName"] != null 
    ? ((DateTime)read.GetDateTime(24)).ToShortDateString()
    : "__.__.____";
2) 在分配前请求DBNull

maskedTextBox2.Text = !reader.IsDBNull(24)
    ? reader.GetDateTime(24).ToShortDateString()
    : "__.__.____";

我不确定precti是什么,但我认为这应该是SqlDataReader,对吗

但是,您的阅读器有一个名为
IsDbNull(INDEX)
的方法,您可以在其中检查特定列是否为DbNull

仅供参考: 您不应该像这样构建SQL查询:

SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
spojeni.Open();
SqlDataReader read= comm.ExecuteReader();

if (read.Read())
{
    object nulldate = (maskedTextBox2.Text = read.GetDateTime(24).ToShortDateString());
    if (nulldate == null)
    {
        maskedTextBox2.Text = "__.__.____";
    }

} 
SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
请尝试以下代码:

SqlCommand command = new SqlCommand("SELECT * FROM zajezd WHERE akce=@akce and rocnik=@rocnik", spojeni);
command.Parameters.AddWithValue("@akce", tentoradek);
command.Parameters.AddWithValue("@rocnik", rocnik);

我不确定precti是什么,但我认为这应该是SqlDataReader,对吗

但是,您的阅读器有一个名为
IsDbNull(INDEX)
的方法,您可以在其中检查特定列是否为DbNull

仅供参考: 您不应该像这样构建SQL查询:

SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
spojeni.Open();
SqlDataReader read= comm.ExecuteReader();

if (read.Read())
{
    object nulldate = (maskedTextBox2.Text = read.GetDateTime(24).ToShortDateString());
    if (nulldate == null)
    {
        maskedTextBox2.Text = "__.__.____";
    }

} 
SqlCommand comm= new SqlCommand("SELECT * FROM zajezd WHERE akce='" + tentoradek + "' AND rocnik='" + rocnik + "'", spojeni);
请尝试以下代码:

SqlCommand command = new SqlCommand("SELECT * FROM zajezd WHERE akce=@akce and rocnik=@rocnik", spojeni);
command.Parameters.AddWithValue("@akce", tentoradek);
command.Parameters.AddWithValue("@rocnik", rocnik);
替换

 object nulldate = (maskedTextBox2.Text = read.GetDateTime(24).ToShortDateString());

替换

 object nulldate = (maskedTextBox2.Text = read.GetDateTime(24).ToShortDateString());


您对SQL攻击非常开放将
If read.read()
更改为
While(read.read()){}
也将动态SQL从潜在的SQL注入代码更改为实现参数化查询您对SQL攻击非常开放将
If read.read()
更改为
While(read.read()){}
还将动态sql从潜在的sql注入代码更改为实现参数化查询Hello感谢您的回答,我尝试了,但仍然收到相同的异常。您确定@Claudio的答案是引发该异常的原因吗?我将设置一些断点并进行验证。我猜这是this line对象nulldate=(maskedTextBox2.Text=read.GetDateTime(24.ToSortDateString());如果将Claudio的答案与这一行一起实现,则会引发异常。我建议只删除上面这一行,只使用Claudio的代码。我建议
OP
也使用string.Format或maskedTextBox2.Text.ToString(“mm/dd/yyyy”)类似这样的设置日期格式或使用
Parse
TryParse
函数来确保日期有效,其他选项将取代null来检查DbNUll。您好,谢谢您的回答,我尝试了,但仍然收到相同的异常。您确定@Claudio的答案是我将设置的引发该异常的原因吗一些断点并进行验证。我猜这是一个this line对象nulldate=(maskedTextBox2.Text=read.GetDateTime(24.ToSortDateString());如果将Claudio的答案与这一行一起实现,则会引发异常。我建议只删除上面一行,只使用Claudio的代码。我建议
OP
也使用string.Format或maskedTextBox2.Text.ToString(“mm/dd/yyyy”)类似这样的设置日期格式或使用
Parse
TryParse
函数来确保日期有效,其他选项将代替null来检查DbNUll。