form1.cs

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

CS
74
字号
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace CreatingDatabaseSchemaInCode
{
	public partial class Form1
	{
		private SqlCeConnection _conn;

		public Form1()
		{
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;

			this.DbInfoDataGridView.AutoGenerateColumns = true;
		}

		private void InitDatabaseButton_Click(System.Object sender, System.EventArgs e)
		{
			DatabaseUtils dataUtils = new DatabaseUtils();
			dataUtils.InitializeDatabase(Properties.Settings.Default.LocalConnectionString);
			LoadSuppliers();
            this.DbInfoBindingSource.DataSource = dataUtils.GetDbInfo(Properties.Settings.Default.LocalConnectionString);
		}
		
		private void DeleteButton_Click(System.Object sender, System.EventArgs e)
		{
			// we'll use the SqlServerCe connection object to get the database file path
            using (SqlCeConnection localConnection = new SqlCeConnection(Properties.Settings.Default.LocalConnectionString))
			{
				// The SqlCeConnection.Database contains the file parth portion
				// of the database from the full connectionstring
				if (File.Exists(localConnection.Database))
				{
					File.Delete(localConnection.Database);
				}
			}
			
		}
		
		private void LoadSuppliers()
		{
            _conn = new SqlCeConnection(Properties.Settings.Default.LocalConnectionString);
			SqlCeCommand cmd = new SqlCeCommand("Suppliers", _conn);
			cmd.CommandType = CommandType.TableDirect;
			_conn.Open();
			this.SupplierBindingSource.DataSource = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
			this.DataGridView1.DataSource = this.SupplierBindingSource;
			this.DataGridView1.AutoGenerateColumns = true;
		}
		
		private void Form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
		{
			if (_conn != null)
			{
				_conn.Dispose();
			}
		}
		
	}
	
}

⌨️ 快捷键说明

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