i want to restrict the user to make only select command in mysql , no delete or update or insert

51 views Asked by At
                    MySqlConnection connection = new MySqlConnection(strConnection);
                    MySqlCommand command = connection.CreateCommand();

                    MySqlDataReader reader;
                    command.CommandText = pQuery;
                    connection.Open();
                    reader = command.ExecuteReader();
                    var dataTable = new DataTable();
                    dataTable.Load(reader);

                    connection.Close();

I took the query from text box , I don't want user to delete or change the data in database using text box

how to restrict the command to only select command

thank you

1

There are 1 answers

3
Omer Gafar On

you can create user in your database who have only the select permission , and connect to the database using that user. Or you can check the user input if its contains "delete" keyword using Contains Method

if( pQuery.ToUpper.Contains("DELETE") or pQuery.ToUpper.Contains("UPDATE"))
{
// do something
}
else{
                MySqlConnection connection = new MySqlConnection(strConnection);
                MySqlCommand command = connection.CreateCommand();

                MySqlDataReader reader;
                command.CommandText = pQuery;
                connection.Open();
                reader = command.ExecuteReader();
                var dataTable = new DataTable();
                dataTable.Load(reader);

                connection.Close();

   }