2015-06-22 55 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

相关问题