简体   繁体   English

为SQL Server数据库创建用户并通过C#.net应用程序访问

[英]Creating user for SQL Server database and accessing through C#.net application

I have created the users for SQL Server database. 我已经为SQL Server数据库创建了用户。 It got created just fine. 它创建得很好。

My problem is when I am accessing through application by executing the following code it does not switch to that user which I am specify as 我的问题是,当我通过执行以下代码访问应用程序时,它不会切换到我指定为的用户

exec as user='myuser'
ALTER USER myuser WITH DEFAULT_SCHEMA=maindb
EXEC sp_addrolemember N'db_owner', N'myuser'

and I have run the query as 我已经运行查询为

DbCommand command = ds.CreateCommand();
command.CommandText = "exec as user='myuser'";
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();

from C# code 从C#代码

Waiting for your valuable responses and comments 等待您的宝贵回应和评论

yes, this is expected. 是的,这是预期的。 the connection string contains the credentials required to connect to the server. 连接字符串包含连接到服务器所需的凭据。 and typically there is a single connection string between the application and database to optimize resources and take advantage of connection pooling etc. 通常在应用程序和数据库之间只有一个连接字符串,以优化资源并利用连接池等。

different users may have different rights in the system, but that can be handled by authorization, not connection strings. 不同的用户在系统中可能具有不同的权限,但是可以通过授权而不是连接字符串来处理。

EXECUTE AS Clause 作为子句执行

The user or login name specified in EXECUTE AS clause must exist as a principal in sys.database_principals or sys.server_principals, respectively, or else the create or alter module operation fails. EXECUTE AS子句中指定的用户名或登录名必须分别作为主体存在于sys.database_principals或sys.server_principals中,否则创建或更改模块操作将失败。 Additionally, the user that creates or alters the module must have IMPERSONATE permissions on the principal. 此外,创建或更改模块的用户必须对主体具有IMPERSONATE权限。

Do you satisfy those conditions? 您满足这些条件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SQL Server Express为ac#.net winform应用程序创建多个用户 - creating multiple users for a c#.net winform application using sql server express 在 C#.NET 应用程序中使用 SQL 服务器时间数据类型? - Use SQL Server time datatype in C#.NET application? 从C#.NET Winform应用程序重新定位SQL Server中的查询 - Repitition of a query in sql server from C#.NET Winform application 后端使用SQL Server 2008的C#.Net应用程序上的TCP错误 - TCP error on a C#.Net application with SQL Server 2008 on the backend “ /”应用程序中的服务器错误。 在c#.net - Server Error in '/' Application. in c#.net 在C#.Net中连接到远程SQL Server - Connecting to remote SQL Server in C#.Net 我如何使用C#.net通过存储过程将pdf文件存储在Sql Server中 - how could i store pdf file in Ms Sql Server through Stored Procedure using C#.net 代码性能:SQL Server查询与C#.Net Web应用程序 - Code performance: SQL Server Query vs C#.Net web application C#.NET WebPages中SQL Server CE数据库事务的语法是什么? - What's the syntax for a SQL Server CE Database Transaction in C#.NET WebPages? 使用C#.Net从SQL Server数据库读取的最快方法 - Fastest way to read from sql server database using C#.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM