Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:04:02+00:00 2026-05-11T20:04:02+00:00

I’m looking for a good way to maintain permissions on who can add data

  • 0

I’m looking for a good way to maintain permissions on who can add data to a database in a C# app and SQL Server 2005.

I need to explain though to make this clear. So let’s take an example of this:

I have two users Bob and Jim, both have been added to the SQL permissions so they have write access to the database. Now all access is based on Domain User Accounts. All other users only have read access.

Now I have a couple tables such as:

  • Users
  • UserPermissions
  • Books
  • BookPublishers

So UserPermissions contains a list of Users and BookPublishers. So for example: Bob has permission to add books for MS Press and Jim has permission to add books for O'Reilly.

Now I need to verify this information and limit what they can add.

So say Jim uses my application from a command line and he writes something like:

Addbook.exe "C# 3.0 in a Nutshell" "O'Reilly"

The tool should go ahead and add the book to the book table.

Now say Bob tries the same command, the tool should error as he does not have permission to add books by O'Reilly.

Right now I need to know how to do a couple things.

  • Verify that a user first has write permission to the SQL Server
  • Verify that the user has write permission to add books by a specific publisher
  • Also I need to verify that the above is true before the tool actually tries to add the data, i.e. I need verification feedback first before the tool continues

Now I’m not 100% worried about the user from injecting malicious data, though it would be nice to stop that, but it’s an internal tool and I guess I can trust the users… (possibly)

Either way I don’t know where to be begin, my SQL skills are very much lacking.

Akk, one last thing, I don’t want to add data using store procedures.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:04:02+00:00Added an answer on May 11, 2026 at 8:04 pm

    Okay, let’s break this down:

    Verify that a user can write to the table (this will return 1 if true, 0 if not):

    SELECT isnull(has_perms_by_name('MyDb.dbo.MyTable', 'OBJECT', 'INSERT'), 0)
    

    Verify that a user can write that publisher:

    SELECT count(*) FROM UserPermissions WHERE
    UserName = 'username' AND Publisher = 'publisher'
    

    Now, that’s the SQL for those, and not the actual C#. To get the values in C#:

    SqlConnection SqlConn = new SqlConnection("connection_string_goes_here");
    SqlCommand SqlCmd = new SqlCommand();
    
    SqlConn.Open();
    SqlCmd.Connection = SqlConn;
    SqlCmd.CommandText = "SELECT isnull(has_perms_by_name('MyDb.dbo.MyTable', " +
        "'OBJECT', 'INSERT'), 0)"
    
    if (SqlCmd.ExecuteScalar())
    {
       SqlCmd.CommandText =
           "SELECT count(*) FROM UserPermissions WHERE " +
           "Username = " + System.Environment.UserDomainName + "\" + 
               System.Environment.UserName + " " +
           AND Publisher = @Publisher";
       SqlCmd.Parameters.Add("@Publisher", SqlDbType.NVarChar);
       SqlCmd.Parameters("@Publisher").Value = PublisherInput;
    
       if(SqlCmd.ExecuteScalar())
       {
           SqlCmd.Parameters.Clear();
           SqlCmd.CommandText = "INSERT INTO Books (Title, Publisher) VALUES " +
                                "(@Title, @Publisher)";
           SqlCmd.Parameters.Add("@Title", SqlDbType.NVarChar);
           SqlCmd.Parameters.Add("@Publisher", SqlDbType.NVarChar);
           SqlCmd.Parameters("@Title").Value = TitleInput;
           SqlCmd.Parameters("@Publisher").Value = PublisherInput;
           SqlCmd.ExecuteNonQuery();
       }
    }
    
    SqlCmd.Dispose();
    SqlConn.Close();
    SqlConn.Dispose();
    

    As a final note, cleanse your input. Use parameters in your application, and do not trust any user, even internal ones. I can’t stress that enough.

    Edit: Because there are more than one way to skin a cat, I felt it foolish of me to not include the LINQ to SQL solution (at least to the count issue):

    int PermsAvailable = (from up in db.UserPermissions
                          where up.Username == 
                              System.Environment.UserDomainName + "\" + 
                              System.Environment.UserName
                          && up.Publisher == PublisherInput
                          select up).Count();
    if(PermsAvailable)
    {
        var NewBook = New Book with {.Title = TitleInput, .Publisher = PublisherInput};
        db.Books.Add(NewBook);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.