📄 mcppbankoperations.cpp
字号:
// This is the main project file for VC++ application project
// generated using an Application Wizard.
// NOTE: This project is to demo the usage of various ADO.net
// classes in a managed C++ extension project
#include "stdafx.h"
// include all the required .NET DLLs
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using "System.Data.dll"
#using "System.Xml.dll"
#include <tchar.h>
// Also specify the required namespaces
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Data;
using namespace System::Data::SqlClient;
using namespace System::Xml;
// Main Class to show the login form
// this is a .net class so it is __gc prefixed
__gc class LoginForm: public Form
{
// all private variables to take care of the form and control display
private:
String *caption; // Caption of the WinForm
int width; // width of the WinForm
int height; // height of the WinForm
TextBox *loginid;
TextBox *pwd;
Label *lglabel;
Label *pwdlabel;
Button *submitbutton;
Button *cancelbutton;
//HACK Made changes here
ErrorProvider *error;
int totallogincount;
public:
LoginForm()
{
caption = "Login form";
width = 280;
height = 180;
LoginControls();
totallogincount= 0;
}
void ~LoginForm()
{
// Form is being destroyed. Do any necessary cleanup here.
}
void LoginControls()
{
// Basic WinForm Settings
Text = caption;
Size = Drawing::Size(width, height);
// Setup Login edit box -- similarly we will password box
loginid = new TextBox();
loginid->Name="loginid";
loginid->Size = Drawing::Size(120, 40);
loginid->TabIndex = 0;
loginid->Location = Drawing::Point(width/2-5, height - 160);
Controls->Add(loginid);
// the following is to create and setup the password editbox
pwd = new TextBox();
pwd->Name="pwd";
pwd->PasswordChar = '*';
pwd->Size = Drawing::Size(120, 40);
pwd->TabIndex = 1;
pwd->Location = Drawing::Point(width/2-5, height - 120);
Controls->Add(pwd);
// to put the labels
lglabel = new Label();
lglabel->Text="Login id";
lglabel->Size = Drawing::Size(90, 20);
lglabel->Location = Drawing::Point(width/2-90, height - 160);
Controls->Add(lglabel);
// to put the labels
pwdlabel = new Label();
pwdlabel->Text="Password";
pwdlabel->Size = Drawing::Size(90, 20);
pwdlabel->Location = Drawing::Point(width/2-90, height - 120);
Controls->Add(pwdlabel);
// The following code creates the buttons
submitbutton = new Button();
submitbutton->Text = "&Submit";
submitbutton->Size = Drawing::Size(90, 30);
submitbutton->TabIndex = 2;
submitbutton->Location = Drawing::Point(width/2-100, height - 70);
submitbutton->Click += (new EventHandler(this, &LoginForm::OnSubmitButtonClick));
Controls->Add(submitbutton);
// The following code creates the cancel button
cancelbutton = new Button();
cancelbutton->Text = "&Cancel";
cancelbutton->Size = Drawing::Size(90, 30);
cancelbutton->TabIndex = 3;
cancelbutton->Location = Drawing::Point(width/2+10, height - 70);
cancelbutton->Click += (new EventHandler(this, &LoginForm::OnCancelButtonClick));
Controls->Add(cancelbutton);
//HACK Made changes here
//create the error provider control
error=new ErrorProvider();
error->BlinkRate=250;
error->BlinkStyle=ErrorBlinkStyle::BlinkIfDifferentError;
}
// This function is called when the submit button is clicked
void OnSubmitButtonClick(Object *sender, EventArgs *e)
{
//HACK Made changes here
if (loginid->Text->get_Length()==0)
{
error->SetError(loginid,"Invalid login name");
return;
}
else
error->SetError(loginid,"");
if (pwd->Text->get_Length()==0)
{
error->SetError(pwd,"Invalid password");
return;
}
else
error->SetError(pwd,"");
// user id and password
String *userId = "sa";
String *password = "";
// Connect to the SQL Database and issue a SELECT command all in one statement
String *query = String::Format(S"SELECT * FROM BankLogin where usernm = '{0}' and pwd='{1}'",loginid->Text,pwd->Text);
// build connect string with the userid and password
String *connectString = String::Format(S"Data Source=localhost;Database=Banking;UID={0};Password={1};", userId, password);
//You can use this format if you are connecting with a known user name and password,
// however it is not recommended to store these in the source for security reasons
// Create the SQL Connection object and pass it the connection string
SqlConnection* sqlconn = new SqlConnection(connectString);
// Open the connection
sqlconn->Open();
// Create the SQL Command to store the select query
SqlCommand *sqlCommand = new SqlCommand(query, sqlconn);
// Create a SqlDataReader to enumerate the result of executing the command against the database.
SqlDataReader *dataReader = sqlCommand->ExecuteReader();
// Always call Read before accessing data.
int numrows = 0;
// Find out if there was a record existing with that user name
// and password - if yes then increase the numrows
while (dataReader->Read())
{
numrows = numrows + 1;
}
// check if any rows were selected
if(numrows == 0)
{
MessageBox::Show("Invalid login name or password. Please try again.");
loginid->Text="";
pwd->Text="";
loginid->Focus();
totallogincount = totallogincount + 1;
if(totallogincount==3)
{
// No row found - hence invalid login
// close the application after 3 tries are done.
MessageBox::Show("You have exhausted your login attempts. The application will now close");
Close();
Application::Exit();
}
}
else
{
dataReader->Close();
sqlconn->Close();
LoginForm::Dispose();
}
}
// closes the application
void OnCancelButtonClick(Object *sender, EventArgs *e)
{
Close();
Application::Exit();
}
};
///////****************************************/////////
// Base class to denote the BankForm - the main application form
__gc class BankForm: public Form
{
// all variable declerations to initialize the various
// controls that are to be displayed on the form
private:
StatusBar *statusBar;
Button *prevbutton;
Button *nextbutton;
Button *firstbutton;
Button *lastbutton;
Button *insertrecord;
Button *updaterecord;
Button *clearfields;
MainMenu *mainMenu;
MenuItem *fileMenu;
MenuItem *recordmenu;
MenuItem *operatemenu;
MenuItem *helpmenu;
LoginForm *lgform;
TextBox *acno;
TextBox *fname;
TextBox *lname;
TextBox *dob;
TextBox *paddr;
TextBox *maddr;
TextBox *phno;
TextBox *emailid;
TextBox *refacno;
TextBox *amount;
TextBox *va;
TextBox *typeac;
Label *heading;
//HACK Made changes here
ErrorProvider *error;
bool flag;
String *caption; // Caption of the WinForm
int width; // width of the WinForm
int height; // height of the WinForm
// a global variable to store the reference to the DataSet
// this data set will be used across the application - once it
// has been populated
DataSet *ds;
// Create a SqlDataAdapter.
SqlDataAdapter* myAdapter;
// variable to store the number of the total records in the dataset at any point
int lastrow;
// a global variable to store which is the current record no.
int currentrec;
public:
BankForm()
{
//HACK Made changes here
//create the error provider control
error=new ErrorProvider();
error->BlinkRate=250;
error->BlinkStyle=ErrorBlinkStyle::BlinkIfDifferentError;
flag=false;
caption = "Bank Operations";
width = 600;
height = 400;
// the following code - calls the login form to be displayed
// in a dialog mode and based on the result of the action
// either exit the applicaiton or continue
lgform = new LoginForm();
if(lgform->ShowDialog(this) == 1)
{
}
lgform->Dispose();
// call the function to create all the controls on the
// bankform
CreateControls();
currentrec = 0;
// Call the function to fetch the data from the database
// put it into a Dataset and also populate the fields with
// the first row that has been fetched
FetchData();
}
void ~BankForm()
{
// Form is being destroyed. Do any necessary cleanup here.
Form::Dispose();
}
/** Function to create the Controls **/
void CreateControls()
{
//create controls here
// FOR EACH CONTROL - the size, location etc. are set
// also specified are the function handlers for buttons
// and menu item controls
// Basic WinForm Settings
Text = caption;
Size = Drawing::Size(width, height);
// Setup Menu
mainMenu = new MainMenu();
fileMenu = new MenuItem("&File");
mainMenu->MenuItems->Add(fileMenu);
fileMenu->MenuItems->Add(new MenuItem("E&xit", new EventHandler(this, &BankForm::OnFileExit)));
// create the records menu
recordmenu = new MenuItem("&Record");
mainMenu->MenuItems->Add(recordmenu);
recordmenu->MenuItems->Add(new MenuItem("&First Record", new EventHandler(this, &BankForm::OnFirstRecord)));
recordmenu->MenuItems->Add(new MenuItem("&Prev Record", new EventHandler(this, &BankForm::OnPrevRecord)));
recordmenu->MenuItems->Add(new MenuItem("&Next Record", new EventHandler(this, &BankForm::OnNextRecord)));
recordmenu->MenuItems->Add(new MenuItem("&Last Record", new EventHandler(this, &BankForm::OnLastRecord)));
// create the Operation menu
operatemenu = new MenuItem("Operate");
mainMenu->MenuItems->Add(operatemenu);
operatemenu->MenuItems->Add(new MenuItem("&Insert New Record", new EventHandler(this, &BankForm::OnInsertRecord)));
operatemenu->MenuItems->Add(new MenuItem("&Update Current Record", new EventHandler(this, &BankForm::OnUpdateRecord)));
Menu = mainMenu;
// Set status bar
statusBar = new StatusBar();
statusBar->Text = "This is the Managed C++ based Bank Application developed by Vineet Arora";
Controls->Add(statusBar);
// Setup first Record Button -- similarly we will create other buttons
firstbutton = new Button();
firstbutton->Text = "&<< First";
firstbutton->Size = Drawing::Size(75, 30);
firstbutton->TabIndex = 0;
firstbutton->Location = Drawing::Point(width/2-180, height - 160);
firstbutton->Click += (new EventHandler(this, &BankForm::OnFirstRecord));
Controls->Add(firstbutton);
// Setup Prev Record Button -- similarly we will create other buttons
prevbutton = new Button();
prevbutton->Text = "&< Prev";
prevbutton->Size = Drawing::Size(75, 30);
prevbutton->TabIndex = 0;
prevbutton->Location = Drawing::Point(width/2-80, height - 160);
prevbutton->Click += (new EventHandler(this, &BankForm::OnPrevRecord));
Controls->Add(prevbutton);
// Setup Next Record Button -- similarly we will create other buttons
nextbutton = new Button();
nextbutton->Text = "& Next >";
nextbutton->Size = Drawing::Size(75, 30);
nextbutton->TabIndex = 0;
nextbutton->Location = Drawing::Point(width/2+20, height - 160);
nextbutton->Click += (new EventHandler(this, &BankForm::OnNextRecord));
Controls->Add(nextbutton);
// Setup last Record Button -- similarly we will create other buttons
lastbutton = new Button();
lastbutton->Text = "&Last >>";
lastbutton->Size = Drawing::Size(75, 30);
lastbutton->TabIndex = 0;
lastbutton->Location = Drawing::Point(width/2+120, height - 160);
lastbutton->Click += (new EventHandler(this, &BankForm::OnLastRecord));
Controls->Add(lastbutton);
// The following various pieces of code create the labels
// and the corresponding edit box for the fields data to
// be displayed on the form
heading = new Label();
heading->Text="First Name";
heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 360);
Controls->Add(heading);
fname = new TextBox();
fname->Name="fname";
fname->Size = Drawing::Size(150, 40);
fname->TabIndex = 2;
fname->Location = Drawing::Point(width/2-120, height - 360);
Controls->Add(fname);
heading = new Label();
heading->Text="Last Name";
heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 340);
Controls->Add(heading);
lname = new TextBox();
lname->Name="lname";
lname->Size = Drawing::Size(150, 20);
lname->TabIndex = 2;
lname->Location = Drawing::Point(width/2-120, height - 340);
Controls->Add(lname);
heading = new Label();
heading->Text="Date of Birth";
heading->Size = Drawing::Size(100, 20);
heading->Location = Drawing::Point(width/2-240, height - 320);
Controls->Add(heading);
dob = new TextBox();
dob->Name="dob";
dob->Size = Drawing::Size(150, 40);
dob->TabIndex = 2;
dob->Location = Drawing::Point(width/2-120, height - 320);
Controls->Add(dob);
heading = new Label();
heading->Text="Permanent Address";
heading->Size = Drawing::Size(120, 20);
heading->Location = Drawing::Point(width/2-240, height - 300);
Controls->Add(heading);
paddr = new TextBox();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -