所以這裏是我嘗試做的事情。我有一些數據從mysql數據庫填充數據表。就像示例項目一樣,我一直在從現有表(名爲examquestion)中抽取這些細節來做SELECT查詢。我的查詢會是這樣的:用數據庫中的數據填寫數據表
SELECT * FROM entrancequestion
WHERE Subject='Abstract Reasoning'
ORDER BY RAND()
LIMIT 10)
UNION
(SELECT * FROM entrancequestion
WHERE Subject='English'
ORDER BY RAND()
LIMIT 30)
UNION
(SELECT * FROM entrancequestion
WHERE Subject='Mathematics'
ORDER BY RAND()
LIMIT 30)
UNION
(SELECT * FROM entrancequestion
WHERE Subject='Science'
ORDER BY RAND()
LIMIT 30)
所以在所有的數據表中應該填充100行總數。
我的問題是這些數據實際插入到DataTable
首先我從數據庫中讀取
dim myqry as string
'where myqry as the codes above
dim examdt as new datatable
Dim conn As New SqlClient.SqlConnection("server=localhost;User Id=root;database=jnhsdb")
Try
conn.Open()
Dim cmd As SqlCommand = New SqlCommand(myqry, conn)
'create data reader
Dim rdr As SqlDataReader = cmd.ExecuteReader
'loop through result set
rdr.Read()
'now im lost on this part.
'i want to insert the data into a data table. i got some code in mind but i think im doing it wrong.
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
所以數據,你怎麼竟從數據庫到數據表中插入這些數據?
後續問題: 是否可以從數據表創建數據集以及這些查詢?
謝謝Yuriy。我對使用datareader和dataadapter感到困惑。是的,我忽略了我使用sql而不是mysql作爲連接器和命令的語句。無論如何,我修正了這個問題,不過謝謝你指出這一點。並感謝您的建議 –