简体   繁体   中英

Database connection in c#.net VS2010

I'm trying to connect to my database in .net using dataset. My code is for a workbook creator. The method takes the questions form the DB and put them in list. When I'm running the code I get the next error:

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: 

the code is :

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);
    }
}

can you tell me whats wrong ? ( in another code its working)

It is telling you where the error is: at index 103 in your initialization, ie connection string.

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

The code formatting in your question doesn't allow for an exact counting of characters, but most likely it is because you used a single backslash (instead of an escaped one like everywhere else) for

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

Change it to

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

Note: The (forward) slash in this fragment " App_Data/bookretorDB1.accdb " should be OK, as Windows APIs typically accept it as a directory separator as well. You may want to, at least for consistency, use a \\\\ here as well.

尝试这样写连接字符串

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM