0

Nothing および/または DbNull.Values を割り当てることができ、Nothing としてインスタンス化することもできるカスタム構造をデカールすることが可能であるかどうか、誰でもアドバイスできますか?

私が探しているのは、データベース クエリから DBNull.Value を受け取り、Nothing として開始できるカスタム DateTime オブジェクトを作成することです。これは可能ですか?

前もって感謝します。

よろしく、デュアン

4

2 に答える 2

2

Nullable<DateTime>(DateTime?略して、またはDate?VB.NET で)のように思えますが、ほぼすべての方法で取得できます。DBNull自分で変換を特別に処理する必要があるだけです。

// You can set a DateTime? to null.
DateTime? d = null;

// You can also set it to a DateTime.
d = DateTime.Now;

// You can check whether it's null in one of two ways:
if (d == null || !d.HasValue) // (These mean the same thing.)
{ }

// Boxing a DateTime? will either result in null or a DateTime value.
SetDatabaseValue(d);

// As for conversions from DBNull, you'll have to deal with that yourself:
object value = GetDatabaseValue();
d = value is DBNull ? null : (DateTime?)value;
于 2010-09-23T07:05:52.280 に答える
0

DataSet を使用している場合は、Field および SetField DataRow 拡張メソッドを使用します。これらにより、DBNull を気にせずに Nullable 型を使用できます。

たとえば、「MyDateField」フィールド (DateTime 型) が null 許容であるとします。次に、次のようなことができます。

foreach (var row in myDataTable)
{
    // will return null if the field is DbNull
    var currentValue = row.Field<DateTime?>("MyDateField");

    // will set the value to DbNull.Value
    row.SetField<DateTime?>("MyDateField", null);
}
于 2010-09-23T07:19:43.980 に答える