c#.net VS2010中的数据库连接

本文关键字:数据库连接 VS2010 net | 更新日期: 2023-09-27 18:26:20

我正在尝试使用数据集连接到.net中的数据库。我的代码是为工作簿创建者编写的。该方法从数据库中提取问题,并将其放入列表中。当我运行代码时,我会得到下一个错误:

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["answerswer"].ToString();
        _currentQuest.goodFeedBack=pRow["goodfeedback"].ToString();
        _currentQuest.badFeedBack1=pRow["badfeedback1"].ToString();
        _currentQuest.badFeedBack2=pRow["badfeedback2"].ToString();
        AllQuestions.Add(_currentQuest);
    }
}

你能告诉我怎么了吗?(在另一个代码中它正在工作)

c#.net VS2010中的数据库连接

它会告诉您错误在哪里:初始化中的索引103,即连接字符串。

初始化字符串的格式不符合从索引103开始的规范。。。

你的问题中的代码格式不允许精确计数字符,但很可能是因为你在中使用了一个反斜杠(而不是像其他地方一样使用转义的反斜杠)

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

将其更改为

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

注意:这个片段"App_Data/bookretorDB1.accdb"中的(正向)斜杠应该是可以的,因为Windows API通常也接受它作为目录分隔符。至少为了保持一致性,您可能还需要在此处使用''

尝试写入类似的连接字符串

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