Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Could you please give me some solution to protect my sql database basically? I have thought of creating a usename and a password but how to do this on an existing database? And if possible, could you let me know how to do that using csharp code not using Designer.
Thank you so much!
Posted

I'm gonna assume we're talking about MySQL, based on your previous question.
You should be able to use Grant[^] with SqlCommand.ExecuteNonQuery()
E.g
C#
// Create a user
mysqlcommand.CommandText = "CREATE USER 'KingBoy'@'localhost' IDENTIFIED BY 'KingBoys Password'";
mysqlcommand.ExecuteNonQuery();

// Allow KingBoy to select and insert from all tables in database db1
mysqlcommand.CommandText = "GRANT SELECT, INSERT ON db1.* TO 'KingBoy'@'localhost'";
mysqlcommand.ExecuteNonQuery();
 
Share this answer
 
v2
Role based security mechanism would be the best to use to provide different set of privileges to different users. This is a good read on this:

http://www.techrepublic.com/article/understanding-roles-in-sql-server-security/1061781[^]

As for the code I am not sure that all the security mechanisms can be applied through that...not all the APIs are available to be used from code...
 
Share this answer
 

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