2015-11-08 82 views
0

我有以下錯誤Asp.net 4連接字符串問題

public int GetRegisteredUsersCount() 
    { 

     string myScalarQuery = "select count(*) from aspnet_Users where isActivated = 0"; 
     string cs = ConfigurationManager.ConnectionStrings["uniteCms"].ConnectionString; 
     SqlConnection myConnection = new SqlConnection(cs.ToString()); 

     SqlCommand myCommand = new SqlCommand(myScalarQuery, myConnection); 
     myCommand.Connection.Open(); 
     int count = (int) myCommand.ExecuteScalar(); 
     myConnection.Close(); 

     return count; 
    } 

,但我卻在連接字符串中我的webconfig連接字符串部分

<add 
name="uniteCms" 
connectionString="Data Source=myserver;Initial 
Catalog=DB_9DF962_davidbuckleyni37;User ID=DB_9DF962_davidbuckleyni37_admin;Password=" 
providerName="System.Data.SqlClient" 
/> 

enter image description here

+0

將初始目錄更改爲數據庫 –

+0

connectionstrings.com將幫助您。 –

+0

@KenTucker請提供並回答Girggi的錯誤標記其stackoverflows政策不提供鏈接以外的asp.net文檔 – rogue39nin

回答

3

什麼數據源實例是否在使用? 如果您使用的是sqlexpress,那麼您應該將Data source = myserver \ sqlexpress。對於任何情況下,它應該像數據源=服務器名稱\實例名稱

1

您的連接字符串似乎有換行符,這也是錯誤意味着(即Initial\r\nCatalog)。

確保不要在屬性值中放置換行符。

<add 
    name="uniteCms" 
    connectionString="Data Source=myserver;Initial Catalog=DB_9DF962_davidbuckleyni37;User ID=DB_9DF962_davidbuckleyni37_admin;Password=" 
    providerName="System.Data.SqlClient" /> 

如果你真的想打破它,你可以嘗試在連接字符串中的分隔符後加入,但我不知道它會不會有任一被允許。

這可能會奏效,可能會失敗,崩潰或什麼都不用說,後來給你約是未經授權的(因爲只使用了第一線)的異常可能會失敗。

<add 
    name="uniteCms" 
    connectionString="Data Source=myserver; 
        Initial Catalog=DB_9DF962_davidbuckleyni37; 
        User ID=DB_9DF962_davidbuckleyni37_admin; 
        Password=" 
    providerName="System.Data.SqlClient" />