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

📄 loginaccess.cs

📁 这是用C#数据库编程的实例
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace databrowser
{
	/// <summary>
	/// LoginAccess 的摘要说明。
	/// </summary>
	public class LoginAccess : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Button cmdok;
		private System.Windows.Forms.Button cmdcancel;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.TextBox txtDatabase;
		private System.Windows.Forms.TextBox txtSQLServer;
		private System.Windows.Forms.Button cmdsever;
		private System.Windows.Forms.Button cmddata;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public LoginAccess(ref String userName,ref String password,
			ref String database,ref String SQLServer,Form frmMain)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//通过窗体参数显示主窗体
			this.ShowDialog(frmMain);
			userName = txtName.Text.ToString().Trim();
			//取得用户名
			password = txtPassword.Text.ToString().Trim();
			//取得密码
			database = txtDatabase.Text.ToString().Trim();
			//取得数据库路径
			SQLServer = txtSQLServer.Text.ToString().Trim();
			//取得SQLServer路径
		}

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

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.txtName = new System.Windows.Forms.TextBox();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.txtDatabase = new System.Windows.Forms.TextBox();
			this.txtSQLServer = new System.Windows.Forms.TextBox();
			this.cmdok = new System.Windows.Forms.Button();
			this.cmdcancel = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.cmdsever = new System.Windows.Forms.Button();
			this.cmddata = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// txtName
			// 
			this.txtName.Location = new System.Drawing.Point(112, 24);
			this.txtName.Name = "txtName";
			this.txtName.TabIndex = 0;
			this.txtName.Text = "Admin";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(112, 48);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.TabIndex = 1;
			this.txtPassword.Text = "";
			// 
			// txtDatabase
			// 
			this.txtDatabase.Location = new System.Drawing.Point(112, 72);
			this.txtDatabase.Name = "txtDatabase";
			this.txtDatabase.TabIndex = 2;
			this.txtDatabase.Text = "";
			// 
			// txtSQLServer
			// 
			this.txtSQLServer.Location = new System.Drawing.Point(112, 104);
			this.txtSQLServer.Name = "txtSQLServer";
			this.txtSQLServer.TabIndex = 3;
			this.txtSQLServer.Text = "";
			// 
			// cmdok
			// 
			this.cmdok.Location = new System.Drawing.Point(88, 144);
			this.cmdok.Name = "cmdok";
			this.cmdok.TabIndex = 4;
			this.cmdok.Text = "确定";
			this.cmdok.Click += new System.EventHandler(this.cmdok_Click);
			// 
			// cmdcancel
			// 
			this.cmdcancel.Location = new System.Drawing.Point(184, 144);
			this.cmdcancel.Name = "cmdcancel";
			this.cmdcancel.TabIndex = 5;
			this.cmdcancel.Text = "取消";
			this.cmdcancel.Click += new System.EventHandler(this.cmdcancel_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 6;
			this.label1.Text = "用户名";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "密码";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 8;
			this.label3.Text = "数据源";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 104);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 9;
			this.label4.Text = "系统数据库";
			// 
			// cmdsever
			// 
			this.cmdsever.Location = new System.Drawing.Point(216, 112);
			this.cmdsever.Name = "cmdsever";
			this.cmdsever.Size = new System.Drawing.Size(24, 23);
			this.cmdsever.TabIndex = 10;
			this.cmdsever.Text = "button3";
			this.cmdsever.Click += new System.EventHandler(this.cmdsever_Click);
			// 
			// cmddata
			// 
			this.cmddata.Location = new System.Drawing.Point(216, 80);
			this.cmddata.Name = "cmddata";
			this.cmddata.Size = new System.Drawing.Size(24, 23);
			this.cmddata.TabIndex = 11;
			this.cmddata.Text = "button4";
			this.cmddata.Click += new System.EventHandler(this.cmddata_Click);
			// 
			// LoginAccess
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 173);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cmddata,
																		  this.cmdsever,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.cmdcancel,
																		  this.cmdok,
																		  this.txtSQLServer,
																		  this.txtDatabase,
																		  this.txtPassword,
																		  this.txtName});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "LoginAccess";
			this.Text = "登录Access数据库";
			this.ResumeLayout(false);

		}
		#endregion

		private void cmdok_Click(object sender, System.EventArgs e)
		{
			//ok按钮的点击事件
			String userName =txtName.Text;
			//取得用户名
			String password =txtPassword.Text;
			//取密码
			String database =txtDatabase.Text;
			//取得数据库路径路径
			String SqlServer=txtSQLServer.Text;
			//取得SQLServer路径
			userName = userName.Trim();
			//删除首尾空格
			password = password.Trim();
			//删除首尾空格
			database = database.Trim();
			//删除首尾空格
			SqlServer = SqlServer.Trim();
			//删除首尾空格
			if(userName.Length != 0 && database.Length != 0 
				&& SqlServer.Length != 0)
			{
				//如果存在用户和服务器
				this.Close();
				//关闭窗体
			}
			else
			{
				MessageBox.Show( "信息填写不完整", "登录错误");
				//否则显示错误信息
			}
		}

		private void cmdcancel_Click(object sender, System.EventArgs e)
		{
			//cancel按钮的点击事件
			txtName.Text = "";
			//清空用户名文本框
			txtPassword.Text = "";
			//清空密码文本框
			txtDatabase.Text = "";
			//清空数据库路径文本框
			txtSQLServer.Text = "";
			//清空服务器文本框
			this.Close();
			//关闭窗口
		}

		private void cmddata_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.Filter = "System Database (*.mdb)|*.mdb";
			//设置读取文件的扩展名
			openFileDialog1.InitialDirectory = "C:\\";
			//设置缺省目录
			DialogResult iResult= openFileDialog1.ShowDialog();
			//取得对话框操作结果
			String fileName="";
			//存放文件名称
			//根据操作结果做出反应
			switch(iResult) {
			case DialogResult.OK:
					//如果现在了ok按钮
					fileName = openFileDialog1.FileName;
					//取得文件名
				break;
			case DialogResult.Cancel:
					//如果选择了cancel按钮
					fileName = "";
					//设置文件名为空
				break;
			default:
					break;
			}
			txtDatabase.Text = fileName.Trim();
			//取得文件名和路径
		}

		private void cmdsever_Click(object sender, System.EventArgs e)
		{
			//按钮button2的敲击事件
			openFileDialog1.InitialDirectory = "C:\\";
			//设置缺省目录
			openFileDialog1.FileName = "";
			//清空文件名
			openFileDialog1.Filter = "System Database (*.mdw)|*.mdw";
			//设置打开文件的扩展名
			openFileDialog1.InitialDirectory =
				"f:\\Program Files\\Microsoft Office\\Office\\";
			//设置缺省目录
			DialogResult iResult =openFileDialog1.ShowDialog();
			//取得对话框返回结果
			String fileName="";
			//定义文件名变量
			switch(iResult) {
			case DialogResult.OK:
					//如果选择了ok按钮
					fileName = openFileDialog1.FileName;
					//取得文件名称
				break;
			case DialogResult.Cancel:
					//如果选择了cancel按钮
					fileName ="";
					//清空文件名称
				break;
			default:
					break;
			}
			txtSQLServer.Text = fileName.Trim();
			//取得服务器名称		
		}

	}
}

⌨️ 快捷键说明

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