Anonymous
SQL VS C++ Как сохранить переменную в базе данных, если флажок активен
Сообщение
Anonymous » 20 ноя 2024, 02:11
Я создаю программу. Необходимо сохранить вопросы и ответы в бо. для этого есть код.
Код: Выделить всё
void UpdateTest()
{
try
{
String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection^ con = gcnew SqlConnection(connectionString);
con->Open();
// Правильный синтаксис UPDATE
String^ sqlQuery = "UPDATE Test_slot_1 SET "
"question = @param2, "
"answer1 = @param3, "
"answer2 = @param4, "
"answer3 = @param5, "
"answer4 = @param6 "
"WHERE id = @id";
SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con);
// Добавляем параметры
cmd->Parameters->AddWithValue("@param2", textBox1->Text);
cmd->Parameters->AddWithValue("@param3", textBox2->Text);
cmd->Parameters->AddWithValue("@param4", textBox3->Text);
cmd->Parameters->AddWithValue("@param5", textBox4->Text);
cmd->Parameters->AddWithValue("@param6", textBox5->Text);
cmd->Parameters->AddWithValue("@id", 1);
cmd->ExecuteNonQuery();
con->Close();
num += 1;
label3->Text = num.ToString();
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
textBox5->Text = "";
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
throw ex;
}
}
void InsertTest()
{
try
{
String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection^ con = gcnew SqlConnection(connectionString);
con->Open();
String^ sqlQuery = "INSERT INTO Test_slot_1 (question, answer1, answer2, answer3, answer4) VALUES (@param2, @param3, @param4, @param5, @param6)";
SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con);
cmd->Parameters->AddWithValue("@param2", textBox1->Text);
cmd->Parameters->AddWithValue("@param3", textBox2->Text);
cmd->Parameters->AddWithValue("@param4", textBox3->Text);
cmd->Parameters->AddWithValue("@param5", textBox4->Text);
cmd->Parameters->AddWithValue("@param6", textBox5->Text);
cmd->ExecuteNonQuery();
con->Close();
label3->Text = num.ToString();
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
textBox5->Text = "";
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
throw ex;
}
}
Код: Выделить всё
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
label5->Text = text1.ToString();
if (num == 1 && textBox1->Text != "")
{
UpdateTest();
}
else if (num >= 2 && text1 > num && textBox1->Text != "")
{
if (num == text1 - 1)
{
button1->Text = L"Завершить";
}
num += 1;
InsertTest();
}
else if (text1 == num && textBox1->Text != "")
{
InsertTest();
}
}
Мне также нужно, чтобы переменная сохранялась в базе данных при нажатии флажка.
Код: Выделить всё
int num = 1;
int text2;
int nanswer;
private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e)
{
text2 = label3->Text->Length;
}
private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox1->Checked == true)
{
checkBox2->Checked = false;
checkBox3->Checked = false;
checkBox4->Checked = false;
nanswer = 1;
}
}
private: System::Void checkBox2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox2->Checked == true)
{
checkBox1->Checked = false;
checkBox3->Checked = false;
checkBox4->Checked = false;
nanswer = 2;
}
}
private: System::Void checkBox3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox3->Checked == true)
{
checkBox1->Checked = false;
checkBox2->Checked = false;
checkBox4->Checked = false;
nanswer = 3;
}
}
private: System::Void checkBox4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox4->Checked == true)
{
checkBox1->Checked = false;
checkBox2->Checked = false;
checkBox3->Checked = false;
nanswer = 4;
}
}
Я пробовал вот так
Код: Выделить всё
int num = 1;
int text2;
int nanswer;
private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e)
{
text2 = label3->Text->Length;
}
private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox1->Checked == true)
{
checkBox2->Checked = false;
checkBox3->Checked = false;
checkBox4->Checked = false;
nanswer = 1;
}
}
private: System::Void checkBox2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox2->Checked == true)
{
checkBox1->Checked = false;
checkBox3->Checked = false;
checkBox4->Checked = false;
nanswer = 2;
}
}
private: System::Void checkBox3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox3->Checked == true)
{
checkBox1->Checked = false;
checkBox2->Checked = false;
checkBox4->Checked = false;
nanswer = 3;
}
}
private: System::Void checkBox4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
if (checkBox4->Checked == true)
{
checkBox1->Checked = false;
checkBox2->Checked = false;
checkBox3->Checked = false;
nanswer = 4;
}
}
void UpdateTest()
{
try
{
String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection^ con = gcnew SqlConnection(connectionString);
con->Open();
// Правильный синтаксис UPDATE
String^ sqlQuery = "UPDATE Test_slot_1 SET "
"question = @param2, "
"answer1 = @param3, "
"answer2 = @param4, "
"answer3 = @param5, "
"answer4 = @param6 "
"check1 = @param7 "
"WHERE id = @id";
SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con);
// Добавляем параметры
cmd->Parameters->AddWithValue("@param2", textBox1->Text);
cmd->Parameters->AddWithValue("@param3", textBox2->Text);
cmd->Parameters->AddWithValue("@param4", textBox3->Text);
cmd->Parameters->AddWithValue("@param5", textBox4->Text);
cmd->Parameters->AddWithValue("@param6", textBox5->Text);
cmd->Parameters->AddWithValue("@param7", checkBox1->Checked);
cmd->Parameters->AddWithValue("@id", 1);
cmd->ExecuteNonQuery();
con->Close();
num += 1;
label3->Text = num.ToString();
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
textBox5->Text = "";
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
throw ex;
}
}
void InsertTest()
{
try
{
String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection^ con = gcnew SqlConnection(connectionString);
con->Open();
String^ sqlQuery = "INSERT INTO Test_slot_1 (question, answer1, answer2, answer3, answer4, check1) VALUES (@param2, @param3, @param4, @param5, @param6, @param7)";
SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con);
cmd->Parameters->AddWithValue("@param2", textBox1->Text);
cmd->Parameters->AddWithValue("@param3", textBox2->Text);
cmd->Parameters->AddWithValue("@param4", textBox3->Text);
cmd->Parameters->AddWithValue("@param5", textBox4->Text);
cmd->Parameters->AddWithValue("@param6", textBox5->Text);
cmd->Parameters->AddWithValue("@param7", checkBox1->Checked);
cmd->ExecuteNonQuery();
con->Close();
label3->Text = num.ToString();
textBox1->Text = "";
textBox2->Text = "";
textBox3->Text = "";
textBox4->Text = "";
textBox5->Text = "";
}
catch (Exception^ ex)
{
MessageBox::Show(ex->Message);
throw ex;
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
label5->Text = text1.ToString();
if (num == 1 && textBox1->Text != "")
{
UpdateTest();
}
else if (num >= 2 && text1 > num && textBox1->Text != "")
{
if (num == text1 - 1)
{
button1->Text = L"Завершить";
}
num += 1;
InsertTest();
}
else if (text1 == num && textBox1->Text != "")
{
InsertTest();
}
}
Но не получилось
Подробнее здесь:
https://stackoverflow.com/questions/792 ... ox-is-acti
1732057906
Anonymous
Я создаю программу. Необходимо сохранить вопросы и ответы в бо. для этого есть код. [code] void UpdateTest() { try { String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;"; SqlConnection^ con = gcnew SqlConnection(connectionString); con->Open(); // Правильный синтаксис UPDATE String^ sqlQuery = "UPDATE Test_slot_1 SET " "question = @param2, " "answer1 = @param3, " "answer2 = @param4, " "answer3 = @param5, " "answer4 = @param6 " "WHERE id = @id"; SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con); // Добавляем параметры cmd->Parameters->AddWithValue("@param2", textBox1->Text); cmd->Parameters->AddWithValue("@param3", textBox2->Text); cmd->Parameters->AddWithValue("@param4", textBox3->Text); cmd->Parameters->AddWithValue("@param5", textBox4->Text); cmd->Parameters->AddWithValue("@param6", textBox5->Text); cmd->Parameters->AddWithValue("@id", 1); cmd->ExecuteNonQuery(); con->Close(); num += 1; label3->Text = num.ToString(); textBox1->Text = ""; textBox2->Text = ""; textBox3->Text = ""; textBox4->Text = ""; textBox5->Text = ""; } catch (Exception^ ex) { MessageBox::Show(ex->Message); throw ex; } } void InsertTest() { try { String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;"; SqlConnection^ con = gcnew SqlConnection(connectionString); con->Open(); String^ sqlQuery = "INSERT INTO Test_slot_1 (question, answer1, answer2, answer3, answer4) VALUES (@param2, @param3, @param4, @param5, @param6)"; SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con); cmd->Parameters->AddWithValue("@param2", textBox1->Text); cmd->Parameters->AddWithValue("@param3", textBox2->Text); cmd->Parameters->AddWithValue("@param4", textBox3->Text); cmd->Parameters->AddWithValue("@param5", textBox4->Text); cmd->Parameters->AddWithValue("@param6", textBox5->Text); cmd->ExecuteNonQuery(); con->Close(); label3->Text = num.ToString(); textBox1->Text = ""; textBox2->Text = ""; textBox3->Text = ""; textBox4->Text = ""; textBox5->Text = ""; } catch (Exception^ ex) { MessageBox::Show(ex->Message); throw ex; } } [/code] [code]private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { label5->Text = text1.ToString(); if (num == 1 && textBox1->Text != "") { UpdateTest(); } else if (num >= 2 && text1 > num && textBox1->Text != "") { if (num == text1 - 1) { button1->Text = L"Завершить"; } num += 1; InsertTest(); } else if (text1 == num && textBox1->Text != "") { InsertTest(); } } [/code] Мне также нужно, чтобы переменная сохранялась в базе данных при нажатии флажка. [code] int num = 1; int text2; int nanswer; private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e) { text2 = label3->Text->Length; } private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox1->Checked == true) { checkBox2->Checked = false; checkBox3->Checked = false; checkBox4->Checked = false; nanswer = 1; } } private: System::Void checkBox2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox2->Checked == true) { checkBox1->Checked = false; checkBox3->Checked = false; checkBox4->Checked = false; nanswer = 2; } } private: System::Void checkBox3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox3->Checked == true) { checkBox1->Checked = false; checkBox2->Checked = false; checkBox4->Checked = false; nanswer = 3; } } private: System::Void checkBox4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox4->Checked == true) { checkBox1->Checked = false; checkBox2->Checked = false; checkBox3->Checked = false; nanswer = 4; } } [/code] Я пробовал вот так [code] int num = 1; int text2; int nanswer; private: System::Void label3_Click(System::Object^ sender, System::EventArgs^ e) { text2 = label3->Text->Length; } private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox1->Checked == true) { checkBox2->Checked = false; checkBox3->Checked = false; checkBox4->Checked = false; nanswer = 1; } } private: System::Void checkBox2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox2->Checked == true) { checkBox1->Checked = false; checkBox3->Checked = false; checkBox4->Checked = false; nanswer = 2; } } private: System::Void checkBox3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox3->Checked == true) { checkBox1->Checked = false; checkBox2->Checked = false; checkBox4->Checked = false; nanswer = 3; } } private: System::Void checkBox4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { if (checkBox4->Checked == true) { checkBox1->Checked = false; checkBox2->Checked = false; checkBox3->Checked = false; nanswer = 4; } } void UpdateTest() { try { String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;"; SqlConnection^ con = gcnew SqlConnection(connectionString); con->Open(); // Правильный синтаксис UPDATE String^ sqlQuery = "UPDATE Test_slot_1 SET " "question = @param2, " "answer1 = @param3, " "answer2 = @param4, " "answer3 = @param5, " "answer4 = @param6 " "check1 = @param7 " "WHERE id = @id"; SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con); // Добавляем параметры cmd->Parameters->AddWithValue("@param2", textBox1->Text); cmd->Parameters->AddWithValue("@param3", textBox2->Text); cmd->Parameters->AddWithValue("@param4", textBox3->Text); cmd->Parameters->AddWithValue("@param5", textBox4->Text); cmd->Parameters->AddWithValue("@param6", textBox5->Text); cmd->Parameters->AddWithValue("@param7", checkBox1->Checked); cmd->Parameters->AddWithValue("@id", 1); cmd->ExecuteNonQuery(); con->Close(); num += 1; label3->Text = num.ToString(); textBox1->Text = ""; textBox2->Text = ""; textBox3->Text = ""; textBox4->Text = ""; textBox5->Text = ""; } catch (Exception^ ex) { MessageBox::Show(ex->Message); throw ex; } } void InsertTest() { try { String^ connectionString = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;"; SqlConnection^ con = gcnew SqlConnection(connectionString); con->Open(); String^ sqlQuery = "INSERT INTO Test_slot_1 (question, answer1, answer2, answer3, answer4, check1) VALUES (@param2, @param3, @param4, @param5, @param6, @param7)"; SqlCommand^ cmd = gcnew SqlCommand(sqlQuery, con); cmd->Parameters->AddWithValue("@param2", textBox1->Text); cmd->Parameters->AddWithValue("@param3", textBox2->Text); cmd->Parameters->AddWithValue("@param4", textBox3->Text); cmd->Parameters->AddWithValue("@param5", textBox4->Text); cmd->Parameters->AddWithValue("@param6", textBox5->Text); cmd->Parameters->AddWithValue("@param7", checkBox1->Checked); cmd->ExecuteNonQuery(); con->Close(); label3->Text = num.ToString(); textBox1->Text = ""; textBox2->Text = ""; textBox3->Text = ""; textBox4->Text = ""; textBox5->Text = ""; } catch (Exception^ ex) { MessageBox::Show(ex->Message); throw ex; } } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { label5->Text = text1.ToString(); if (num == 1 && textBox1->Text != "") { UpdateTest(); } else if (num >= 2 && text1 > num && textBox1->Text != "") { if (num == text1 - 1) { button1->Text = L"Завершить"; } num += 1; InsertTest(); } else if (text1 == num && textBox1->Text != "") { InsertTest(); } } [/code] Но не получилось Подробнее здесь: [url]https://stackoverflow.com/questions/79205314/sql-vs-c-how-to-save-a-variable-in-the-database-provided-the-checkbox-is-acti[/url]