2013-04-23 122 views
-1

作爲標題表明我有一個持久的數據庫連接有點問題。在這裏,我們有2個數據庫,一個用於開發和測試,另一個用於官方,生產。我們曾經在測試和部署時手動更改不同的連接字符串,有時會發生奇怪的錯誤。當我們突然停止開發數據庫時,整個生產應用程序停止工作,說SQL連接丟失了。但web.config指向生產而不是測試環境!開發人員數據庫連接持續在生產環境

經過一番研究,我們發現了web.config轉換,並認爲這將解決我們的問題。它沒有。即使我們的web.config沒有引用它,這個問題仍然存在,就好像我們的應用程序對開發環境緊張一樣。爲了使事情更清楚,我會發布連接字符串,轉換和發生的最新SQL錯誤(當我們將開發數據庫限制爲單用戶進行一些更新時),以及我們將它連接到我們的方式DBML(L2SQL)。

連接字符串

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> 

    <!--ConnectionString Central--> 
    <add name="OperationsBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/> 
    <add name="AnalisysBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/> 

<!--Production ConnectionString--> 
    <!--<add name="OperationsDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/> 
    <add name="AnalisysDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/>--> 

WEB.CONFIG轉化(製備)

<connectionStrings> 
     <add name="OperationsDB" 
     connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword" 
      providerName="System.Data.SqlClient" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 

     <add name="AnalisysDB" 
     connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword" 
      providerName="System.Data.SqlClient" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 
    </connectionStrings> 

WEB.CONFIG變換(DEV)

<connectionStrings> 
     <add name="OperationsDB" 
     connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword" 
      providerName="System.Data.SqlClient" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 

     <add name="AnalisysDB" 
     connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword" 
      providerName="System.Data.SqlClient" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 
    </connectionStrings> 

SQL ERROR(上dev的單用戶限制環境)

Cannot open database "operation" requested by the login. The login failed. Login failed for user 'lab'. 

最後,我們的方式把它連接到LINQ到SQL的DBML

public partial class OperationDataContext 
    { 
     private const string OPERATION_CONN_STRING = "OperationsDB"; 
     public OperationDataContext() 
      : base(System.Configuration.ConfigurationManager.ConnectionStrings[OPERATION_CONN_STRING].ConnectionString, mappingSource) 
     { 
      OnCreated(); 
     } 
    } 

    public partial class AnalisysDataContext 
    { 
     private const string ANALISYS_CONN_STRING = "AnalisysDB"; 
     public AnalisysDataContext() 
      : base(System.Configuration.ConfigurationManager.ConnectionStrings[ANALISYS_CONN_STRING].ConnectionString, mappingSource) 
     { 
      OnCreated(); 
     } 
    } 
+0

您的基本web.config連接字符串在元素內嗎?元素是的直接後代嗎?元素和路徑必須完全匹配主配置和配置轉換文件 – Tommy 2013-04-23 19:53:24

+0

@Tommy,是的。 web.config轉換工作完美無瑕,這是我對這個問題無知的另一個原因。當轉換後檢查我的產品web.config時,它沒有任何對我的開發數據庫的引用,否則我的客戶端將無法使用該應用程序,因爲連接會指向錯誤的數據庫。我的猜測是,VS正在生成一些引用開發數據庫的底層代碼。 – AdrianoRR 2013-04-24 17:23:07

回答

0

這可能只是一個錯字,但我也注意到,在開發「名稱」爲「OperationsBD」和「 AnaylsisBD「,但轉換設置爲匹配」OperationsDB「和」AnaylsisDB「的名稱。如果這不是拼寫錯誤,它將永遠不匹配,因此從不替換連接字符串。

+0

我更改了數據庫名稱,並在執行時犯了一個錯誤。當我再次閱讀我的問題並修復它時看到了它。但請放心,轉換工作正常,問題仍然存在。 – AdrianoRR 2013-04-23 19:16:06

+0

啊,那麼我的回答是沒有意義的。 – brenton 2013-04-23 19:21:15