#include <msclr\marshal_cppstd.h> // для преобразования String^ в std::string
void TransfeTest()
{
try
{
String^ connectionstring = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection con(connectionstring);
con.Open();
String^ sqlquery = "SELECT * FROM Slot_1";
SqlCommand cmd(sqlquery, % con);
SqlDataReader^ reader = cmd.ExecuteReader();
while (reader->Read())
{
if (!reader->IsDBNull(reader->GetOrdinal("Questions")))
CodeQuestions = Convert::ToInt32(reader["Questions"]);
if (!reader->IsDBNull(reader->GetOrdinal("NameTest")))
CodeNameTest = msclr::interop::marshal_as<std::string>(reader["NameTest"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Question")))
CodeQuestion = msclr::interop::marshal_as<std::string>(reader["Question"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Answer1")))
CodeAnswer1 = msclr::interop::marshal_as<std::string>(reader["Answer1"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Answer2")))
CodeAnswer2 = msclr::interop::marshal_as<std::string>(reader["Answer2"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Answer3")))
CodeAnswer3 = msclr::interop::marshal_as<std::string>(reader["Answer3"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Answer4")))
CodeAnswer4 = msclr::interop::marshal_as<std::string>(reader["Answer4"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("Answer5")))
CodeAnswer5 = msclr::interop::marshal_as<std::string>(reader["Answer5"]->ToString());
if (!reader->IsDBNull(reader->GetOrdinal("CorrectAnswer1")))
CodeCorrectAnswer1 = Convert::ToInt32(reader["CorrectAnswer1"]);
if (!reader->IsDBNull(reader->GetOrdinal("CorrectAnswer2")))
CodeCorrectAnswer2 = Convert::ToInt32(reader["CorrectAnswer2"]);
if (!reader->IsDBNull(reader->GetOrdinal("CorrectAnswer3")))
CodeCorrectAnswer3 = Convert::ToInt32(reader["CorrectAnswer3"]);
if (!reader->IsDBNull(reader->GetOrdinal("CorrectAnswer4")))
CodeCorrectAnswer4 = Convert::ToInt32(reader["CorrectAnswer4"]);
if (!reader->IsDBNull(reader->GetOrdinal("CorrectAnswer5")))
CodeCorrectAnswer5 = Convert::ToInt32(reader["CorrectAnswer5"]);
}
reader->Close();
con.Close();
}
catch (Exception^ ex)
{
MessageBox::Show("Ошибка: " + ex->Message);
throw ex;
}
}
NatsuУченик (234)
1 месяц назад
Появляется ошибка: мя, за которым следует выражение :: должно определять класс или пространство имён.
if (!reader->IsDBNull(reader->GetOrdinal("NameTest")))
CodeNameTest = msclr::interop::marshal_as<std::string>(reader["NameTest"]->ToString());
подчёркнуто msclr
как это можно исправить?
int CodeQuestions;
string CodeNameTest;
string CodeQuestion;
string CodeAnswer1;
string CodeAnswer2;
string CodeAnswer3;
string CodeAnswer4;
string CodeAnswer5;
int CodeCorrectAnswer1;
int CodeCorrectAnswer2;
int CodeCorrectAnswer3;
int CodeCorrectAnswer4;
int CodeCorrectAnswer5;
в .h файле формы такой
#include <string>
using namespace std;
extern int CodeQuestions;
extern string CodeNameTest;
extern string CodeQuestion;
extern string CodeAnswer1;
extern string CodeAnswer2;
extern string CodeAnswer3;
extern string CodeAnswer4;
extern string CodeAnswer5;
extern int CodeCorrectAnswer1;
extern int CodeCorrectAnswer2;
extern int CodeCorrectAnswer3;
extern int CodeCorrectAnswer4;
extern int CodeCorrectAnswer5;
в кнопке есть данная функция
void TransfeTest()
{
try
{
String^ connectionstring = "Data Source=WIN-DFFK8CA8HJQ\\SQLEXPRESS;Initial Catalog=DBConstructorTesting;Persist Security Info=True;Integrated Security=true;";
SqlConnection con(connectionstring);
con.Open ();
String^ sqlquery = "SELECT * FROM Slot_1";
SqlCommand cmd(sqlquery, % con);
SqlDataReader^ reader = cmd.ExecuteReader();
while (reader->Read())
{
if (!reader->IsDBNull(reader->GetOrdinal("Questions")))
CodeQuestions = Convert::ToInt32(reader["Questions"]);
}
reader->Close();
con.Close();
}
catch (Exception^ ex)
{
MessageBox::Show("Ошибка: " + ex->Message);
throw ex;
}
}
нужно как-то перенести в переменные информацию из этих столбцов таблицы
CREATE TABLE [dbo].[Slot_1] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Questions] INT NULL,
[NameTest] NVARCHAR (MAX) NULL,
[Question] NVARCHAR (MAX) NULL,
[Answer1] NVARCHAR (MAX) NULL,
[Answer2] NVARCHAR (MAX) NULL,
[Answer3] NVARCHAR (MAX) NULL,
[Answer4] NVARCHAR (MAX) NULL,
[Answer5] NVARCHAR (MAX) NULL,
[CorrectAnswer1] INT NULL,
[CorrectAnswer2] INT NULL,
[CorrectAnswer3] INT NULL,
[CorrectAnswer4] INT NULL,
[CorrectAnswer5] INT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);