开发者

Why does the "drop user" command for SQL not work in C#?

开发者 https://www.devze.com 2023-03-11 15:43 出处:网络
Why does the drop user a_2 command not work when I try to use it in C#, while when I tried it in SQL Server Management Studio, it works there !!???

Why does the drop user a_2 command not work when I try to use it in C#, while when I tried it in SQL Server Management Studio, it works there !!???

This is the code that I used it when I create the user & give it a permission:

use DataBaseName;
create login a_2 with password='Aa123';
create user a_2 for login a_2;
grant insert to a_2;

..........

The connection string used :

Data Source=TheServerName;Integrated Security=True;database=master

Note:

I tried to drop a database from C# and it works perfectly without any problem, but when I use it to drop a user the problem raised here !!!

This is the exception that gets raised

Exception is: Cannot drop the user 'a_2', because it开发者_开发技巧 does not exist or you do not have permission.


I believe your problem is that "you do not have permission". Your application is running as a different user from you. You can solve this by either giving the default application user the necessary permissions on the database (not a good idea) or by creating a special user in the database for your application and having the application run as that user.

Be aware that giving your application (or any user, for that matter) permissions to drop users could be very risky if someone figures out how to hack your application. And you aren't actually including the ability to drop the database in your app, are you? Think about it. Instead of creating users in the database, consider creating a Users table just for your application. Then your users won't have such extensive rights to your actual database.


DROP USER would require use DataBaseName;

Or a different connection string

Data Source=TheServerName;Integrated Security=True;database=DataBaseName

That is, the DROP USER only makes sense in the context of your database. When you run it later you are in the context of master = fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消