SqlDataAdapter Fill给出NULL主键

时间:2015-01-07 20:03:22

标签: asp.net sql-server sqldataadapter

所以我有点头脑,我正在寻求帮助。我们最近将.Net 3.5,Win 2003和SQL 2008的ASP.Net Web表单应用程序移动到.Net 4.5,Win 2008和SQL 2012.该应用程序在旧平台上运行了4年多而没有任何问题。在新平台上,我们遇到了导致其崩溃的间歇性问题。我们添加了错误捕获以隔离问题发生的确切位置以及我们看到的内容没有任何意义。

抛出错误的行在我们的DAL中,在那里它查询表以获取与指定父键相关的主键列表:

Select 
    ItemKey
From Items
Where ItemParentKey = @Key

Create Table Items
(
    ItemKey int identity(1,1) not null
    ,ItemParentKey int
    ,constraint PK_Items primary key clustered (ItemKey)
)

在.net端,我们使用SQL数据适配器的fill方法来调用包含上述select语句的存储过程,并将结果加载到数据表中。当我们尝试遍历数据表的行并使用键执行某些操作时,会发生错误,所有键都为null,这会导致无效的强制转换异常。数据表在xsd文件中定义,该文件具有一个名为Key的整数列。

Adapter.Fill(data);
foreach(KeyListRow Row in data.KeyList)
{
    Item.Children.Add(Row.Key);  //Invalid cast exception
}

应该注意,它实际上是设计器生成的代码,它在尝试转换为强类型列时抛出异常,这是我们能够捕获它的。

尝试捕获并添加代码以记录查询参数和结果的详细信息,显示查询返回参数值的正确行数,但它们都返回null ...(不应该可以使用上面的查询和表定义)

结合其他观点,这是间歇性的,它可以工作数天或数周没有问题,然后每小时崩溃一周。在应用程序关闭时手动执行存储过程并检查导致错误的记录数据显示预期值,而不是应用程序报告为日志中的结果。重新启动站点的应用程序池将清除问题并将我们重新启动,直到它再次开始发生。流量水平与应用程序停机的频率之间似乎存在轻微的相关性,但我们还没有能够具体分离出来。

有关可能原因或进一步调查途径的任何建议?


编辑2015-03-16

异常详细信息:

ExceptionType - ExceptionMessage(钻取内部异常)

  1. System.Web.HttpUnhandledException - 类型' System.Web.HttpUnhandledException'的异常。被扔了。
  2. System.Data.StrongTypingException - 列'键'的值在表格中#KeyList'是DBNull。
  3. System.InvalidCastException - 指定的强制转换无效。
  4. 返回数据:

      

    data.KeyList.Count:7;
      data.KeyList [0] .Key: NULL ;
      data.KeyList [1] .Key: NULL ;
      data.KeyList [2] .Key: NULL ;
      data.KeyList [3] .Key: NULL ;
      data.KeyList [4] .Key: NULL ;
      data.KeyList [5] .Key: NULL ;
      data.KeyList [6] .Key: NULL ;

    Designer Generated Code:

    public partial class dsXXXX : global::System.Data.DataSet
    {
    
        private KeyListDataTable tableKeyList;
    
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        [global::System.ComponentModel.Browsable(false)]
        [global::System.ComponentModel.DesignerSerializationVisibility(global::System.ComponentModel.DesignerSerializationVisibility.Content)]
        public KeyListDataTable KeyList {
            get {
                return this.tableKeyList;
            }
        }
    
        /// <summary>
        ///Represents the strongly named DataTable class.
        ///</summary>
        [global::System.Serializable()]
        [global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")]
        public partial class KeyListDataTable : global::System.Data.TypedTableBase<KeyListRow> {
    
            private global::System.Data.DataColumn columnKey;
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public KeyListDataTable() {
                this.TableName = "KeyList";
                this.BeginInit();
                this.InitClass();
                this.EndInit();
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            internal KeyListDataTable(global::System.Data.DataTable table) {
                this.TableName = table.TableName;
                if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
                    this.CaseSensitive = table.CaseSensitive;
                }
                if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
                    this.Locale = table.Locale;
                }
                if ((table.Namespace != table.DataSet.Namespace)) {
                    this.Namespace = table.Namespace;
                }
                this.Prefix = table.Prefix;
                this.MinimumCapacity = table.MinimumCapacity;
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected KeyListDataTable(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context) : 
                    base(info, context) {
                this.InitVars();
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public global::System.Data.DataColumn KeyColumn {
                get {
                    return this.columnKey;
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            [global::System.ComponentModel.Browsable(false)]
            public int Count {
                get {
                    return this.Rows.Count;
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public KeyListRow this[int index] {
                get {
                    return ((KeyListRow)(this.Rows[index]));
                }
            }
    
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public event KeyListRowChangeEventHandler KeyListRowChanging;
    
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public event KeyListRowChangeEventHandler KeyListRowChanged;
    
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public event KeyListRowChangeEventHandler KeyListRowDeleting;
    
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public event KeyListRowChangeEventHandler KeyListRowDeleted;
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public void AddKeyListRow(KeyListRow row) {
                this.Rows.Add(row);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public KeyListRow AddKeyListRow(int Key) {
                KeyListRow rowKeyListRow = ((KeyListRow)(this.NewRow()));
                object[] columnValuesArray = new object[] {
                        Key};
                rowKeyListRow.ItemArray = columnValuesArray;
                this.Rows.Add(rowKeyListRow);
                return rowKeyListRow;
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public override global::System.Data.DataTable Clone() {
                KeyListDataTable cln = ((KeyListDataTable)(base.Clone()));
                cln.InitVars();
                return cln;
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override global::System.Data.DataTable CreateInstance() {
                return new KeyListDataTable();
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            internal void InitVars() {
                this.columnKey = base.Columns["Key"];
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            private void InitClass() {
                this.columnKey = new global::System.Data.DataColumn("Key", typeof(int), null, global::System.Data.MappingType.Element);
                base.Columns.Add(this.columnKey);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public KeyListRow NewKeyListRow() {
                return ((KeyListRow)(this.NewRow()));
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override global::System.Data.DataRow NewRowFromBuilder(global::System.Data.DataRowBuilder builder) {
                return new KeyListRow(builder);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override global::System.Type GetRowType() {
                return typeof(KeyListRow);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override void OnRowChanged(global::System.Data.DataRowChangeEventArgs e) {
                base.OnRowChanged(e);
                if ((this.KeyListRowChanged != null)) {
                    this.KeyListRowChanged(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override void OnRowChanging(global::System.Data.DataRowChangeEventArgs e) {
                base.OnRowChanging(e);
                if ((this.KeyListRowChanging != null)) {
                    this.KeyListRowChanging(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override void OnRowDeleted(global::System.Data.DataRowChangeEventArgs e) {
                base.OnRowDeleted(e);
                if ((this.KeyListRowDeleted != null)) {
                    this.KeyListRowDeleted(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            protected override void OnRowDeleting(global::System.Data.DataRowChangeEventArgs e) {
                base.OnRowDeleting(e);
                if ((this.KeyListRowDeleting != null)) {
                    this.KeyListRowDeleting(this, new KeyListRowChangeEvent(((KeyListRow)(e.Row)), e.Action));
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public void RemoveKeyListRow(KeyListRow row) {
                this.Rows.Remove(row);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedTableSchema(global::System.Xml.Schema.XmlSchemaSet xs) {
                global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType();
                global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence();
                dsXXXX ds = new dsXXXX();
                global::System.Xml.Schema.XmlSchemaAny any1 = new global::System.Xml.Schema.XmlSchemaAny();
                any1.Namespace = "http://www.w3.org/2001/XMLSchema";
                any1.MinOccurs = new decimal(0);
                any1.MaxOccurs = decimal.MaxValue;
                any1.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
                sequence.Items.Add(any1);
                global::System.Xml.Schema.XmlSchemaAny any2 = new global::System.Xml.Schema.XmlSchemaAny();
                any2.Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1";
                any2.MinOccurs = new decimal(1);
                any2.ProcessContents = global::System.Xml.Schema.XmlSchemaContentProcessing.Lax;
                sequence.Items.Add(any2);
                global::System.Xml.Schema.XmlSchemaAttribute attribute1 = new global::System.Xml.Schema.XmlSchemaAttribute();
                attribute1.Name = "namespace";
                attribute1.FixedValue = ds.Namespace;
                type.Attributes.Add(attribute1);
                global::System.Xml.Schema.XmlSchemaAttribute attribute2 = new global::System.Xml.Schema.XmlSchemaAttribute();
                attribute2.Name = "tableTypeName";
                attribute2.FixedValue = "KeyListDataTable";
                type.Attributes.Add(attribute2);
                type.Particle = sequence;
                global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable();
                if (xs.Contains(dsSchema.TargetNamespace)) {
                    global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream();
                    global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream();
                    try {
                        global::System.Xml.Schema.XmlSchema schema = null;
                        dsSchema.Write(s1);
                        for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext(); ) {
                            schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current));
                            s2.SetLength(0);
                            schema.Write(s2);
                            if ((s1.Length == s2.Length)) {
                                s1.Position = 0;
                                s2.Position = 0;
                                for (; ((s1.Position != s1.Length) 
                                            && (s1.ReadByte() == s2.ReadByte())); ) {
                                    ;
                                }
                                if ((s1.Position == s1.Length)) {
                                    return type;
                                }
                            }
                        }
                    }
                    finally {
                        if ((s1 != null)) {
                            s1.Close();
                        }
                        if ((s2 != null)) {
                            s2.Close();
                        }
                    }
                }
                xs.Add(dsSchema);
                return type;
            }
        }
    
        /// <summary>
        ///Represents strongly named DataRow class.
        ///</summary>
        public partial class KeyListRow : global::System.Data.DataRow {
    
            private KeyListDataTable tableKeyList;
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            internal KeyListRow(global::System.Data.DataRowBuilder rb) : 
                    base(rb) {
                this.tableKeyList = ((KeyListDataTable)(this.Table));
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public int Key {
                get {
                    try {
                        return ((int)(this[this.tableKeyList.KeyColumn]));
                    }
                    catch (global::System.InvalidCastException e) {
    /*****Exception is thrown here*****/
                        throw new global::System.Data.StrongTypingException("The value for column \'Key\' in table \'KeyList\' is DBNull.", e);
                    }
                }
                set {
                    this[this.tableKeyList.KeyColumn] = value;
                }
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public bool IsKeyNull() {
                return this.IsNull(this.tableKeyList.KeyColumn);
            }
    
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
            public void SetKeyNull() {
                this[this.tableKeyList.KeyColumn] = global::System.Convert.DBNull;
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我们从未确定过这个问题的确切原因,但是我们的服务器团队将其他一些应用程序迁移到了另一台服务器,而且这个应用程序再次开始运行。因此,所有指标都是我们处理某种形式的资源匮乏或类似冲突。