Смекни!
smekni.com

Предметная область "тестирование" (стр. 2 из 2)

1.Сообщение о неверном вводе логина или пароля


2.Попытка создания записи главного администратора

В системе запрещено создание дублирующих записей. Запись главного администратора является единственной и не может быть удалена из системы.

Текст программы, с необходимыми комментариями

Реализация входа пользователя в систему

private: System::Void Вход_Click(System::Object^ sender, System::EventArgs^ e) {

if((textBox1->Text!="") && (textBox2->Text!=""))

{

String^ connect="provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection = gcnew OleDbConnection(connect);

OleDbCommand^ myOleDbCommand = myOleDbConnection->CreateCommand();

myOleDbCommand->CommandText =

"Select [id],[lvl]"+

"FROM [auth]"+

"WHERE [login]='"+textBox1->Text+"' AND ([pass]='"+textBox2->Text+"');";

myOleDbConnection->Open();

OleDbDataReader^ myOleDbDataReader = myOleDbCommand->ExecuteReader();

while(myOleDbDataReader->Read())

{

id=Convert::ToInt32(myOleDbDataReader[0]);

lvl=Convert::ToInt32(myOleDbDataReader[1]);

}

myOleDbDataReader->Close();

myOleDbConnection->Close();

}

else

{

this->label3->Visible=true;

}

if(lvl==0)

{

String^ connectionString2 = "provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection2 = gcnew OleDbConnection(connectionString2);

OleDbCommand^ myOleDbCommand2 = myOleDbConnection2->CreateCommand();

myOleDbCommand2->CommandText =

"INSERT INTO [log] "+

"VALUES ("+id+", Now(), 'Входвсистему');";

myOleDbConnection2->Open();

OleDbDataReader^ myOleDbDataReader2 = myOleDbCommand2->ExecuteReader();

myOleDbConnection2->Close();

this->Text = L"РаботасБД";

this->ClientSize = System::Drawing::Size(500, 220);

this->menuStrip1->Visible=true;

this->textBox1->Visible=false;

this->textBox2->Visible=false;

this->label1->Visible=false;

this->label2->Visible=false;

this->Вход->Visible=false;

this->Выход->Visible=false;

this->dataGridView1->Visible=true;

this->dataGridView1->Location = System::Drawing::Point(12,30);

this->listBox1->Visible=true;

this->listBox1->Location=System::Drawing::Point(300,30);

}

if(lvl==1)

{

String^ connectionString2 = "provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection2 = gcnew OleDbConnection(connectionString2);

OleDbCommand^ myOleDbCommand2 = myOleDbConnection2->CreateCommand();

myOleDbCommand2->CommandText =

"INSERT INTO [log] "+

"VALUES ("+id+", Now(), 'Входвсистему');";

myOleDbConnection2->Open();

OleDbDataReader^ myOleDbDataReader2 = myOleDbCommand2->ExecuteReader();

myOleDbConnection2->Close();

this->Text = L"РаботасБД";

this->ClientSize = System::Drawing::Size(500, 220);

this->menuStrip1->Visible=true;

this->пользователяToolStripMenuItem->Visible=false;

this->участникиToolStripMenuItem->Visible=false;

this->логToolStripMenuItem->Visible=false;

this->textBox1->Visible=false;

this->textBox2->Visible=false;

this->label1->Visible=false;

this->label2->Visible=false;

this->Вход->Visible=false;

this->Выход->Visible=false;

this->dataGridView1->Visible=true;

this->dataGridView1->Location = System::Drawing::Point(12,30);

this->dataGridView1->ReadOnly=false;

this->listBox1->Visible=true;

this->listBox1->Location=System::Drawing::Point(300,30);

this->Сохранить->Enabled=true;

}

if(lvl==2)

{

String^ connectionString2 = "provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection2 = gcnew OleDbConnection(connectionString2);

OleDbCommand^ myOleDbCommand2 = myOleDbConnection2->CreateCommand();

myOleDbCommand2->CommandText =

"INSERT INTO [log] "+

"VALUES ("+id+", Now(), 'Входвсистему');";

myOleDbConnection2->Open();

OleDbDataReader^ myOleDbDataReader2 = myOleDbCommand2->ExecuteReader();

myOleDbConnection2->Close();

this->Text = L"РаботасБД";

this->ClientSize = System::Drawing::Size(500, 220);

this->menuStrip1->Visible=true;

this->пользователяToolStripMenuItem->Visible=false;

this->тестВопросToolStripMenuItem->Visible=false;

this->вопросОтветToolStripMenuItem->Visible=false;

this->ответыToolStripMenuItem->Visible=false;

this->участникиToolStripMenuItem->Visible=false;

this->тестыToolStripMenuItem->Visible=false;

this->логToolStripMenuItem->Visible=false;

this->уровеньToolStripMenuItem->Visible=false;

this->textBox1->Visible=false;

this->textBox2->Visible=false;

this->label1->Visible=false;

this->label2->Visible=false;

this->Вход->Visible=false;

this->Выход->Visible=false;

this->dataGridView1->Visible=true;

this->dataGridView1->Location = System::Drawing::Point(12,30);

this->dataGridView1->ReadOnly=true;

this->listBox1->Visible=true;

this->listBox1->Location=System::Drawing::Point(300,30);

this->Сохранить->Enabled=false;

}

}

Участок кода , реализующий один из запросов

if (flag==7)

{

String^ connect="provider=Microsoft.Jet.OLEDB.4.0;"+path_d;

OleDbConnection^ myOleDbConnection = gcnew OleDbConnection(connect);

DataSet = gcnew System::Data::DataSet();

DataSet->CaseSensitive = true;

OleDbCommand^ myOleDbCommand = myOleDbConnection->CreateCommand();

myOleDbCommand->CommandText ="SELECT Answer.grade, Test_Question.ID_test, Test_Question.ID_question, Answer.ID_answer FROM Answer INNER JOIN (Test_Question INNER JOIN Answer_Question ON Test_Question.ID_question = Answer_Question.ID_question) ON Answer.ID_answer = Answer_Question.ID_answer WHERE (((Test_Question.ID_test)='"+this->textBox3->Text+"') AND ((Test_Question.ID_question)=[Answer_Question].[ID_question]) AND ((Answer.ID_answer)=[Answer_Question].[ID_answer]) AND ((Answer.correct)='yes'));";

myOleDbConnection->Open();

myOleDbDataAdapter = gcnew OleDbDataAdapter();

myOleDbDataAdapter->SelectCommand = myOleDbCommand;

myOleDbDataAdapter->Fill(DataSet,"Test");

dataGridView1->DataSource = DataSet ;

dataGridView1->DataMember = "Test";

this->dataGridView1->DataSource = DataSet->Tables["Test"]->DefaultView;

myOleDbConnection->Close();

table="Test";

String^ connectionString2 = "provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection2 = gcnew OleDbConnection(connectionString2);

OleDbCommand^ myOleDbCommand2 = myOleDbConnection2->CreateCommand();

myOleDbCommand2->CommandText =

"INSERT INTO [log] "+

"VALUES ("+id+", Now(), 'Выводоценоктестасуказанием id теста');";

myOleDbConnection2->Open();

OleDbDataReader^ myOleDbDataReader2 = myOleDbCommand2->ExecuteReader();

myOleDbConnection2->Close();

}

Так как изменение данных и создание новой записи происходят средствами компонента datagridview то далее будет показан код, отвечающий за сохранение этих данных (кнопка «сохранить»)

private: System::Void Сохранить_Click(System::Object^ sender, System::EventArgs^ e) {

OleDbCommandBuilder^ myOleDbCommandBuilder=gcnew OleDbCommandBuilder(myOleDbDataAdapter);

myOleDbDataAdapter->Update(DataSet,table);

DataSet->AcceptChanges();

String^ connectionString2 = "provider=Microsoft.Jet.OLEDB.4.0;"+path_a;

OleDbConnection^ myOleDbConnection2 = gcnew OleDbConnection(connectionString2);

OleDbCommand^ myOleDbCommand2 = myOleDbConnection2->CreateCommand();

myOleDbCommand2->CommandText =

"INSERT INTO [log] "+

"VALUES ("+id+", Now(), 'Сохранениеизменений');";

myOleDbConnection2->Open();

OleDbDataReader^ myOleDbDataReader2 = myOleDbCommand2->ExecuteReader();

myOleDbConnection2->Close();

}