簡體   English   中英

SqlDataAdapter Fill提供NULL主鍵

[英]SqlDataAdapter Fill gives NULL primary keys

因此,我有一些需要幫助的地方。 我們最近將ASP.Net Web表單應用程序從.Net 3.5,Win 2003和SQL 2008遷移到了.Net 4.5,Win 2008和SQL2012。該應用程序在舊平台上運行了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語句的存儲過程,並將結果加載到數據表中。 當我們嘗試遍歷數據表的行並使用鍵進行操作時,會發生錯誤,所有鍵均為空,這將導致無效的強制轉換異常。 數據表在xsd文件中定義,其中包含一個稱為Key的單個整數列。

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

應該注意的是,實際上是設計器生成的代碼在嘗試轉換為強類型的列時引發了異常,這是我們能夠捕獲的異常。

對此進行嘗試,並添加代碼來記錄查詢參數和結果的詳細信息,這表明查詢返回的參數值的行數正確,但是它們都返回空值……(對於以上查詢和表定義)

再加上其他幾點,這是斷斷續續的,它將連續幾天或幾周沒有問題,然后每小時崩潰一次。 當應用程序關閉時,手動執行存儲的proc並檢查導致錯誤的記錄的數據將顯示預期值,而不是應用程序在日志中報告的結果。 重新啟動站點的應用程序池將解決問題並帶我們備份,直到再次開始發生。 流量水平與應用程序關閉的頻率之間似乎存在微小的關聯,但是到目前為止,我們還無法具體隔離。

關於可能的原因或進一步調查的途徑有何建議?


編輯2015-03-16

異常詳細信息:

ExceptionType-ExceptionMessage(鑽探內部異常)

  1. System.Web.HttpUnhandledException-引發了類型為'System.Web.HttpUnhandledException'的異常。
  2. System.Data.StrongTypingException-表'KeyList'中'Key'列的值為DBNull。
  3. System.InvalidCastException-指定的強制轉換無效。

返回的數據:

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

設計器生成的代碼:

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;
        }
    }

}

我們從未隔離出此問題的確切原因,但是我們的服務器團隊將其他一些應用程序移至另一台服務器,並且該服務器再次開始運行。 因此,所有指標都表明我們正在處理某種形式的資源匱乏或類似沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM