joinqueryresultsetform.cs

来自「Microsoft Mobile Development Handbook的代码」· CS 代码 · 共 59 行

CS
59
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MobileDevelopersHandbook
{
    public partial class JOINQueryResultSetForm : Form
    {
        public JOINQueryResultSetForm()
        {
            InitializeComponent();
        }

        private void JOINQueryResultSetForm_Load(object sender, EventArgs e)
        {
            try
            {
                // Create an instance of the typed SqlCeResultSet
                JoinQueryResultSetResultSets.ProductsCategoryResultSet set =
                    new JoinQueryResultSetResultSets.ProductsCategoryResultSet();
            }
            catch (SqlCeException sqlEx)
            {
                MessageBox.Show("The default Constructor created by the tools does not work with"
                + " a complex query. Message: " + sqlEx.Message + "\r\nPress OK to continue.");
            }

            string connectionString = "Data Source ="
                + (System.IO.Path.GetDirectoryName(System.Reflection.
                Assembly.GetExecutingAssembly().GetName().CodeBase)
                + "\\MyDatabase.sdf\\; Password =\"MobileP@ssw0rd\";");

            // Create an instance using our own constructor - don't open it yet
            JoinQueryResultSetResultSets.ProductsCategoryResultSet rsltSet =
                new JoinQueryResultSetResultSets.ProductsCategoryResultSet(false, connectionString);

            // Define the query
            string query = "SELECT Product.ProductID, Product.Name, Product.Color,"
            + "Product.ListPrice, Product.Size, ProductCategory.Name AS CategoryName "
            + "FROM Product, ProductCategory "
            + "WHERE Product.ProductCategoryID = ProductCategory.ProductCategoryID";

            // Open it, and supply our query
            rsltSet.Open(query);

            // List the results in a ListView
            while (rsltSet.Read())
            {
                // Simple list in the format: <product name> - <product category mame>
                listView1.Items.Add(new ListViewItem(rsltSet.Name + " - " + rsltSet.CategoryName));
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?