2012-02-29 78 views
1

我試圖使用數據集連接到.net中的數據庫。我的代碼是用於工作簿創建者的。該方法從DB獲取問題並將其放入列表中。當我運行代碼,我得到了一個錯誤:c#.net中的數據庫連接VS2010

System.ArgumentException was unhandled by user code 
    Message=Format of the initialization string does not conform to specification starting at index 103. 
    Source=System.Data 
    StackTrace: 
     at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) 
     at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) 
     at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) 
     at System.Data.OleDb.OleDbConnectionString..ctor(String connectionString, Boolean validate) 
     at System.Data.OleDb.OleDbConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) 
     at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) 
     at System.Data.OleDb.OleDbConnection.ConnectionString_Set(String value) 
     at System.Data.OleDb.OleDbConnection.set_ConnectionString(String value) 
     at System.Data.OleDb.OleDbConnection..ctor(String connectionString) 
     at System.Data.OleDb.OleDbDataAdapter..ctor(String selectCommandText, String selectConnectionString) 
     at bookPage.getPageDB() in c:\Users\asaf_pearl\Documents\Visual Studio 2010\WebSites\bookreator\App_Code\question.cs:line 188 
     at book.Page_init(Object sender, EventArgs e) in c:\Users\asaf_pearl\Documents\Visual Studio 2010\WebSites\bookreator\book.aspx.cs:line 23 
     at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
     at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) 
     at System.Web.UI.Control.OnInit(EventArgs e) 
     at System.Web.UI.Page.OnInit(EventArgs e) 
     at System.Web.UI.Control.InitRecursive(Control namingContainer) 
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
    InnerException: 

代碼:

public void getPageDB() 
{ 
    string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
    Source=C:\\Users\\asaf_pearl\\Documents\\Visual Studio 
    2010\\WebSites\bookreator\\App_Data/bookretorDB1.accdb" + ";"; 
    string myQuery = "select from fivebaloons where page=1"; 

    OleDbDataAdapter oda = new OleDbDataAdapter (myQuery, ConnStr); 
    DataSet ds = new DataSet(); 
    oda.Fill(ds); 
    foreach(DataRow pRow in ds.Tables[0].Rows){ 

     _currentQuest.question=pRow["question"].ToString(); 
     _currentQuest.questionNumber =Convert.ToInt16(pRow["questionnumber"]); 
     _currentQuest.rightAnswer=pRow["answer"].ToString(); 
     _currentQuest.goodFeedBack=pRow["goodfeedback"].ToString(); 
     _currentQuest.badFeedBack1=pRow["badfeedback1"].ToString(); 
     _currentQuest.badFeedBack2=pRow["badfeedback2"].ToString(); 
     AllQuestions.Add(_currentQuest); 
    } 
} 

你能告訴我什麼是錯? (在另一個代碼中它的工作)

+0

請告訴我和代碼及其工作這有什麼區別?顯然,連接字符串有一些細微差別。 – Alex 2012-02-29 11:03:59

回答

1

它告訴你錯誤的位置:在初始化的索引103處,即連接字符串。

... Format of the initialization string does not conform to specification starting at index 103 ...

在你的問題格式化代碼不允許字符的準確計數,但最有可能是因爲你用一個反斜槓(而不是一個轉義一個像其他地方一樣)對

string connStr = "... WebSites\bookreator ..."; 

將其更改爲

string connStr = "... WebSite\\bookreator ..."; 

注意:此片段「App_Data/bookretorDB1.accdb」應該是確定的(前)斜槓,因爲Windows的API通常接受它作爲一個目錄分離器也是如此。至少爲了保持一致性,您也可以在這裏使用\\

+0

不錯,請注意,'SELECT FROM ....'缺少一個* – Steve 2012-02-29 11:09:54

+0

良好的觀察! – code4life 2012-03-09 21:26:03

0

嘗試寫連接字符串這樣

string constr = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", YourServerName,YourDatabaseName);