2015-06-22 53 views
0

我目前在vb.net中爲學校分配構建了一個多選題測驗程序。問題,選項和答案都存儲在Microsoft Access文件(.mdb)中的表中。我已經將它作爲數據連接導入並創建了包含表格的數據鏈接。我目前的問題是將表中的字符串移動到一個數組中,以便顯示和比較它們(用於自動標記)。推動這個障礙將是一個很大的幫助。將字符串從數據庫移動到數組

感謝,

回答

0

有人也許能夠提供更有效的答案,但在我去對此方法如下:

'create the connection, replace with the relevant connection string and database file 
Dim con As New OleDbConnection 
    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = db.mdb" 
    con.Open() 
'ready for loading, creating new datatables and sets to load the data from the database into. 
    Dim dt As New DataTable 
    Dim ds As New DataSet 
    ds.Tables.Add(dt) 
    Dim da As New OleDbDataAdapter 
' your query - change to what is needed. 
    da = New OleDbDataAdapter("Select * from TABLE", con) 
    da.Fill(dt) 
'fill into a datagridview, which you need to add to the form 
    DataGridView1.DataSource = dt.DefaultView 
    con.Close() 
'create the array, change x to how many entries 
    Dim arr(0 To x) As String 
'check the datagridview for where the data is being filled to. Adds the value of position 0,0 in the DGV to position arr(0), for example. first is row, second is column. 
    arr(0) = DataGridView1.Item(0, 0).Value 
    arr(1) = DataGridView1.Item(1, 0).Value 
    arr(2) = DataGridView1.Item(2, 0).Value 
    arr(3) = DataGridView1.Item(3, 0).Value 
    arr(4) = DataGridView1.Item(4, 0).Value 
    arr(5) = DataGridView1.Item(5, 0).Value 
    arr(6) = DataGridView1.Item(6, 0).Value 
    arr(7) = DataGridView1.Item(7, 0).Value 
    arr(8) = DataGridView1.Item(8, 0).Value 
+0

感謝您的回答,但一對夫婦的變量指定了「聲明期望」錯誤。即arr和con。 –

+0

Marc,代碼對我來說工作得非常好。檢查你是否正確地將代碼放入函數,過程等中,並且該過程的語法是正確的。 – farewell

相關問題