数据集字段DBNull -> 整数?

发布于 2024-07-14 12:49:06 字数 786 浏览 8 评论 0原文

SQLServer int 字段。 值有时为空。 DataAdapter 填充数据集 OK,并且可以在 DatagridView 中显示数据 OK。

当尝试以编程方式从数据集中检索数据时,数据集字段检索代码会引发 StronglyTypedException 错误。

 [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public int curr_reading {
        get {
            try {
                return ((int)(this[this.tableHistory.curr_readingColumn]));
            }
            catch (global::System.InvalidCastException e) {
                throw new global::System.Data.StrongTypingException("The value for column \'curr_reading\' in table \'History\' is DBNull.", e);
            }

通过检查 get 访问器中的 DBNull 并返回 null 解决了这个问题,但是...... 当数据集结构被修改(仍在开发)时,我的更改(毫不奇怪)消失了。

处理这种情况的最佳方法是什么? 看来我只能在数据集级别处理它。 是否有某种属性可以告诉自动代码生成器将更改保留在适当的位置?

SQLServer int field. Value sometimes null.
DataAdapter fills dataset OK and can display data in DatagridView OK.

When trying to retrieve the data programmatically from the dataset the Dataset field retrieval code throws a StronglyTypedException error.

 [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public int curr_reading {
        get {
            try {
                return ((int)(this[this.tableHistory.curr_readingColumn]));
            }
            catch (global::System.InvalidCastException e) {
                throw new global::System.Data.StrongTypingException("The value for column \'curr_reading\' in table \'History\' is DBNull.", e);
            }

Got past this by checking for DBNull in the get accessor and returning null but...
When the dataset structure is modified (Still developing) my changes (unsurprisingly) are gone.

What is the best way to handle this situation?
It seems I am stuck with dealing with it at the dataset level.
Is there some sort of attribute that can tell the auto code generator to leave the changes in place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情定在深秋 2024-07-21 12:49:06

在类型化数据集设计器中,有 nullvalue 属性。
默认情况下,其值为抛出异常(因此您生成的代码)
您可以将其设置为所需的默认值..即。 0。
然后它将返回 0 而不是异常。 (生成其他代码)

VS2008:这直接在数据集设计器中工作。

VS2005:它仅适用于设计器中的字符串,但您可以直接编辑 XSD 并设置属性 msprop:nullValue="0"

In the typed dataset designer there is the nullvalue property.
By default its value is throw exception (hence your generated code)
You can set it to the desired default value.. ie. 0.
Then it will return 0 instead of an exception. (other code is generated)

VS2008 :This works directly in the dataset designer.

VS2005 : It only works for strings in the designer but you can directly edit the XSD an set the property msprop:nullValue="0"

过期情话 2024-07-21 12:49:06
  1. 保留自动生成的代码。 没有办法“拦截”它的生成,因此您所做的任何更改都保证迟早会消失。

  2. .NET(至少是.NET 2.0 system.data 位)不会从 DBNull 转换为其他任何内容。 这很糟糕,但您对此无能为力。

  3. 编写一个名为 ToNullable() 或类似的扩展方法:它可以执行以下操作:

public static Nullable<T> ToNullable(this object x){
    if(x == DBNull.Value)
       return default(T); // return null thing
    else
       return (T)x;
}

那么你可以做

int? thing = DataRow["column"].ToNullable<int>();
  1. Leave the auto-generated code alone. There's no way to "intercept" it getting generated so any changes you do make are guaranteed to get blown away sooner or later.

  2. .NET (well at least the .NET 2.0 system.data bits) will not convert from DBNull into anything else. This sucks but you can't do anything about it.

  3. Write an extension method called ToNullable() or similar: it can do this:

.

public static Nullable<T> ToNullable(this object x){
    if(x == DBNull.Value)
       return default(T); // return null thing
    else
       return (T)x;
}

then you can do

int? thing = DataRow["column"].ToNullable<int>();
活雷疯 2024-07-21 12:49:06

数据集将有一个布尔属性来指示 null。

int curr_reading = ( Iscurr_readingColumnNull) ? 
                   <default_value> : row.curr_readingColumn;

The dataset will have a boolean property to indicate null.

int curr_reading = ( Iscurr_readingColumnNull) ? 
                   <default_value> : row.curr_readingColumn;
望喜 2024-07-21 12:49:06

如果没记错的话,您需要将该行标记为正在编辑 - 使用 .BeginEdit() 或类似方法 - 然后进行编辑并保存该行,可能使用 .EndEdit() 或类似方法。 您可能想要稍微阅读一下这些方法(它们可能位于 DataSet、DataTable 或 DataRow 上)——我的记忆有点模糊。

希望这至少有一点帮助。

If memory serves, you need to mark the row as being edited - using .BeginEdit() or similar - then make your edits and save the row, probably using .EndEdit() or similar. You may want to do a little bit of reading into these methods (they may be on the DataSet, DataTable or DataRow) - my memory is a little hazy.

Hope this helps at least a little bit.

千柳 2024-07-21 12:49:06
if(row["curr_reading"] is DBNull){

}else{
    row.curr_reading;
}
if(row["curr_reading"] is DBNull){

}else{
    row.curr_reading;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文