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

📄 form1.cs

📁 windows mobile数据库查询应用源码
💻 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 DatabaseViewer
{
	public partial class Form1
	{
		public Form1()
		{
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
		}
		
		#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

		private SqlCeConnection _conn;

		private void OpenButton_Click(System.Object sender, System.EventArgs e)
		{
			if (this.openFileDialog.ShowDialog(this) != DialogResult.Cancel)
			{
				this.TextBox1.Text = this.openFileDialog.FileName;
			}
		}
		
		private void Button1_Click(System.Object sender, System.EventArgs e)
		{
			if (_conn == null)
			{
				_conn = new SqlCeConnection();
			}
			if (_conn.State == ConnectionState.Open)
			{
				_conn.Close();
			}
			_conn.ConnectionString = string.Format("Data Source = {0};password = {1}", this.TextBox1.Text, this.PasswordTextBox.Text);
			try
			{
				_conn.Open();
				SqlCeCommand cmd = new SqlCeCommand();
				cmd.Connection = _conn;
				cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME > \'__%\'";
				this.TablesBindingSource.DataSource = cmd.ExecuteResultSet(System.Data.SqlServerCe.ResultSetOptions.Scrollable);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.ToString());
				this.TablesBindingSource.DataSource = null;
			}
		}
		
		private void Form1_Load(System.Object sender, System.EventArgs e)
		{
			this.DataGridView1.AutoGenerateColumns = true;
			this.TableDataDataGridView.AutoGenerateColumns = true;
			this.UserNameLabel.Text = Environment.UserDomainName + "\\" + Environment.UserName;
		}

		private SqlCeResultSet GetAllRowsForTable(SqlCeConnection sqlceConn, string tableName, bool updateable)
		{
			SqlCeResultSet resultSet = null;
			try
			{
				if (sqlceConn.State != ConnectionState.Open)
				{
					sqlceConn.Open();
				}
				// Since some tables may have spaces in them, we bracket the table name
				// SQL Syntax doesn't support parameterizing the FROM clause, so we'll just glue it together
				// SQL Ev doesn't support multiple commands seperated by ;, nor does it enable any execution functionality
				// so the SQL injection surface area is pretty much nill
				SqlCeCommand cmd = new SqlCeCommand(string.Format("SELECT * FROM [{0}]", tableName), sqlceConn);
				// If the table is a user table, we can make updates
				if (updateable)
				{
					//If GetTableType(tableName, sqlceConn) = TableType.UserTable Then
					resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
				}
				else
				{
					resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
				}
				
			}
			catch (Exception ex)
			{
				if (updateable)
				{
					return GetAllRowsForTable(sqlceConn, tableName, false);
				}
				else
				{
					MessageBox.Show(ex.ToString());
				}
			}
			
			return resultSet;
		}
		
		private void TablesBindingSource_CurrentChanged(System.Object sender, System.EventArgs e)
		{
			if (this.TablesBindingSource.Current != null)
			{
				// Each time the user selects a different table, we load it's data
				RowView rowView =  (RowView)this.TablesBindingSource.Current;
				string tableName =  (string)rowView.UpdatableRecord[0];
				SqlCeResultSet result;
				result = GetAllRowsForTable(_conn, tableName, true);
				if ((result != null)&& result.HasRows)
				{
					this.TableDataBindingSource.DataSource = result;
				}
				else
				{
					this.TableDataBindingSource.DataSource = null;
					this.TablesBindingSource.ResetBindings(true);
				}
			}
		}
	}
	
}

⌨️ 快捷键说明

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