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

📄 form1.h

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


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

	/// <summary> 
	/// Form1 摘要
	///
	/// 警告: 如果您更改该类的名称,则需要更改 
	///          与该类所依赖的所有 .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("");		//记录用户部门


			 

	public:
		FormLogin(void)
		{
			InitializeComponent();
			//根据用户编号查找用户名
			UserName->SelectCommand->Parameters->Item[0]->Value = TBoxUserID->Text;			
			UserName->Fill(DataSetLogin,S"UserName");
			if(this->DataSetLogin->Tables->Item[S"UserName"]->Rows->Count == 0)
				return;
			this->TBoxUserName->Text = this->DataSetLogin->Tables->Item[S"UserName"]->Rows->Item[0]->Item[S"姓名"]->ToString();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::Label *  label1;

	private: System::Windows::Forms::TextBox *  TBoxUserName;
	private: System::Windows::Forms::Label *  label2;
	private: System::Windows::Forms::TextBox *  TBoxPassword;
	private: System::Windows::Forms::Label *  label3;
	private: System::Windows::Forms::Label *  label4;


	private: System::Data::SqlClient::SqlConnection *  sqlConnection1;

	private: System::Windows::Forms::Button *  BtnLogin;
	private: System::Windows::Forms::Button *  BtnCancel;


	private: System::Data::SqlClient::SqlDataAdapter *  AdaptorLogin;


	private: System::Windows::Forms::TextBox *  TBoxUserID;

	private: System::Data::SqlClient::SqlCommand *  sqlSelectCommand1;
	private: System::Data::DataSet *  DataSetLogin;
	private: System::Data::SqlClient::SqlDataAdapter *  UserName;
	private: System::Data::SqlClient::SqlCommand *  sqlSelectCommand2;

	private:
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		System::ComponentModel::Container * components;

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		void InitializeComponent(void)
		{
			this->TBoxUserID = new System::Windows::Forms::TextBox();
			this->TBoxUserName = new System::Windows::Forms::TextBox();
			this->label2 = new System::Windows::Forms::Label();
			this->TBoxPassword = new System::Windows::Forms::TextBox();
			this->label3 = new System::Windows::Forms::Label();
			this->AdaptorLogin = new System::Data::SqlClient::SqlDataAdapter();
			this->sqlSelectCommand1 = new System::Data::SqlClient::SqlCommand();
			this->sqlConnection1 = new System::Data::SqlClient::SqlConnection();
			this->BtnLogin = new System::Windows::Forms::Button();
			this->BtnCancel = new System::Windows::Forms::Button();
			this->DataSetLogin = new System::Data::DataSet();
			this->label4 = new System::Windows::Forms::Label();
			this->UserName = new System::Data::SqlClient::SqlDataAdapter();
			this->sqlSelectCommand2 = new System::Data::SqlClient::SqlCommand();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->DataSetLogin))->BeginInit();
			this->SuspendLayout();
			// 
			// TBoxUserID
			// 
			this->TBoxUserID->Location = System::Drawing::Point(293, 55);
			this->TBoxUserID->Name = S"TBoxUserID";
			this->TBoxUserID->Size = System::Drawing::Size(162, 31);
			this->TBoxUserID->TabIndex = 1;
			this->TBoxUserID->Text = S"1";
			this->TBoxUserID->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->TBoxUserID->Leave += new System::EventHandler(this, TBoxUserID_Leave);
			// 
			// TBoxUserName
			// 
			this->TBoxUserName->Location = System::Drawing::Point(293, 123);
			this->TBoxUserName->Name = S"TBoxUserName";
			this->TBoxUserName->Size = System::Drawing::Size(162, 31);
			this->TBoxUserName->TabIndex = 3;
			this->TBoxUserName->Text = S"";
			this->TBoxUserName->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(59, 123);
			this->label2->Name = S"label2";
			this->label2->Size = System::Drawing::Size(205, 42);
			this->label2->TabIndex = 2;
			this->label2->Text = S"用户名";
			// 
			// TBoxPassword
			// 
			this->TBoxPassword->Location = System::Drawing::Point(293, 192);
			this->TBoxPassword->Name = S"TBoxPassword";
			this->TBoxPassword->PasswordChar = '*';
			this->TBoxPassword->Size = System::Drawing::Size(162, 31);
			this->TBoxPassword->TabIndex = 5;
			this->TBoxPassword->Text = S"";
			this->TBoxPassword->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			// 
			// 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(59, 192);
			this->label3->Name = S"label3";
			this->label3->Size = System::Drawing::Size(205, 41);
			this->label3->TabIndex = 4;
			this->label3->Text = S"密码";
			// 
			// AdaptorLogin
			// 
			this->AdaptorLogin->SelectCommand = this->sqlSelectCommand1;
			System::Data::Common::DataTableMapping* __mcTemp__1[] = new System::Data::Common::DataTableMapping*[1];
			__mcTemp__1[0] = new System::Data::Common::DataTableMapping(S"Table", S"", new System::Data::Common::DataColumnMapping * [0]);
			this->AdaptorLogin->TableMappings->AddRange(__mcTemp__1);
			// 
			// sqlSelectCommand1
			// 
			this->sqlSelectCommand1->CommandText = S"SELECT 用户编号, 部门, 姓名, 性别, 密码 FROM 用户清单 WHERE (用户编号 = @UserID) AND (姓名 = @UserName)" 
				S" AND (密码 = @Password)";
			this->sqlSelectCommand1->Connection = this->sqlConnection1;
			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"密码"));
			// 
			// sqlConnection1
			// 
			this->sqlConnection1->ConnectionString = S"workstation id=localhost;packet size=4096;integrated security=SSPI;initial catalo" 
				S"g=JxcBook;persist security info=False";
			// 
			// BtnLogin
			// 
			this->BtnLogin->BackColor = System::Drawing::SystemColors::Control;
			this->BtnLogin->ForeColor = System::Drawing::SystemColors::ControlText;
			this->BtnLogin->Location = System::Drawing::Point(88, 274);
			this->BtnLogin->Name = S"BtnLogin";
			this->BtnLogin->Size = System::Drawing::Size(117, 41);
			this->BtnLogin->TabIndex = 6;
			this->BtnLogin->Text = S"登录";
			this->BtnLogin->Click += new System::EventHandler(this, BtnLogin_Click);
			// 
			// 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(308, 274);
			this->BtnCancel->Name = S"BtnCancel";
			this->BtnCancel->Size = System::Drawing::Size(117, 41);
			this->BtnCancel->TabIndex = 7;
			this->BtnCancel->Text = S"退出";
			// 
			// DataSetLogin
			// 
			this->DataSetLogin->DataSetName = S"DataSetLogin";
			this->DataSetLogin->Locale = new System::Globalization::CultureInfo(S"zh-CN");
			// 
			// 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(60, 52);
			this->label4->Name = S"label4";
			this->label4->Size = System::Drawing::Size(205, 42);
			this->label4->TabIndex = 8;
			this->label4->Text = S"用户编号";
			// 
			// UserName
			// 
			this->UserName->SelectCommand = this->sqlSelectCommand2;
			System::Data::Common::DataTableMapping* __mcTemp__2[] = new System::Data::Common::DataTableMapping*[1];
			System::Data::Common::DataColumnMapping* __mcTemp__3[] = new System::Data::Common::DataColumnMapping*[2];
			__mcTemp__3[0] = new System::Data::Common::DataColumnMapping(S"姓名", S"姓名");
			__mcTemp__3[1] = new System::Data::Common::DataColumnMapping(S"用户编号", S"用户编号");
			__mcTemp__2[0] = new System::Data::Common::DataTableMapping(S"Table", S"用户清单", __mcTemp__3);
			this->UserName->TableMappings->AddRange(__mcTemp__2);
			// 
			// sqlSelectCommand2
			// 
			this->sqlSelectCommand2->CommandText = S"SELECT 姓名, 用户编号 FROM 用户清单 WHERE (用户编号 = @ID)";
			this->sqlSelectCommand2->Connection = this->sqlConnection1;
			this->sqlSelectCommand2->Parameters->Add(new System::Data::SqlClient::SqlParameter(S"@ID", System::Data::SqlDbType::VarChar, 6, S"用户编号"));
			// 
			// FormLogin
			// 
			this->AcceptButton = this->BtnLogin;
			this->AutoScaleBaseSize = System::Drawing::Size(11, 24);
			this->CancelButton = this->BtnCancel;
			this->CausesValidation = false;
			this->ClientSize = System::Drawing::Size(514, 327);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->BtnCancel);
			this->Controls->Add(this->BtnLogin);
			this->Controls->Add(this->TBoxPassword);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->TBoxUserName);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->TBoxUserID);
			this->Font = new System::Drawing::Font(S"宋体", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)134);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
			this->Location = System::Drawing::Point(32, 32);
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = S"FormLogin";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			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[S"Users"]->Rows->Item[0]->Item[S"部门"]->ToString();		//保存用户部门

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

private: System::Void TBoxUserID_Leave(System::Object *  sender, System::EventArgs *  e)
		 {
			this->DataSetLogin->Tables->Item[S"UserName"]->Clear();
			//根据用户编号查找用户名
			UserName->SelectCommand->Parameters->Item[0]->Value = TBoxUserID->Text;			
			UserName->Fill(DataSetLogin,S"UserName");
			if(this->DataSetLogin->Tables->Item[S"UserName"]->Rows->Count == 0)
				return;
			this->TBoxUserName->Text = this->DataSetLogin->Tables->Item[S"UserName"]->Rows->Item[0]->Item[S"姓名"]->ToString();
		 }

};
}


⌨️ 快捷键说明

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