Sunday, March 25, 2012

Can't SqlCommand - Help.

Hi all,

I am using VS C++ 2005 and SQL Server 2005 to build a simple application which will work with a database.

I can write the code to open the database which all goes well, but as soon as I write in a 'SqlCommand' and execute it I get an error message telling me that the 'Connection' property hasn't been 'initialised'.

The code I have writted is as follows:

#include "stdafx.h"

using namespace System;

using namespace System::Data;

using namespace System::Data::Sql;

using namespace System::Data::SqlClient;

int main()

{

SqlConnection conn("Data Source=gps028;Initial Catalog=master;Integrated Security=True");

SqlCommand cmd("CREATE DATABASE dbTest");

conn.Open();

cmd.ExecuteNonQuery();

return 0;

}

Where am I going wrong with the code ?

Pete

You need to associate your command and connection. Right now they are unrelated. You can do this either with the

SqlCommand.Connection property, or
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.connection(VS.80).aspx

SqlConnection.CreateCommand() method.
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.createcommand(VS.80).aspx

|||

G'Day Todd,

Thanks for the help on this - all up and running again.

I ended up using the Connection.CreateCommand method.

Cheers

Pete

No comments:

Post a Comment