⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultsetform.cs

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 CS
字号:
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 ResultSetForm : Form
    {
        public ResultSetForm()
        {
            InitializeComponent();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            DoStuffWithResultSet();
        }

        private const int SSCE_M_KEYDUPLICATE = 25016;

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

            // Create the ResultSets using our own constructor, to override the default behavior
            using (ProductsResultSetResultSets.ProductCategoryResultSet prodCatRS =
                new ProductsResultSetResultSets.ProductCategoryResultSet(false, connectionString))
            {
                using (ProductsResultSetResultSets.ProductResultSet productRS =
                    new ProductsResultSetResultSets.ProductResultSet(false, connectionString))
                {
                    try
                    {
                        // Open the two ResultSets
                        prodCatRS.OpenEx();
                        productRS.OpenEx();

                        // Report count of records to the screen...
                        ReportRecords(prodCatRS.Connection, productRS);

                        // SECOND: Add some product categories
                        // To make sure we don't duplicate names, productCategory.Name has a unique constraint
                        try
                        {
                            prodCatRS.AddProductCategoryRecord("Rock Climbing Equipment");
                            prodCatRS.AddProductCategoryRecord("Scuba Diving Equipment");
                            textBox1.Text += "\r\nProduct Categories added.\r\n";
                        }
                        catch (System.Data.SqlServerCe.SqlCeException sqlEx)
                        {
                            if (sqlEx.NativeError == SSCE_M_KEYDUPLICATE)
                            {
                                // If the categories already exist, just continue
                                textBox1.Text += "\r\nProduct Category addition failed, items already exist.\r\n";
                            }
                            else
                            {
                                throw;
                            }
                        }

                        // THIRD: Add some products in the category with categoryID of 1
                        productRS.AddProductRecord("Contoso Single Rope", "Red/Blue", 155.95M, "60m", 1);
                        productRS.AddProductRecord("Contoso Rock Shoes", "Black", 89.95M, "8", 1);
                        textBox1.Text += "\r\nProducts added.\r\n";

                        // Report count of records to the screen...
                        ReportRecords(prodCatRS.Connection, productRS);
                    }
                    finally
                    {
                        // Close the DataReaders
                        productRS.Close();
                        prodCatRS.Close();
                        // Close and dispose of the database connections
                        productRS.Connection.Close();
                        prodCatRS.Connection.Close();
                    }
                }
            }
        }

        private void ReportRecords(SqlCeConnection conn, ProductsResultSetResultSets.ProductResultSet productRS)
        {
            int categoryCount;
            // One way of counting the records is to ask the database
            using(SqlCeCommand cmd = new SqlCeCommand("SELECT COUNT(*) FROM ProductCategory", conn))
            {
                categoryCount = (int)cmd.ExecuteScalar();
            }
          
            // Alternatively, Get count by casting the ResultSet to IListSource
            int productCount = ((IListSource)productRS).GetList().Count;

            StringBuilder sb = new StringBuilder(textBox1.Text);
            sb.Append("There are currently ");
            sb.Append(categoryCount);
            sb.Append(" product categories.\r\n");
            sb.Append("There are currently ");
            sb.Append(productCount);
            sb.Append(" products.\r\n");
            textBox1.Text = sb.ToString();
        }
    }
}

⌨️ 快捷键说明

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