 |
|
ต้องการ import excel ไป update ข้อมูลในตาราง แต่เกิดปัญหาตรงที่คอลัมน์ที่เป็นตัวเลขกลายเป็นค่า 0 ทั้งหมด ไม่ทราบว่าเกิดจากอะไรคะ |
|
 |
|
|
 |
 |
|
ลองDebug ดูแล้วคะ ไม่เกินอะไรขึ้นแต่ตอนนี้ update ทุกrow เป็นค่าของ row สุดท้ายหมดเลยคะ
รบกวน พี่ๆช่วยหน่อยนะคะลองทำมาหลายวันแล้ว แต่พอทำเปน insert สามารถทำได้
Code (VB.NET)
For i As Integer = 0 To dgvImportScore.RowCount - 1
''strsql = "INSERT INTO Table_1(resub_id,nisit_id,name_score,midterm_score,final_score,project_score,homework_score)"
''strsql &= "VALUES('" & re_subid & "' , '" & dgvImportScore.Rows(i).Cells(1).Value & "', '" & dgvImportScore.Rows(i).Cells(4).Value & "', '" & dgvImportScore.Rows(i).Cells(5).Value & "', '" & dgvImportScore.Rows(i).Cells(6).Value & "', '" & dgvImportScore.Rows(i).Cells(7).Value & "', '" & dgvImportScore.Rows(i).Cells(8).Value & "')"
strsql = "UPDATE Table_1 SET nisit_id = '" & dgvImportScore.Rows(i).Cells(1).Value & "', name_score = '" & dgvImportScore.Rows(i).Cells(4).Value & "', midterm_score = '" & dgvImportScore.Rows(i).Cells(5).Value & "' , final_score = '" & dgvImportScore.Rows(i).Cells(6).Value & "' , project_score = '" & dgvImportScore.Rows(i).Cells(7).Value & "' , homework_score = '" & dgvImportScore.Rows(i).Cells(8).Value & "' WHERE resub_id = '" & re_subid & "' "
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
Next
MessageBox.Show("เพิ่มคะแนนเรียบร้อยแล้ว")
ไม่ทราบว่ามีตรงไนผิดไหมคะ พอดีเอามาจาก insert แต่เปลี่ยนคำสั่งเป็น upadate
|
 |
 |
 |
 |
Date :
2015-05-18 21:05:31 |
By :
Preeyapan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
WHERE resub_id = '" & re_subid & "' " หรือเปล่า ครับ
|
 |
 |
 |
 |
Date :
2015-05-19 16:26:22 |
By :
joevoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามคุณ joevoy ครับ
resub_id มันเหมือนกันทั้ง 3 แถวเลย
|
 |
 |
 |
 |
Date :
2015-05-19 16:34:35 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ทราบว่าเหมือนกันตรงไหนทั้ง 3 แถวหรอคะ ยังไม่ค่อยเข้าใจเลยคะ
เพราะต้องการ update ข้อมูลทั้งหมดในตาราง ตาม resub_id ที่เลือก โดยการ import file excel
ถ้าแบบนี้ต้องเปลี่ยนโค้ดในส่วนไหนหรอคะ
ขอบคุณคะ
|
 |
 |
 |
 |
Date :
2015-05-21 22:22:22 |
By :
Preeyapan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวแปร re_subid เอามาจากไหนครับ
เมื่อใช้ ใน loop for
For i As Integer = 0 To dgvImportScore.RowCount - 1
ตัวแปร strsql จะถูก generate ให้มี where re_subid = 're_subid_เดียวกัน' ทุก loop เลยนะครับ
ซื้อการ update มันจะทำ กับ record ที่มี re_subid ตรงกัน
ดังนั้น เมื่อมันมี where ที่เหมือนกัน record สุดท้ายเท่านั้นที่ได้รับการอัพเดท เพราะทำหลังสุด
อันแรกที่ทำก่อน จะถุกทับด้วย record หลังครับ
ปล. ที่ insert ทำได้ เพราะไม่ได้อ้างอิง where เป็นการนำเข้าใหม่ ไม่สนใจ record เก่าที่มีอยู่แล้ว
|
ประวัติการแก้ไข 2015-05-21 23:07:37
 |
 |
 |
 |
Date :
2015-05-21 23:04:16 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วคะ ขอบคุณมากคะ
|
 |
 |
 |
 |
Date :
2015-05-24 22:21:51 |
By :
Preeyapan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2015-05-25 09:37:18 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|