📄 form1.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -