开发者

quickly insert data to remote mysql server in vb.net

I have small application in vb.net

I have one table in msaccess, which is having around 20,000 records,

now, I want to insert these records to remote server of mysql,

I tried as below code, it takes around 3-4 hours to insert data,

Can anyone show me faster way to insert data, i will be very glad..

dst in code is having all records from ms Access

For i As Integer = 0 To dst.Tables(0).Rows.Count - 1

mycmd.Connection = mycn

str = "INSERT INTO tblstudentresults(department_desc, grade, roll_no, name, course_code, course_desc, examination_type, total_marks, obtained_marks)"
/*'str = str & " VALUES('" & cname & "','" & sdr("grade").ToString() & "','" & sdr("st_code").ToString() & "','" & sdr("stName").ToString() & "','" & sdr("Subject_code").ToString() & "','" 开发者_JS百科& sdr("Subject").ToString() & "','" & sdr("ExamTitle").ToString() & "','" & sdr("Maxmark").ToString() & "','" & sdr("score").ToString() & "')" -- Added non-VB comment here to improve readability */
str = str & " VALUES('" & cname & "', '" & dst.Tables(0).Rows(i)("grade").ToString() & "', '" & dst.Tables(0).Rows(i)("st_code").ToString() & "', '" & dst.Tables(0).Rows(i)("stName").ToString() & "', '', '" & dst.Tables(0).Rows(i)("Subject").ToString() & "', '" & dst.Tables(0).Rows(i)("ExamTitle").ToString() & "', '" & dst.Tables(0).Rows(i)("Maxmark").ToString() & "', '" & dst.Tables(0).Rows(i)("score").ToString() & "')"
mycmd.CommandText = str
mycmd.ExecuteNonQuery()

next


It might be faster to construct a multiple-row insert in one large string (or maybe chunks of, say 500 rows), then run the entire insert statement in a single call, something roughly like the following:

Dim firstRow as Boolean = True
str = "INSERT INTO tblstudentresults(...) VALUES"

For i As Integer = 0 To dst.Tables(0).Rows.Count - 1

    ' Only insert comma after first row, so we don't have comma at the end.
    If Not firstRow Then str = str & ","

    str = str & "('" & cname & "','" ...

    firstRow = False

Next

mycmd.Connection = mycn
mycmd.CommandText = str
mycmd.ExecuteNonQuery()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜