Table is nullable DateTime, but DataSet throws an exception?

Próbuję użyć DataSet designer, aby utworzyć datatable z zapytania. Mam to w porządku. Użyte zapytanie zwraca nullable kolumnę datetime z bazy danych. Ale, gdy dotrze do tego kodu:

DataSet1.DataTable1DataTable table = adapter.GetData();

To rzuca Mocnypingexception z:

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public System.DateTime event_start_date {
    get {
        try {
            return ((global::System.DateTime)(this[this.tableDataTable1.event_start_dateColumn]));
        }
        catch (global::System.InvalidCastException e) {
            throw new global::System.Data.StrongTypingException("The value for column \'event_start_date\' in table \'DataTable1\' is DBNull.", e);
        }
    }
    set {
        this[this.tableDataTable1.event_start_dateColumn] = value;
    }
}

Jak użyć designera, aby ta kolumna była Nullable?

 20
Author: Amy, 2009-10-28

8 answers

Wpisane zestawy danych nie obsługują typów nullable. Wspierają one nullable Kolumny .

Typed data set generator tworzy non-nullable właściwości i pokrewne metody do obsługi wartości null. Jeśli utworzysz kolumnę MyDate typu DateTime i AllowDbNull ustawioną na true, podklasa DataRow zaimplementuje nie nullable DateTime właściwość o nazwie MyDate, metodę SetMyDateNull() i metodę IsMyDateNull(). Oznacza to, że jeśli chcesz użyć typu nullable w kodzie, musisz to:

DateTime? myDateTime = myRow.IsMyDateNull() ? null : (DateTime?) row.MyDate;

Chociaż to nie całkowicie nie udaremnia celu używania wpisanych zestawów danych, to naprawdę jest do bani. To frustrujące, że wpisane zestawy danych implementują kolumny nullable w sposób mniej użyteczny niż na przykład metody rozszerzenia System.Data.

Jest szczególnie złe, ponieważ wpisane zbiory danych Czy używają typów nullable w niektórych miejscach - na przykład, metoda Add<TableName>Row() dla tabeli zawierającej opisaną powyżej kolumnę DateTime nullable zajmie DateTime? parametr.

Dawno temu pytałem o ten problem na forach MSDN i ostatecznie kierownik projektu ADO wyjaśnił, że typy nullable zostały zaimplementowane w tym samym czasie, co wpisane zestawy danych, a jego zespół nie miał czasu, aby w pełni zintegrować te dwa do daty wysyłki.NET 2.0. I o ile mi wiadomo, od tego czasu nie dodali nowych funkcji do wpisywanych zestawów danych.

 40
Author: Robert Rossney,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-15 20:47:01

Dzięki To rozwiązało mój podobny problem : Oto kod. W przypadku tego pytania

Isevent_start_date()

Zwróci, czy pole jest równe null.

W moim przypadku: miałem podobny problem i użyłem następującego rozwiązania

            //Table's Name is Efforts,
            //Column's name is Target
            //So the dataset would automatically generate a property called IsTargetNull() which can be used to check nullables
            //Create an Adaptor
            EffortsTableAdapter ad = new EffortsTableAdapter();
            ProjectDashBoard.Db.EffortsDataTable efforts = ad.GetData();
            DataColumn targetColumn = new DataColumn();
            targetColumn = efforts.TargetColumn;

            List<DateTime?> targetTime = new List<DateTime?>();
            foreach (var item in efforts)
            {

                //----------------------------------------------------
                //This is the line that we are discussing about : 
                DateTime? myDateTime = item.IsTargetNull() ? null : (DateTime?)item.Target;
                //----------------------------------------------------

                targetTime.Add(myDateTime);

            }
 1
Author: Rajesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 06:41:41

Wygląda na to, że Designer pomylił Typ bazy danych dla kolumny.

Otwórz xsd Designer, Naciśnij F4, aby otworzyć Properties Window. Wybierz odpowiednią kolumnę i ustaw Nullable (lub coś w tym stylu, nie pamiętam dokładnej nazwy) na true.

 1
Author: Johannes Rudolph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-05 12:44:05

Aby to działało z LINQ musisz przejść do Właściwości tabel w zbiorze danych.xsd. Najpierw sprawdź i upewnij się, że kolumna jest rzeczywiście ustawiona na nullable. Następnie należy spojrzeć na konkretną właściwość" NullValue " dla kolumny. Wartość Null domyślnie jest "Exception", przynajmniej w VS 2012. Ustaw go na Nothing for VB tak, że możesz zrobić "IsNot Nothing" w klauzuli LINQ Where.

 1
Author: Jack D Menendez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-25 20:25:11

Zrobiłem to, aby wstawić wartość NULL w kolumnie DateTime,

Zakładając, że mam kolumnę nullable DateTime w bazie danych, pobrałem dane z bazy danych w obiekcie o nazwie response i chcę wstawić wartość nullable DateTime w kolumnie zbioru danych o nazwie RenewDate:

// create anew row of the same type of your table row
var rw = ds.StudentActionPrintDT.NewStudentActionPrintDTRow();

// check for null value
if(!response.RenewDate.HasValue)
{
  // if null, then the let DataSet to set it null by it's own way 
  rw.SetRenewDateNull();
}
else
{
  // if not null set value to the datetime value
  rw.RenewDate = response.RenewDate.Value;
}
// add the created row to the dateset [DataSetName].[ColumnName].Add[ColumnName]Row([The Created Row]);
ds.StudentActionPrintDT.AddStudentActionPrintDTRow(rw);
 1
Author: fawzy mokhtar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-21 15:41:20

System.Obiekt DateTime nie jest nullable. Aby DateTime nullable zrobić to DateTime? (put a ? po DateTime)

DateTime? nullableDateTime = null;
 -1
Author: Aaron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-28 17:28:04

Używam poniższego kodu do obsługi komórek null w arkuszu Excel, który jest odczytywany do datatable.

if (!reader.IsDBNull(0))                                
{                                    
  row["DateOnCall"] = (DateTime)reader[0];
}
 -1
Author: Tequila,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-02 13:47:08

Wydaje się inaczej z DataTable i mocno wpisane DataTable... Użyj w ten sposób.

DataSet1.DataTable1DataTable table = new DataSet1.DataTable1DataTable();
table.Merge(adapter.GetData().CopyToDataTable());
 -1
Author: Onur Öçalan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-13 13:02:34