0
System::String ^name_string;
SqlCeConnection ^sqlConnection = gcnew SqlCeConnection("Data Source=Northwind.sdf");
sqlConnection->Open();

SqlCeCommand comSelect = gcnew SqlCeCommand("SELECT * FROM tablo WHERE id = 33 ", sqlConnection);
SqlCeDataReader ^reader = comSelect->ExecuteReader();  
name_string = reader["name"]->ToString();  

sqlConnection->Close();

What's the mistake on the code above ?
:?:

1
Contributor
2
Replies
3
Views
6 Years
Discussion Span
Last Post by sheennave
0

Dear posters,

It's tricky.. DataReader works fine in the following example :

System::String ^name_string;
SQLiteConnection ^connection = gcnew SQLiteConnection();			  
connection->ConnectionString = "Data Source = database";
connection->Open();
SQLiteCommand^ command = connection->CreateCommand();  
command->CommandText = " Select name from test where id_int = 33 " ;     
SQLiteDataReader ^dataReader1 = command->ExecuteReader();
name_string = dataReader1["name"]->ToString(); 
connection->Close();

HOWEVER it gives error in the following case from the 5th and 6th lines :

System::String ^name_string;
SqlCeConnection ^sqlConnection = gcnew SqlCeConnection("Data Source= database");
sqlConnection->Open();
SqlCeCommand comSelect = gcnew SqlCeCommand("Select name from test where id_int = 33 ", sqlConnection);
SqlCeDataReader ^reader = comSelect->ExecuteReader();  
name_string = reader["name"]->ToString();  
sqlConnection->Close();

Please help.. !!

0

Actually the answer is obvious :

I remember this just few minutes before.. While reading data from a SqlServer database unlike SQLite databases we should use the following structure :

while (reader->Read())
{
   name_string = reader["name"]->ToString();
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.