form1.cs

来自「将带您了解SQL Server Compact 3.5的发展历程和功能特性」· CS 代码 · 共 90 行

CS
90
字号
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace SqlCeResultSet
{
	public partial class Form1
	{
		#region Default Instance
		
		private static Form1 defaultInstance;
		
		/// <summary>
		/// Added by the VB.Net to C# Converter to support default instance behavour in C#
		/// </summary>
		public static Form1 Default
		{
			get
			{
				if (defaultInstance == null)
				{
					defaultInstance = new Form1();
					defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
				}
				
				return defaultInstance;
			}
		}
		
		static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
		{
			defaultInstance = null;
		}
		
		#endregion

		// Instance the connection
		private SqlCeConnection _conn;
		public Form1()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			this.EmployeesDataGridView.AutoGenerateColumns = true;
			//TODO:  instance the connection
			_conn = new SqlCeConnection("Data Source = |DataDirectory|\\Northwind.sdf"); //;
		}

		private void LoadButton_Click(System.Object sender, System.EventArgs e)
		{
			// TODO: Check for open connection
			if (_conn.State != ConnectionState.Open)
			{
				_conn.Open();
			}

			// TODO: Create command using TableDirect
			SqlCeCommand cmd = new SqlCeCommand();
			cmd.Connection = _conn;
			cmd.CommandText = "SELECT EmployeeID, LastName, FirstName, Title FROM Employees";
			
			// TODO: Set the bindingSource to the ResultSet
			this.EmployeeBindingSource.DataSource = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
		}
		
		private void UpdateUnderlyingDataButton_Click(System.Object sender, System.EventArgs e)
		{
			SqlCeCommand cmd = new SqlCeCommand();
			cmd.Connection = new SqlCeConnection(" Data Source = |DataDirectory|\\Northwind.sdf");
			cmd.Connection.Open();
			cmd.CommandText = "UPDATE Employees " + " SET LastName = @lastName " + " WHERE  EmployeeID = @employeeId";
			cmd.Parameters.Add("@employeeId", this.EmployeeIdTextBox.Text);
			cmd.Parameters.Add("@lastName", this.EmployeeNameTextBox.Text);
			cmd.ExecuteNonQuery();
			cmd.Connection.Dispose();
		}
		
	}
	
}

⌨️ 快捷键说明

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