zoukankan      html  css  js  c++  java
  • 09存储过程查询多个结果集

    1 创建Northwind代码文件

    執行:sqlmetal /code:"c:\linqtest7\northwind.cs" /language:csharp "c:\linqtest7\northwnd.mdf" /sprocs /functions /pluralize

    1.2 Northwind 的储存过程

    [Get Customer And Orders]

    image

    1.3 存储过程映射的代码

    下面为工具生成代码。

      /// <summary>
        /// 獲取客戶信息和訂單信息
        /// </summary>
        /// <param name="customerID">客戶ID(輸入)</param>
        /// <returns>
        /// 返回2個結果:客戶信息和訂單信息
        /// SELECT * FROM Customers AS c WHERE c.CustomerID = @CustomerID
        /// SELECT * FROM Orders AS o WHERE o.CustomerID = @CustomerID
        /// </returns>
        /// <remarks>
        /// Function:對應到數據庫的Procedure名字
        /// ResultType:返回結果的類型,必須指定每一個結果的類型
        /// IMultipleResults:表示返回多個結果
        /// </remarks>
        [Function(Name = "dbo.Get Customer And Orders")]
        [ResultType(typeof(GetCustomerAndOrdersResult1))]
        [ResultType(typeof(GetCustomerAndOrdersResult2))]
        public IMultipleResults GetCustomerAndOrders([Parameter(Name = "CustomerID", DbType = "NChar(5)")] string customerID)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);
            return ((IMultipleResults)(result.ReturnValue));
        }

    注意:返回多個結果使用IMultipleResults

    1.4 GetCustomerAndOrdersResult1

    public partial class GetCustomerAndOrdersResult1
    {
    
        private string _CustomerID;
    
        private string _CompanyName;
    
        private string _ContactName;
    
        private string _ContactTitle;
    
        private string _Address;
    
        private string _City;
    
        private string _Region;
    
        private string _PostalCode;
    
        private string _Country;
    
        private string _Phone;
    
        private string _Fax;
    
        public GetCustomerAndOrdersResult1()
        {
        }
    
        [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
        public string CustomerID
        {
            get
            {
                return this._CustomerID;
            }
            set
            {
                if ((this._CustomerID != value))
                {
                    this._CustomerID = value;
                }
            }
        }
    
        [Column(Storage = "_CompanyName", DbType = "NVarChar(40)")]
        public string CompanyName
        {
            get
            {
                return this._CompanyName;
            }
            set
            {
                if ((this._CompanyName != value))
                {
                    this._CompanyName = value;
                }
            }
        }
    
        [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]
        public string ContactName
        {
            get
            {
                return this._ContactName;
            }
            set
            {
                if ((this._ContactName != value))
                {
                    this._ContactName = value;
                }
            }
        }
    
        [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]
        public string ContactTitle
        {
            get
            {
                return this._ContactTitle;
            }
            set
            {
                if ((this._ContactTitle != value))
                {
                    this._ContactTitle = value;
                }
            }
        }
    
        [Column(Storage = "_Address", DbType = "NVarChar(60)")]
        public string Address
        {
            get
            {
                return this._Address;
            }
            set
            {
                if ((this._Address != value))
                {
                    this._Address = value;
                }
            }
        }
    
        [Column(Storage = "_City", DbType = "NVarChar(15)")]
        public string City
        {
            get
            {
                return this._City;
            }
            set
            {
                if ((this._City != value))
                {
                    this._City = value;
                }
            }
        }
    
        [Column(Storage = "_Region", DbType = "NVarChar(15)")]
        public string Region
        {
            get
            {
                return this._Region;
            }
            set
            {
                if ((this._Region != value))
                {
                    this._Region = value;
                }
            }
        }
    
        [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]
        public string PostalCode
        {
            get
            {
                return this._PostalCode;
            }
            set
            {
                if ((this._PostalCode != value))
                {
                    this._PostalCode = value;
                }
            }
        }
    
        [Column(Storage = "_Country", DbType = "NVarChar(15)")]
        public string Country
        {
            get
            {
                return this._Country;
            }
            set
            {
                if ((this._Country != value))
                {
                    this._Country = value;
                }
            }
        }
    
        [Column(Storage = "_Phone", DbType = "NVarChar(24)")]
        public string Phone
        {
            get
            {
                return this._Phone;
            }
            set
            {
                if ((this._Phone != value))
                {
                    this._Phone = value;
                }
            }
        }
    
        [Column(Storage = "_Fax", DbType = "NVarChar(24)")]
        public string Fax
        {
            get
            {
                return this._Fax;
            }
            set
            {
                if ((this._Fax != value))
                {
                    this._Fax = value;
                }
            }
        }
    }

    1.6 GetCustomerAndOrdersResult2

    public partial class GetCustomerAndOrdersResult2
    {
    
        private System.Nullable<int> _OrderID;
    
        private string _CustomerID;
    
        private System.Nullable<int> _EmployeeID;
    
        private System.Nullable<System.DateTime> _OrderDate;
    
        private System.Nullable<System.DateTime> _RequiredDate;
    
        private System.Nullable<System.DateTime> _ShippedDate;
    
        private System.Nullable<int> _ShipVia;
    
        private System.Nullable<decimal> _Freight;
    
        private string _ShipName;
    
        private string _ShipAddress;
    
        private string _ShipCity;
    
        private string _ShipRegion;
    
        private string _ShipPostalCode;
    
        private string _ShipCountry;
    
        public GetCustomerAndOrdersResult2()
        {
        }
    
        [Column(Storage = "_OrderID", DbType = "Int")]
        public System.Nullable<int> OrderID
        {
            get
            {
                return this._OrderID;
            }
            set
            {
                if ((this._OrderID != value))
                {
                    this._OrderID = value;
                }
            }
        }
    
        [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
        public string CustomerID
        {
            get
            {
                return this._CustomerID;
            }
            set
            {
                if ((this._CustomerID != value))
                {
                    this._CustomerID = value;
                }
            }
        }
    
        [Column(Storage = "_EmployeeID", DbType = "Int")]
        public System.Nullable<int> EmployeeID
        {
            get
            {
                return this._EmployeeID;
            }
            set
            {
                if ((this._EmployeeID != value))
                {
                    this._EmployeeID = value;
                }
            }
        }
    
        [Column(Storage = "_OrderDate", DbType = "DateTime")]
        public System.Nullable<System.DateTime> OrderDate
        {
            get
            {
                return this._OrderDate;
            }
            set
            {
                if ((this._OrderDate != value))
                {
                    this._OrderDate = value;
                }
            }
        }
    
        [Column(Storage = "_RequiredDate", DbType = "DateTime")]
        public System.Nullable<System.DateTime> RequiredDate
        {
            get
            {
                return this._RequiredDate;
            }
            set
            {
                if ((this._RequiredDate != value))
                {
                    this._RequiredDate = value;
                }
            }
        }
    
        [Column(Storage = "_ShippedDate", DbType = "DateTime")]
        public System.Nullable<System.DateTime> ShippedDate
        {
            get
            {
                return this._ShippedDate;
            }
            set
            {
                if ((this._ShippedDate != value))
                {
                    this._ShippedDate = value;
                }
            }
        }
    
        [Column(Storage = "_ShipVia", DbType = "Int")]
        public System.Nullable<int> ShipVia
        {
            get
            {
                return this._ShipVia;
            }
            set
            {
                if ((this._ShipVia != value))
                {
                    this._ShipVia = value;
                }
            }
        }
    
        [Column(Storage = "_Freight", DbType = "Money")]
        public System.Nullable<decimal> Freight
        {
            get
            {
                return this._Freight;
            }
            set
            {
                if ((this._Freight != value))
                {
                    this._Freight = value;
                }
            }
        }
    
        [Column(Storage = "_ShipName", DbType = "NVarChar(40)")]
        public string ShipName
        {
            get
            {
                return this._ShipName;
            }
            set
            {
                if ((this._ShipName != value))
                {
                    this._ShipName = value;
                }
            }
        }
    
        [Column(Storage = "_ShipAddress", DbType = "NVarChar(60)")]
        public string ShipAddress
        {
            get
            {
                return this._ShipAddress;
            }
            set
            {
                if ((this._ShipAddress != value))
                {
                    this._ShipAddress = value;
                }
            }
        }
    
        [Column(Storage = "_ShipCity", DbType = "NVarChar(15)")]
        public string ShipCity
        {
            get
            {
                return this._ShipCity;
            }
            set
            {
                if ((this._ShipCity != value))
                {
                    this._ShipCity = value;
                }
            }
        }
    
        [Column(Storage = "_ShipRegion", DbType = "NVarChar(15)")]
        public string ShipRegion
        {
            get
            {
                return this._ShipRegion;
            }
            set
            {
                if ((this._ShipRegion != value))
                {
                    this._ShipRegion = value;
                }
            }
        }
    
        [Column(Storage = "_ShipPostalCode", DbType = "NVarChar(10)")]
        public string ShipPostalCode
        {
            get
            {
                return this._ShipPostalCode;
            }
            set
            {
                if ((this._ShipPostalCode != value))
                {
                    this._ShipPostalCode = value;
                }
            }
        }
    
        [Column(Storage = "_ShipCountry", DbType = "NVarChar(15)")]
        public string ShipCountry
        {
            get
            {
                return this._ShipCountry;
            }
            set
            {
                if ((this._ShipCountry != value))
                {
                    this._ShipCountry = value;
                }
            }
        }
    }

    2 新建項目

    新建Window Form App ,命名為SprocOnlyApp

    3 添加引用和Namespace

    加入 System.Data.Linq.dll

    using System.Data.Linq;

    4 Northwind代码添加到项目

    northwind.cs添加到SprocOnlyApp

    5 设计界面

    在Form上拖一个按钮,用来调用程式。

    6 测试

     private void button5_Click(object sender, EventArgs e)
            {
                Northwnd db = new Northwnd(@"c:\data\northwnd.mdf");
    
                // 聲明變量用來儲存查詢定義
                IMultipleResults result = db.GetCustomerAndOrders("ALFKI");
                
                // ------ 方法一 使用專為結果定義的類------
                // 執行查詢並且輸出結果
                // 客戶
                foreach (GetCustomerAndOrdersResult1 cust in
                    result.GetResult<GetCustomerAndOrdersResult1>())
                {
                    Console.WriteLine(cust.CustomerID);
                }
                // Pause to view company names; press Enter to continue.
                Console.WriteLine(" ------ Orders ------ ");
                // 訂單
                foreach (GetCustomerAndOrdersResult2 ord in
                    result.GetResult<GetCustomerAndOrdersResult2>())
                {
                    Console.WriteLine(ord.OrderID);
                }
    
                // ------ 方法二 使用根據數據庫表生成的類------
                Console.WriteLine(" == 使用根據數據庫表生成的類 == ");
                // 遇到結果是取表的部分有用的欄位,除非是繼承關係,否則不能使用。
                // 不好使用。
                foreach (Customer cust in result.GetResult<Customer>())
                {
                    Console.WriteLine(cust.CustomerID);
                }
                foreach (Order ord in result.GetResult<Order>())
                {
                    Console.WriteLine(ord.OrderID);
                }
            }

    image

    注意IMultipleResults的功能-以变量返回顺序表示已对应函数或查询的结果。

    ISingleResult的功能-表示拥有单一返回顺序之已对应函数的结果。

  • 相关阅读:
    Lock“锁定”语句(C# 参考)
    单件模式(Singleton Pattern)[转]
    mysql中insert into和replace into以及insert ignore用法区别[转]
    CSS优先级、继承
    Silverlight不错的小游戏
    smarty?所有IE都不正常?
    CSS截字对比
    li元素内部浮动后,li的高度不正常
    使IE8强行使用IE7渲染网页的头部代码
    Silverlight?
  • 原文地址:https://www.cnblogs.com/htht66/p/2306827.html
Copyright © 2011-2022 走看看