Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
SQL
"CREATE USER john LOGIN PASSWORD 'abc';"

The extended query will be:
SQL
"CREATE USER john LOGIN PASSWORD 'abc' IN ROLE "members" CREATEROLE;"

Here is the C# code I use:
C#
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.
Posted
Comments
camaron182 30-Jul-16 23:11pm    
can you include the SQL command when you provide values for txtCredentials? it could be PostgreSQL syntax
Stelig 31-Jul-16 5:51am    
I am using TextBox[] txtCredentials = {txtUser, txtPw};
and then call the method.

1 solution

When I use the following, it works, but of course many will protest against not using a parameterized query:
C#
public static void CreateUser(NpgsqlConnection pgConnection, string user, string password)
{
    try
    {
        string sql = string.Format("CREATE USER {0} LOGIN PASSWORD '{1}';", user, password);
        pgConnection.Open();

        using (var pgCommand = new NpgsqlCommand())
        {
            pgCommand.Connection = pgConnection;
            pgCommand.CommandText = sql;
            pgCommand.ExecuteNonQuery();
        }
    }
    catch (NpgsqlException pgMsg)
    {
        MessageBox.Show(pgMsg.Message);
    }
}
 
Share this answer
 
v2
Comments
Stelig 31-Jul-16 11:44am    
I have tried the above command but still the same error: 42601: syntax error at or near "$1".

The problem is indeed at password part, where the quotes are needed. But it seems that the parameters are not accepting single quotes. Or at least not that way...
Stelig 1-Aug-16 11:45am    
Thank you Rick,
Your solution works for me aswell.

But as you mentioned, it is not parameterized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900