Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In a program, I need to create and add a table to a database at Runtime. I know how to define some fields for it by using Create Table Command (as in following code), but what I need to know is how to set a password for the table at runtime. I mean, when ones wants to open the table, he or she would be asked for password and would not be allowed to open it readily not knowing the password I set for it at runtime. Could you please help me?

Thanks a lot

...
myCommand.CommandText = "CREATE TABLE myTable " +
                                          " (" +
                                            " ID int IDENTITY(1,1) PRIMARY KEY," +
                                            " Name char(10),"+
                                            "Age Number" +
                                            ")";
Posted
Updated 13-Aug-10 21:36pm
v2
Comments
Sandeep Mewara 12-Aug-10 15:24pm    
1. It's an OLD repost of yours with slight modification
2. What do you mean by 'set Username & password for table'?
AspDotNetDev 12-Aug-10 15:37pm    
Are you asking about how to set the SQL permissions for the table? That's not the same thing as setting a username and password.

1 solution

I can think of two seperate solutions in order to make this work in the way you described.
The first method you'll need two seperate login user accounts in SQL Server. The first account will be the actual connection service log-in and the second account will be the connection required to access the table. So in short you'll need 2 database connections open PER USER...
NO THANKS! - So I'm not even going to describe that method further.


The second method would be lock-down the table just as soon as its created via the following script.
SQL
DENY SELECT ON dbo.[myTable] TO useraccount;
DENY INSERT ON dbo.[myTable] TO useraccount;
DENY UPDATE ON dbo.[myTable] TO useraccount;
DENY DELETE ON dbo.[myTable] TO useraccount;
DENY ALTER ON dbo.[myTable] TO useraccount;
DENY VIEW DEFINITION ON dbo.[myTable] TO useraccount;

Afterwards you would have your code of the program manage and validate the password.
When the password validates you simply run the same script as above only changing DENY to GRANT as illustrated below.
SQL
GRANT SELECT ON dbo.[myTable] TO useraccount;
GRANT INSERT ON dbo.[myTable] TO useraccount;
GRANT UPDATE ON dbo.[myTable] TO useraccount;
GRANT DELETE ON dbo.[myTable] TO useraccount;
GRANT ALTER ON dbo.[myTable] TO useraccount;
GRANT VIEW DEFINITION ON dbo.[myTable] TO useraccount;

Hope this helps,
-ArtificerGM
 
Share this answer
 
v2

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