2017-08-23 26 views
0

在我的代碼中,兩個表已連接並且在本地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(); 
       } 
      } 
     } 
    } 

誤差圖像:

**ERROR IMAGE**

回答

0

通常其一個端口問題可能由防火牆阻止,所以檢查我們使用telnet命令從應用服務器到數據庫服務器如下:

在windows命令中輸入:

telnet database_ip_address_or_host_name port 

實例爲SQL Server的情況下:

telnet 192.168.1.1 1433 

因此,要解決你的問題:在數據庫服務器

    1. 打開數據庫端口確保沒有防火牆阻止了你在應用服務器上連接數據庫的應用程序

    how to open port in windows

  • +0

    如何連接兩個數據庫,並使用的SqlConnection CON C# –

    0

    聽起來像應用程序池服務帳戶和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

    +0

    綁定記錄爵士 如何連接兩個數據庫,並使用的SqlConnection CON C# –

    +0

    連接字符串名稱** **構造,** ** constr1如果 –

    +0

    兩個數據庫綁定記錄在同一個SQL Server實例上,您只需要一個連接字符串。只要確保你的用戶有正確的權限。 –

    相關問題