SQL command with parameters
Posted: Wed 30 Jul 2008 14:52
Hello,
I have tried to insert a new table into a database using a sql command with parameters. The variables "name" and "owner" are parameters of my function and declared as following outside this function (I am using c++ managed):
First step in my function is to create a new command:
Second step is to prepare the command text:
Third step is the execution:
But I always get an exception:
Why does the command using parameters not work?
with kind regards,
Robert
I have tried to insert a new table into a database using a sql command with parameters. The variables "name" and "owner" are parameters of my function and declared as following outside this function (I am using c++ managed):
Code: Select all
String^ name = "db_30082008__1609";
String^ owner = "postgres";
Code: Select all
PgSqlCommand^ sqlCommand = m_Connection->CreateCommand();
Code: Select all
sqlCommand->CommandText = "CREATE DATABASE :database WITH OWNER = :owner ENCODING = 'UTF8';";
sqlCommand->Parameters->AddWithValue("database", name);
sqlCommand->Parameters->AddWithValue("owner", owner);
Code: Select all
sqlCommand->ExecuteNonQuery();
If I try to set the values as fixed string, it works:CoreLab.PostgreSql.PgSqlException: syntax error at or near "$1"
bei CoreLab.PostgreSql.a.d(Boolean A_0)
bei CoreLab.PostgreSql.a.ae()
bei CoreLab.PostgreSql.a.b(String A_0)
bei CoreLab.PostgreSql.a.l()
bei CoreLab.PostgreSql.PgSqlCommand.a(Boolean A_0, Int32 A_1, Int32 A_2)
bei CoreLab.Common.DbCommandBase.ExecuteDbDataReader(CommandBehavior behavior)
bei System.Data.Common.DbCommand.ExecuteReader()
bei CoreLab.Common.DbCommandBase.ExecuteNonQuery()
Code: Select all
sqlCommand->CommandText = "CREATE DATABASE " + name + " WITH OWNER = " + owner + " ENCODING = 'UTF8';";
with kind regards,
Robert