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