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

📄 mainform.cs

📁 TREEVIEW AND LISTVIEW IS USER
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;

namespace WinDemoOne
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.Button btnShowSubWindow;
		private System.Windows.Forms.Button btnSqlClient;
		private System.Windows.Forms.Button btnOracleClient;
		private System.Windows.Forms.Button btnDataSet;
		private System.Windows.Forms.Button btnAutoDataSet;
		private System.Windows.Forms.Button btnShowUltraGridForm;
		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuFileExit;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton tbbExit;
		private System.Windows.Forms.ToolBarButton tbbaaa;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel2;
		private System.ComponentModel.IContainer components;

		public MainForm()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.btnShowSubWindow = new System.Windows.Forms.Button();
			this.btnSqlClient = new System.Windows.Forms.Button();
			this.btnOracleClient = new System.Windows.Forms.Button();
			this.btnDataSet = new System.Windows.Forms.Button();
			this.btnAutoDataSet = new System.Windows.Forms.Button();
			this.btnShowUltraGridForm = new System.Windows.Forms.Button();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuFileExit = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.tbbExit = new System.Windows.Forms.ToolBarButton();
			this.tbbaaa = new System.Windows.Forms.ToolBarButton();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
			this.SuspendLayout();
			// 
			// linkLabel1
			// 
			this.linkLabel1.Location = new System.Drawing.Point(136, 88);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.TabIndex = 0;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "www.sina.com.cn";
			// 
			// btnShowSubWindow
			// 
			this.btnShowSubWindow.Location = new System.Drawing.Point(56, 152);
			this.btnShowSubWindow.Name = "btnShowSubWindow";
			this.btnShowSubWindow.Size = new System.Drawing.Size(128, 24);
			this.btnShowSubWindow.TabIndex = 1;
			this.btnShowSubWindow.Text = "Show Sub Window";
			this.btnShowSubWindow.Click += new System.EventHandler(this.btnShowSubWindow_Click);
			// 
			// btnSqlClient
			// 
			this.btnSqlClient.Location = new System.Drawing.Point(272, 88);
			this.btnSqlClient.Name = "btnSqlClient";
			this.btnSqlClient.Size = new System.Drawing.Size(136, 24);
			this.btnSqlClient.TabIndex = 2;
			this.btnSqlClient.Text = "SqlClient";
			this.btnSqlClient.Click += new System.EventHandler(this.btnSqlClient_Click);
			// 
			// btnOracleClient
			// 
			this.btnOracleClient.Location = new System.Drawing.Point(272, 136);
			this.btnOracleClient.Name = "btnOracleClient";
			this.btnOracleClient.Size = new System.Drawing.Size(144, 24);
			this.btnOracleClient.TabIndex = 3;
			this.btnOracleClient.Text = "OracleClient";
			this.btnOracleClient.Click += new System.EventHandler(this.btnOracleClient_Click);
			// 
			// btnDataSet
			// 
			this.btnDataSet.Location = new System.Drawing.Point(272, 176);
			this.btnDataSet.Name = "btnDataSet";
			this.btnDataSet.TabIndex = 4;
			this.btnDataSet.Text = "DataSet";
			this.btnDataSet.Click += new System.EventHandler(this.btnDataSet_Click);
			// 
			// btnAutoDataSet
			// 
			this.btnAutoDataSet.Location = new System.Drawing.Point(272, 216);
			this.btnAutoDataSet.Name = "btnAutoDataSet";
			this.btnAutoDataSet.Size = new System.Drawing.Size(144, 23);
			this.btnAutoDataSet.TabIndex = 5;
			this.btnAutoDataSet.Text = "AutoDataSet";
			this.btnAutoDataSet.Click += new System.EventHandler(this.btnAutoDataSet_Click);
			// 
			// btnShowUltraGridForm
			// 
			this.btnShowUltraGridForm.Location = new System.Drawing.Point(56, 208);
			this.btnShowUltraGridForm.Name = "btnShowUltraGridForm";
			this.btnShowUltraGridForm.Size = new System.Drawing.Size(208, 24);
			this.btnShowUltraGridForm.TabIndex = 6;
			this.btnShowUltraGridForm.Text = "Show UltraGrid Form";
			this.btnShowUltraGridForm.Click += new System.EventHandler(this.btnShowUltraGridForm_Click);
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnuFile});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFileExit,
																					this.menuItem1});
			this.mnuFile.Text = "文件(&F)";
			// 
			// mnuFileExit
			// 
			this.mnuFileExit.Index = 0;
			this.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.F9;
			this.mnuFileExit.Text = "退出";
			this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.Text = "-";
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.tbbExit,
																						this.tbbaaa});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(584, 41);
			this.toolBar1.TabIndex = 8;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// tbbExit
			// 
			this.tbbExit.ImageIndex = 1;
			this.tbbExit.Tag = "Exit";
			this.tbbExit.Text = "退出";
			// 
			// tbbaaa
			// 
			this.tbbaaa.ImageIndex = 0;
			this.tbbaaa.Tag = "aaa";
			this.tbbaaa.Text = "aaa";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 332);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1,
																						  this.statusBarPanel2});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(584, 22);
			this.statusBar1.TabIndex = 9;
			this.statusBar1.Text = "statusBar1";
			// 
			// statusBarPanel1
			// 
			this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.statusBarPanel1.Text = "statusBarPanel1";
			this.statusBarPanel1.ToolTipText = "helloworld";
			this.statusBarPanel1.Width = 107;
			// 
			// statusBarPanel2
			// 
			this.statusBarPanel2.Text = "statusBarPanel2";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(584, 354);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.toolBar1);
			this.Controls.Add(this.btnShowUltraGridForm);
			this.Controls.Add(this.btnAutoDataSet);
			this.Controls.Add(this.btnDataSet);
			this.Controls.Add(this.btnOracleClient);
			this.Controls.Add(this.btnSqlClient);
			this.Controls.Add(this.btnShowSubWindow);
			this.Controls.Add(this.linkLabel1);
			this.IsMdiContainer = true;
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.Text = "MainForm";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.MainForm_Load);
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			mainMenu.MenuItems[0].MenuItems.Add("aaa");

			statusBar1.Panels[0].Text = DateTime.Now.ToShortDateString();
		}

		private void btnShowSubWindow_Click(object sender, System.EventArgs e)
		{
			SubForm subForm = new SubForm();
			subForm.Show();

		}

		private void btnSqlClient_Click(object sender, System.EventArgs e)
		{
			//创建连接
			string connectionString = "server = powersea;uid = sa; pwd = helloworld; database = pubs";
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = connectionString;
			connection.Open();

			//创建命令
			string sql = "select count(*) from authors";
			SqlCommand cmd = new SqlCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = sql;			
			cmd.Connection = connection;
			int count = (int)cmd.ExecuteScalar();


			SqlTransaction tran = connection.BeginTransaction();
			cmd.Transaction = tran;
			try
			{
				sql = "delete from temp";
				cmd.CommandText = sql;
				cmd.ExecuteNonQuery();

				sql = "delete from authorss";
				cmd.CommandText = sql;
				cmd.ExecuteNonQuery();

				tran.Commit();
			}
			catch(Exception ex)
			{
				tran.Rollback();
			}


			connection.Close();
	
		}

		private void btnOracleClient_Click(object sender, System.EventArgs e)
		{
			//创建连接
			string connectionString = "user id=scott;data source=oralsea;password=tiger";
//			OracleConnection conn = new OracleConnection();
//			conn.ConnectionString = connectionString;
//			conn.Open();

			//创建命令
			string sql = "select * from emp";
//			OracleCommand cmd = new OracleCommand(sql, conn);
//			string count = cmd.ExecuteScalar().ToString();

			//创建DataAdapter
			OracleDataAdapter adapter = new OracleDataAdapter(sql, connectionString);
			DataSet ds = new DataSet();
			adapter.Fill(ds);
			
			MessageBox.Show(ds.GetXml());

//			conn.Close();
		}

		private void btnDataSet_Click(object sender, System.EventArgs e)
		{
			DataSet ds = new DataSet("temp");
			
			//构造DataTable
			//列
			DataTable dt1 = new DataTable();
			dt1.Columns.Add("ID", Type.GetType("System.Int32"));
			dt1.Columns.Add("Name", Type.GetType("System.String"));
			//数据
			DataRow dr = null;
			for (int i = 1; i <= 10; i++)
			{
				dr = dt1.NewRow();

				dr["ID"] = i;
				dr["Name"] = "Name" + i;
				dt1.Rows.Add(dr);
			}
			
			ds.Tables.Add(dt1);

			MessageBox.Show(ds.GetXml());


			//访问数据
			DataTable dt2 = ds.Tables[0];
			for (int i = 0; i < dt2.Rows.Count; i++)
			{
				for (int j = 0; j < dt2.Columns.Count; j++)
				{
					Console.WriteLine(dt2.Rows[i][j].ToString());
				}
			}

		}

		private void btnAutoDataSet_Click(object sender, System.EventArgs e)
		{
			//连接
			string connectionString = "server = powersea;uid = sa; pwd = helloworld; database = pubs";
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = connectionString;
			connection.Open();
			//创建Command
			string sql = "select * from authors";
			SqlCommand cmd = new SqlCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = sql;			
			cmd.Connection = connection;
			
			//创建DataAdapter
			SqlDataAdapter adapter = new SqlDataAdapter(cmd);
			DataSet ds = new DataSet();
			adapter.Fill(ds);			

			MessageBox.Show(ds.GetXml());

		}

		private void btnShowUltraGridForm_Click(object sender, System.EventArgs e)
		{
			UltraGridForm form = new UltraGridForm();
			form.Show();
		}

		private void mnuFileExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch(e.Button.Tag.ToString())
			{
				case "Exit" :
					mnuFileExit_Click(sender, (EventArgs)e);
					break;
			}
		}
	}
}

⌨️ 快捷键说明

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