sqlParameterCollection use in C++

G

Guest

I am a newby with .Net. I tried the following snippet from David Sceppa’s
MS ADO.NET Core Reference book. I get the following error from the SQL
Server Exception:

"System.Datat.SqlClient.SqlExeception:Line 1:Incorrect syntax near “OrderIdâ€.
Line 1: Incorrect syntax near “?â€.
At System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior ……"

I converted the code snippet from the book from C# to C++. It appears to
not like the “?†placeholders. Does anyone have any insight into this error?
Is it supported in C++?

static System::Data::SqlClient::SqlCommand * CreateInsertCommand()
{
String * strSql;

strSql = S"INSERT INTO [tdforderdetails]"
S" (OrderId, ProductID, Quantity, UnitPrice, Discount)"
S" VALUES( ?, ?, ?, ?, ?)";

SqlCommand * cmd = new SqlCommand(strSql,cn); // cn already established
SqlParameterCollection * pc = cmd->Parameters;
pc->Add(S"OrderId", SqlDbType::Int)->Value = __box(11077);
pc->Add(S"ProductID", SqlDbType::Int)->Value = __box(11);
pc->Add(S"Quantity", SqlDbType::SmallInt)->Value = __box(33);
pc->Add(S"UnitPrice", SqlDbType::Money)->Value = __box(14);
pc->Add(S"Discount", SqlDbType::Money)->Value = __box(1.2);

try
{
cmd->ExecuteNonQuery();
}
catch (System::Data::SqlClient::SqlException *e)
{
MessageBox::Show(e->ToString());
}
return cmd;
}
 
I

Ilya Tumanov [MS]

You should use named parameters with SQL Client:

S"VALUES (@OrderId, ...

You might need to add @ to the name:

pc->Add(S"@OrderId", SqlDbType::Int)->Value = __box(11077);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top