2016-03-10 59 views
-1

嗨,任何人都可以幫助我這方面。 我在做一個hang子手項目。 但是單詞應該來自Access oledb。 任何人都可以給我一個關於如何做到這一點的想法。 我使用Office 2007和VS 2010如何將訪問Oledb轉移到C#窗體窗體陣列

對於我將這段代碼:

public void text() 
    { 
     char[] divider = {','}; 
     string[] word = {"GENES","APPLES","GRAPES"}; //Words List replace this with words from access oledb// 
     words = new string[word.Length]; 
     int counter = 0; 
     foreach (string a in word) 
     { 
       string [] splitter = a.Split(divider); 
       words[counter++] = splitter[0]; 
     } 

    } 
+0

您正在使用的Office 2007是爲了什麼?你有沒有搜索「獲取記錄從訪問oledb C#」 –

回答

0
public void text() 
{ 
    List<string> oWords = new List<string>(); 
    string sConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data source=PathtoYourAccessDB.accdb"; 
    string sQueryString= "select YourWordsColumn from YouAccessTable"; 

    using (OleDbConnection oConnection = new OleDbConnection(sConnectionString)) 
    { 
      OleDbCommand oCommand = new OleDbCommand(sQueryString, oConnection); 
      oConnection.Open(); 
      using (OleDbDataReader oReader = oCommand.ExecuteReader()) 
      { 
       while (oReader.Read()) 
        oWords.Add(oReader.GetValue(0).ToString()); 
      } 
    } 
    // If you want the words in an array 
    string [] words = oWords.ToArray(); 
} 
+0

謝謝這工作得很好!, – Mary

+0

對不起,我還想問如何處理int而不是字符串的值是int? – Mary

+0

@Mary List myInts = new List ();然後在while循環中讀取讀取器中的值,將其轉換爲int(oReader.Read()) myInts.Add(Convert.ToInt32(oReader.GetValue(0))); –