2017-03-31 31 views
1

使用Microsoft Visual Studio Community - 通過Azure進行部署。錯誤 - 附加信息:初始化字符串的格式不符合從索引0開始的規範

我一直在這個問題上停留了5天。在嘗試保存用戶對測驗的答案時,我仍然會收到此錯誤。我已經瀏覽了很多其他解決方案,詢問同樣的事情,但沒有發現任何解決方案。這是完整的錯誤:

An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code Additional information: Format of the initialization string does not conform to specification starting at index 0.

我已經看過並重新查看我的連接字符串和代碼本身,並沒有發現任何錯誤。

這裏是我的代碼:

@using WebMatrix.Data; 
@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 

var Q1 = ""; 
var Q2 = ""; 
var Q3 = ""; 
var Q4 = ""; 
var Q5 = ""; 
var Q6 = ""; 
var Q7 = ""; 
var Q8 = ""; 
var Q9 = ""; 
var Q10 = ""; 
var weeknum = ""; 

if (IsPost) 
{ 
    Q1 = Request.Form["Question1"]; 
    Q2 = Request.Form["Question2"]; 
    Q3 = Request.Form["Question3"]; 
    Q4 = Request.Form["Question4"]; 
    Q5 = Request.Form["Question5"]; 
    Q6 = Request.Form["Question6"]; 
    Q7 = Request.Form["Question7"]; 
    Q8 = Request.Form["Question8"]; 
    Q9 = Request.Form["Question9"]; 
    Q10 = Request.Form["Question10"]; 
    weeknum = "1"; 




    var db = Database.OpenConnectionString("PayForPlay", "System.Data.SqlClient"); 
    var insertCommand = "INSERT INTO PayForPlay.WeeklyPredictions (WeekNumber, Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8, Question9, Question10) Values(@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10) "; 

    db.Execute(insertCommand, weeknum, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10); 
    Response.Redirect("~/Home/Submitted"); 

這裏是我的連接字符串:

<connectionStrings> 
    <add name=「PayForPlay" 
    providerName="System.Data.SqlClient" 
    connectionString="Data Source=tcp:memberships2016.database.windows.net,1433;Initial Catalog=PayForPlay;Integrated Security=False;User [email protected];Password=Annie207!;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" /> 
    <!--<add name="PayForPlay" connectionString="Data Source=memberships2016.database.windows.net;Initial Catalog=PayForPlay;Integrated Security=False;User ID=annah;Password=*********;Connect Timeout=15;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False " /> --> 
</connectionStrings> 

是評論我的連接字符串的一部分是我嘗試另一個連接字符串(以下蔚藍格式)和它沒有工作。

+0

用戶ID = [LoginForDb] @ [serverName]; ? – Steve

+0

[這裏有一個很好的Azure連接字符串格式的參考。](https://www.connectionstrings.com/sql-azure/) – Siyual

+0

@Steve我只是試過,它仍然給我同樣的錯誤! arrgghhhhhh – Marguerite

回答

0

WebMatrix.Data.OpenConnectionString,據我所知,需要一個完整的連接字符串,而不是存儲在配置文件中的連接字符串的關鍵名稱。

The OpenConnectionString method differs from the Open method, which uses the name of a connection string that is stored in the Web.config file. You might use the OpenConnectionString method when a connection string is programmatically generated or is provided by the user.

這就解釋了爲什麼該方法無法將'PayForPlay'理解爲連接字符串。它希望看到一個"Data Source=memberships2016....."

相反,閱讀建議Open方法的文檔,你可以看到:

The Open(String) method can connect to a database in two ways. The Open(String) method first searches the Web application's App_Data folder for a database file that matches the name that is passed (without the file-name extension) to the method. If no matching database file is found, the method searches the application's Web.config file for a connection string that matches name.

所以,看來你的問題應該使用Open方法與

來解決
var db = Database.Open("PayForPlay"); 
+0

非常感謝您的幫助Steve!在我修復了這個問題並將PayForPlay.WeeklyPredictions改爲WeeklyPredictions之後,我得到了它的工作!我很開心 !! – Marguerite

+0

很高興能有所幫助。作爲該網站的新用戶,我建議閱讀[如何接受答案](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Steve

相關問題