Sql server connection and interogation

Nov 17, 2011 at 4:23pm
Hello,
Is my first time when i use Visual Studio 2010 and I have some difficulties when I try to connect to Sql server and get some data.
I try to translate one VB example into VS C++, but i have some errors.
This is the code for connection to sql databse:

SqlConnection con;
SqlDataAdapter da;
SqlCommand comanda;

try{
con.ConnectionString = "Server=192.168.92.25;Database=northwind;UID=raul;PWD=raul;";

comanda.CommandText = "SELECT * FROM products";
comanda.Connection = con ;
con.Open();
SqlDataReader^ date = comanda.ExecuteReader();

con.Close();
}
catch ( Exception ^Exception )
{

MessageBox::Show( Exception->Message, "EROARE.....Verfica!!!!", MessageBoxButtons::OK, MessageBoxIcon::Error );

} // end catch



and this is the error :
1>c:\vc2011\t7\t7\Form1.h(102): error C2664: 'void System::Data::Common::DbCommand::Connection::set(System::Data::Common::DbConnection ^)' : cannot convert parameter 1 from 'System::Data::SqlClient::SqlConnection' to 'System::Data::Common::DbConnection ^'

How can I wrote this code "comanda.Connection = con ;" to pass this error??

Do you have some exemple in VSC 2010 with sql connection/interrogation ??

Thank you for help!!!
Nov 18, 2011 at 12:46am
change line:

comanda.Connection = con ;


to:

comanda.Connection = %con ;


Notice the [%] symbol before the [con] variable. This is akin to the [&] used in standard/native c++ for returning the address of an object. The [%] symbol in the above code will return the 'address' (reference) to the conn object. The SqlCommand objects Connection attribute (via commanda.Connection) expects a reference to a SqlConnection object instead of an actual SqlConnection object.

Topic archived. No new replies allowed.