Hello,
I am trying to create a Win Form in C#, that will be used to add users (
login roles) to my PostgreSQL server.
The SQL query for Postgres is:
"CREATE USER john LOGIN PASSWORD 'abc';"
The extended query will be:
"CREATE USER john LOGIN PASSWORD 'abc' IN ROLE "members" CREATEROLE;"
Here is the C# code I use:
public static void CreateUser(NpgsqlConnection pgConnection, TextBox[] txtCredentials)
{
try
{
if (pgConnection.State != ConnectionState.Open)
pgConnection.Open();
using (var pgCommand = new NpgsqlCommand())
{
pgCommand.Parameters.Clear();
pgCommand.Parameters.AddWithValue("@user", txtCredentials[0].Text);
pgCommand.Parameters.AddWithValue("@pw", txtCredentials[1].Text);
pgCommand.CommandText = "CREATE USER @user LOGIN PASSWORD @pw;";
pgCommand.Connection = pgConnection;
pgCommand.ExecuteNonQuery();
}
}
catch (NpgsqlException pgMsg)
{
MessageBox.Show(pgMsg.Message);
}
}
When it reaches ".ExecuteNonQuery()", it throws an error with the following message:
42601: syntax error at or near "$1"
I have no clue where might be the problem, because the query with parameters in C# looks the same as the one in SQL.
Can you please share an advice?
Thank you
What I have tried:
I have tried to use "CREATE ROLE" instead "CREATE USER", but still the same result.