【问题标题】:The server principal " " is not able to access the database " "under the current security context服务器主体“ ”无法在当前安全上下文下访问数据库“ ”
【发布时间】:2017-08-23 05:44:50
【问题描述】:

在我的代码中,两个表已连接,并且在本地 Host 中运行良好。但是当我连接相同的代码来运行时,代码无法正常工作。

连接字符串:

string constr = ConfigurationManager.ConnectionStrings["strconstr"].ConnectionString;

string constr = ConfigurationManager.ConnectionStrings["constrHOS"].ConnectionString;


 private void BindContactPerson(string hdnHospitalContactPersonID)
    {
        BD_Hospital iHospital = new BD_Hospital();
        string constr = ConfigurationManager.ConnectionStrings["constrHOS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT D.DepartmentName,DS.DesignationName,hc.ContactPersonName,hc.ContactPersonAddress,hc.Mobile,hc.Phone,hc.AadhaarCardNo FROM HospitalContactPersonDeatils hc LEFT JOIN VAN_SETTING.[dbo].[Department] D ON D.DepartmentID =hc.DepartmentID LEFT JOIN VAN_SETTING.[dbo].[Designation] DS ON DS.DesignationID =hc.DesignationID where HospitalID='" + hdnHospitalContactPersonID + "'    ", con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    gvCustomers.DataSource = dt;
                    gvCustomers.DataBind();
                }
            }
        }
    }

错误图片:

【问题讨论】:

    标签: c# sql asp.net database-connection left-join


    【解决方案1】:

    通常它的端口问题可能被防火墙阻止,因此我们使用从应用程序服务器到数据库服务器的 telnet 命令进行检查,如下所示:

    在windows命令中输入:

    telnet database_ip_address_or_host_name port
    

    Sql server 案例示例:

    telnet 192.168.1.1 1433
    

    所以要解决这个问题,你必须:

    1. 在数据库服务器中打开数据库端口
    2. 确保没有防火墙阻止应用服务器中的应用程序连接到数据库

    how to open port in windows

    【讨论】:

    • 如何使用SqlConnection con c#连接两个数据库并绑定记录
    【解决方案2】:

    听起来应用程序池服务帐户和 SQL 登录之间的映射存在问题,或者 SQL 登录和 SQL 用户帐户之间的映射可能存在问题。

    解决此问题的最简单方法是以运行您网站的应用程序池的服务帐户身份登录。登录后,尝试打开 SQL Server 管理控制台并尝试使用集成身份验证访问数据库。无论您看到什么错误,您的网站都会遇到同样的错误。

    更多信息可以在this question找到。

    如果您不知道应用程序池服务帐户是什么,try this article

    Click here if you don't know the difference between a SQL login and a SQL user.

    【讨论】:

    • 先生如何使用SqlConnection con c#连接两个数据库并绑定记录
    • 连接字符串名称 constr , constr1
    • 如果两个数据库在同一个 SQL Server 实例上,你只需要一个连接字符串。只需确保您的用户拥有正确的权限即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2018-03-29
    相关资源
    最近更新 更多