简体   繁体   English

SqlDataAdapter Fill提供NULL主键

[英]SqlDataAdapter Fill gives NULL primary keys

So I have a bit of a head scratcher I am looking for help with. 因此,我有一些需要帮助的地方。 We recently moved an ASP.Net web forms app from .Net 3.5, Win 2003, and SQL 2008 to .Net 4.5, Win 2008, and SQL 2012. The app ran for 4+ years on the old platform without any issues. 我们最近将ASP.Net Web表单应用程序从.Net 3.5,Win 2003和SQL 2008迁移到了.Net 4.5,Win 2008和SQL2012。该应用程序在旧平台上运行了4年以上,没有任何问题。 On the new platform we are experiencing an intermittent problem that causes it to crash. 在新平台上,我们遇到一个间歇性问题,导致其崩溃。 We have added error trapping to isolate where exactly the problem is occurring and what we are seeing does not make any sense. 我们添加了错误捕获功能,以隔离出问题出在哪里以及我们看到的内容没有任何意义。

The line throwing the error is in our DAL, where it queries a table to get the list of primary keys related to a specified parent key: 引发错误的行在我们的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)
)

On the .net side we are using the fill method of a SQL data adapter to call a stored procedure containing the above select statement, and load the results into a data table. 在.net端,我们使用SQL数据适配器的fill方法来调用包含上述select语句的存储过程,并将结果加载到数据表中。 The error occurs when we try to loop through the rows of the datatable and do something with the keys, all of the keys are null, which causes an invalid cast exception. 当我们尝试遍历数据表的行并使用键进行操作时,会发生错误,所有键均为空,这将导致无效的强制转换异常。 The data table is defined in an xsd file with a single integer column called Key. 数据表在xsd文件中定义,其中包含一个称为Key的单个整数列。

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

Should note that it is actually the designer generated code which throws the exception while trying to convert to the strongly typed column, this where we are able to catch it. 应该注意的是,实际上是设计器生成的代码在尝试转换为强类型的列时引发了异常,这是我们能够捕获的异常。

Putting a try catch around this and adding code to log the details of the query parameter and results shows that the query is returning the correct number of rows for the parameter value, but they are all coming back null… (which should not be possible with the above query and table definition) 对此进行尝试,并添加代码来记录查询参数和结果的详细信息,这表明查询返回的参数值的行数正确,但是它们都返回空值……(对于以上查询和表定义)

Couple other points, this is intermittent, it will work for days or weeks without an issue, and then crash every hour for a week. 再加上其他几点,这是断断续续的,它将连续几天或几周没有问题,然后每小时崩溃一次。 Manually executing the stored proc and examining the data for the record that is causing the error while the app is down shows the expected values, not what the app is reporting as the results in the logs. 当应用程序关闭时,手动执行存储的proc并检查导致错误的记录的数据将显示预期值,而不是应用程序在日志中报告的结果。 Restarting the application pool for the site will clear the problem and bring us back up, until it starts happening again. 重新启动站点的应用程序池将解决问题并带我们备份,直到再次开始发生。 There seems to be a slight correlation between the traffic levels and how often the app goes down, but nothing we have been able to concretely isolate as of yet. 流量水平与应用程序关闭的频率之间似乎存在微小的关联,但是到目前为止,我们还无法具体隔离。

Any suggestions as to probable cause or further avenues of investigation? 关于可能的原因或进一步调查的途径有何建议?


EDIT 2015-03-16 编辑2015-03-16

Exception Details: 异常详细信息:

ExceptionType - ExceptionMessage (drilling through the inner exceptions) ExceptionType-ExceptionMessage(钻探内部异常)

  1. System.Web.HttpUnhandledException - Exception of type 'System.Web.HttpUnhandledException' was thrown. System.Web.HttpUnhandledException-引发了类型为'System.Web.HttpUnhandledException'的异常。
  2. System.Data.StrongTypingException - The value for column 'Key' in table 'KeyList' is DBNull. System.Data.StrongTypingException-表'KeyList'中'Key'列的值为DBNull。
  3. System.InvalidCastException - Specified cast is not valid. System.InvalidCastException-指定的强制转换无效。

Data being returned: 返回的数据:

data.KeyList.Count: 7; data.KeyList.Count:7;
data.KeyList[0].Key: NULL ; data.KeyList [0] .Key: NULL
data.KeyList[1].Key: NULL ; data.KeyList [1] .Key: NULL
data.KeyList[2].Key: NULL ; data.KeyList [2] .Key: NULL
data.KeyList[3].Key: NULL ; data.KeyList [3] .Key: NULL
data.KeyList[4].Key: NULL ; data.KeyList [4] .Key: NULL
data.KeyList[5].Key: NULL ; data.KeyList [5] .Key: NULL
data.KeyList[6].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;
        }
    }

}

We never did isolate the exact cause of this issue, but our server team moved some other applications to another server and this one started behaving again. 我们从未隔离出此问题的确切原因,但是我们的服务器团队将其他一些应用程序移至另一台服务器,并且该服务器再次开始运行。 So all indicators are that we were dealing with some form of resource starvation or similar conflict. 因此,所有指标都表明我们正在处理某种形式的资源匮乏或类似冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM