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

📄 formlogin.h

📁 Visual C++.net数据库开发经典案例
💻 H
字号:
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace My
{
	/// <summary> 
	/// FormLogin 摘要
	///
	/// 警告: 如果您更改该类的名称,则将需要更改 
	///          与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的 
	///          “资源文件名”属性。  否则,
	///          设计器将不能与此窗体关联的
	///          本地化资源正确交互。
	/// </summary>
	public __gc class FormLogin : public System::Windows::Forms::Form
	{
	public:
		static String* strUserID = new String("");		//记录用户编号	
		static String* strUser = new String("");			//记录用户名称
		static String* strDepartment = new String("");		//记录用户部门
	private: System::Data::DataSet *  DataSetLogin;
	private: System::Windows::Forms::TextBox *  TBoxUserName;
	private: System::Windows::Forms::TextBox *  TBoxUserID;
	private: System::Windows::Forms::Button *  BtnCancel;
	private: System::Windows::Forms::Label *  label4;
	private: System::Windows::Forms::TextBox *  TBoxPassword;
	private: System::Windows::Forms::Label *  label2;
	private: System::Windows::Forms::Button *  BtnLogin;
	private: System::Windows::Forms::Label *  label3;
	private: System::Data::SqlClient::SqlDataAdapter *  AdaptorLogin;
	private: System::Data::SqlClient::SqlCommand *  sqlSelectCommand1;
	private: System::Data::SqlClient::SqlCommand *  sqlInsertCommand1;
	private: System::Data::SqlClient::SqlConnection *  sqlConnection2;




			

	public: 
		FormLogin(void)
		{
			InitializeComponent();
		}
        
	protected: 
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
        
	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container* components;

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->DataSetLogin = new System::Data::DataSet();
			this->TBoxUserName = new System::Windows::Forms::TextBox();
			this->TBoxUserID = new System::Windows::Forms::TextBox();
			this->BtnCancel = new System::Windows::Forms::Button();
			this->label4 = new System::Windows::Forms::Label();
			this->TBoxPassword = new System::Windows::Forms::TextBox();
			this->label2 = new System::Windows::Forms::Label();
			this->BtnLogin = new System::Windows::Forms::Button();
			this->label3 = new System::Windows::Forms::Label();
			this->AdaptorLogin = new System::Data::SqlClient::SqlDataAdapter();
			this->sqlInsertCommand1 = new System::Data::SqlClient::SqlCommand();
			this->sqlConnection2 = new System::Data::SqlClient::SqlConnection();
			this->sqlSelectCommand1 = new System::Data::SqlClient::SqlCommand();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->DataSetLogin))->BeginInit();
			this->SuspendLayout();
			// 
			// DataSetLogin
			// 
			this->DataSetLogin->DataSetName = S"DataSetLogin";
			this->DataSetLogin->Locale = new System::Globalization::CultureInfo(S"zh-CN");
			// 
			// TBoxUserName
			// 
			this->TBoxUserName->Location = System::Drawing::Point(288, 99);
			this->TBoxUserName->Name = S"TBoxUserName";
			this->TBoxUserName->Size = System::Drawing::Size(162, 21);
			this->TBoxUserName->TabIndex = 19;
			this->TBoxUserName->Text = S"";
			this->TBoxUserName->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// TBoxUserID
			// 
			this->TBoxUserID->Location = System::Drawing::Point(288, 31);
			this->TBoxUserID->Name = S"TBoxUserID";
			this->TBoxUserID->Size = System::Drawing::Size(162, 21);
			this->TBoxUserID->TabIndex = 17;
			this->TBoxUserID->Text = S"";
			this->TBoxUserID->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// BtnCancel
			// 
			this->BtnCancel->BackColor = System::Drawing::SystemColors::Control;
			this->BtnCancel->DialogResult = System::Windows::Forms::DialogResult::Cancel;
			this->BtnCancel->ForeColor = System::Drawing::SystemColors::ControlText;
			this->BtnCancel->Location = System::Drawing::Point(303, 250);
			this->BtnCancel->Name = S"BtnCancel";
			this->BtnCancel->Size = System::Drawing::Size(117, 41);
			this->BtnCancel->TabIndex = 23;
			this->BtnCancel->Text = S"退出";
			// 
			// label4
			// 
			this->label4->Font = new System::Drawing::Font(S"宋体", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)134);
			this->label4->Location = System::Drawing::Point(55, 28);
			this->label4->Name = S"label4";
			this->label4->Size = System::Drawing::Size(205, 42);
			this->label4->TabIndex = 24;
			this->label4->Text = S"用户编号";
			// 
			// TBoxPassword
			// 
			this->TBoxPassword->Location = System::Drawing::Point(288, 168);
			this->TBoxPassword->Name = S"TBoxPassword";
			this->TBoxPassword->PasswordChar = '*';
			this->TBoxPassword->Size = System::Drawing::Size(162, 21);
			this->TBoxPassword->TabIndex = 21;
			this->TBoxPassword->Text = S"";
			this->TBoxPassword->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// label2
			// 
			this->label2->Font = new System::Drawing::Font(S"宋体", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)134);
			this->label2->Location = System::Drawing::Point(54, 99);
			this->label2->Name = S"label2";
			this->label2->Size = System::Drawing::Size(205, 42);
			this->label2->TabIndex = 18;
			this->label2->Text = S"用户名";
			// 
			// BtnLogin
			// 
			this->BtnLogin->BackColor = System::Drawing::SystemColors::Control;
			this->BtnLogin->ForeColor = System::Drawing::SystemColors::ControlText;
			this->BtnLogin->Location = System::Drawing::Point(83, 250);
			this->BtnLogin->Name = S"BtnLogin";
			this->BtnLogin->Size = System::Drawing::Size(117, 41);
			this->BtnLogin->TabIndex = 22;
			this->BtnLogin->Text = S"登录";
			this->BtnLogin->Click += new System::EventHandler(this, BtnLogin_Click);
			// 
			// label3
			// 
			this->label3->Font = new System::Drawing::Font(S"宋体", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)134);
			this->label3->Location = System::Drawing::Point(54, 168);
			this->label3->Name = S"label3";
			this->label3->Size = System::Drawing::Size(205, 41);
			this->label3->TabIndex = 20;
			this->label3->Text = S"密码";
			// 
			// AdaptorLogin
			// 
			this->AdaptorLogin->InsertCommand = this->sqlInsertCommand1;
			this->AdaptorLogin->SelectCommand = this->sqlSelectCommand1;
			System::Data::Common::DataTableMapping* __mcTemp__1[] = new System::Data::Common::DataTableMapping*[1];
			System::Data::Common::DataColumnMapping* __mcTemp__2[] = new System::Data::Common::DataColumnMapping*[5];
			__mcTemp__2[0] = new System::Data::Common::DataColumnMapping(S"用户编号", S"用户编号");
			__mcTemp__2[1] = new System::Data::Common::DataColumnMapping(S"部门", S"部门");
			__mcTemp__2[2] = new System::Data::Common::DataColumnMapping(S"姓名", S"姓名");
			__mcTemp__2[3] = new System::Data::Common::DataColumnMapping(S"性别", S"性别");
			__mcTemp__2[4] = new System::Data::Common::DataColumnMapping(S"密码", S"密码");
			__mcTemp__1[0] = new System::Data::Common::DataTableMapping(S"Table", S"用户清单", __mcTemp__2);
			this->AdaptorLogin->TableMappings->AddRange(__mcTemp__1);
			// 
			// sqlInsertCommand1
			// 
			this->sqlInsertCommand1->CommandText = S"INSERT INTO 用户清单(用户编号, 部门, 姓名, 性别, 密码) VALUES (@用户编号, @部门, @姓名, @性别, @密码); SELECT" 
				S" 用户编号, 部门, 姓名, 性别, 密码 FROM 用户清单";
			this->sqlInsertCommand1->Connection = this->sqlConnection2;
			this->sqlInsertCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@用户编号", System::Data::SqlDbType::VarChar, 6, S"用户编号"));
			this->sqlInsertCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@部门", System::Data::SqlDbType::VarChar, 20, S"部门"));
			this->sqlInsertCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@姓名", System::Data::SqlDbType::VarChar, 10, S"姓名"));
			this->sqlInsertCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@性别", System::Data::SqlDbType::VarChar, 2, S"性别"));
			this->sqlInsertCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@密码", System::Data::SqlDbType::VarChar, 10, S"密码"));
			// 
			// sqlConnection2
			// 
			this->sqlConnection2->ConnectionString = S"workstation id=localhost;packet size=4096;integrated security=SSPI;initial catalo" 
				S"g=hrmbook;persist security info=False";
			// 
			// sqlSelectCommand1
			// 
			this->sqlSelectCommand1->CommandText = S"SELECT 用户编号, 部门, 姓名, 性别, 密码 FROM 用户清单 WHERE (用户编号 = @UserID) AND (姓名 = @UserName)" 
				S" AND (密码 = @Password)";
			this->sqlSelectCommand1->Connection = this->sqlConnection2;
			this->sqlSelectCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@UserID", System::Data::SqlDbType::VarChar, 6, S"用户编号"));
			this->sqlSelectCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@UserName", System::Data::SqlDbType::VarChar, 10, S"姓名"));
			this->sqlSelectCommand1->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@Password", System::Data::SqlDbType::VarChar, 10, S"密码"));
			// 
			// FormLogin
			// 
			this->AcceptButton = this->BtnLogin;
			this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
			this->CancelButton = this->BtnCancel;
			this->ClientSize = System::Drawing::Size(504, 318);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->TBoxPassword);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->BtnLogin);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->TBoxUserName);
			this->Controls->Add(this->TBoxUserID);
			this->Controls->Add(this->BtnCancel);
			this->Name = S"FormLogin";
			this->Text = S"登录";
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->DataSetLogin))->EndInit();
			this->ResumeLayout(false);

		}		
		//---------根据输入的用户ID,名称和密码进行登录验证------------------
	private: System::Void BtnLogin_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 //根据用户名,名称和密码在数据库中进行检索
				 AdaptorLogin->SelectCommand->Parameters->Item[0]->Value = TBoxUserID->Text;
                 AdaptorLogin->SelectCommand->Parameters->Item[1]->Value = TBoxUserName->Text;
				 AdaptorLogin->SelectCommand->Parameters->Item[2]->Value = TBoxPassword->Text;

				 AdaptorLogin->Fill(DataSetLogin,S"Users");

				 				
				 if(DataSetLogin->Tables->Item[0]->Rows->Count > 0)  
				 {
					 //检索的结果不为空则表明登录验证通过
					 MessageBox::Show(this,String::Concat(S"欢迎你,",TBoxUserName->Text),"进入系统");

					 strUserID = TBoxUserID->Text;														//保存用户编号
					 strUser = TBoxUserName->Text;														//保存用户名
					 strDepartment = DataSetLogin->Tables->Item[0]->Columns->Item[1]->ToString();		//保存用户部门

					 //对话窗口返回
					 this->DialogResult = DialogResult::OK;
				 }
				 else
				 {
					 //验证不通过则提示错误
					 MessageBox::Show(this,"用户名或密码错误!","进入系统");
				 }
				 
			 }

};
}

⌨️ 快捷键说明

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