 |
|
ใครรุโค้ด import excel ช่วยดูให้หน่อย งับ ช่วยที่ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Protected Sub Import_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Import.Click
FileUpload1.PostedFile.SaveAs(Server.MapPath("FlieExcel/" + FileUpload1.FileName))
Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= " + Server.MapPath("FlieExcel/" + FileUpload1.FileName) + ";" & _
"Extended Properties=Excel 8.0;"
Dim da = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", ConnStr)
Dim ds As New DataSet
da.Fill(ds)
Dim conn As New SqlConnection(MasterClass.ConnStr)
For Each dr As DataRow In ds.Tables(0).Rows
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Student(Student_ID,Major_ID,Student_Course,Student_pre,Student_first,Student_last,Student_nick,Student_tower,Student_room,Student_pic,StudentPassword)" & _
" VALUES(@Student_ID,@Major_ID,@Student_Course,@Student_pre,@Student_first,@Student_last,@Student_nick,@Student_tower,@Student_room,@Student_pic,@StudentPassword) "
cmd.Parameters.Add("@Student_ID", SqlDbType.VarChar).Value = dr("Student_ID")
cmd.Parameters.Add("@Major_ID", SqlDbType.VarChar).Value = dr("Major_ID")
cmd.Parameters.Add("@Student_Course", SqlDbType.Int).Value = dr("Student_Course")
cmd.Parameters.Add("@Student_pre", SqlDbType.VarChar).Value = dr("Student_pre")
cmd.Parameters.Add("@Student_first", SqlDbType.VarChar).Value = dr("Student_first")
cmd.Parameters.Add("@Student_last", SqlDbType.VarChar).Value = dr("Student_last")
cmd.Parameters.Add("@Student_nick", SqlDbType.VarChar).Value = dr("Student_nick")
cmd.Parameters.Add("@Student_tower", SqlDbType.VarChar).Value = dr("Student_tower")
cmd.Parameters.Add("@Student_room", SqlDbType.VarChar).Value = dr("Student_room")
cmd.Parameters.Add("@Student_pic", SqlDbType.VarChar).Value = dr("Student_ID") & ".jpg"
cmd.Parameters.Add("@StudentPassword", SqlDbType.VarChar).Value = dr("Student_ID")
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Next
MasterClass.Alert.ShowAlertMessage("นำเข้าข้อมูลเรียบร้อย")
End Sub
มันก่อ import ได้น่ะงับ ถ้าข้อมมูลมี 2-3 ข้อมูล แต่พอเจอข้อมูลมากมาก มันก่อขัดค่องซ่ะงั้น บอกว่า ค่า null บ้างค่า string บ้าง ของดูให้หน่อยงับ งง มาก
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2011-09-15 02:19:07 |
By :
mikcat |
View :
1022 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เปลี่ยนการเขียนใหม่ครับ
Code (VB.NET)
Protected Sub Import_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Import.Click
FileUpload1.PostedFile.SaveAs(Server.MapPath("FlieExcel/" + FileUpload1.FileName))
Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= " + Server.MapPath("FlieExcel/" + FileUpload1.FileName) + ";" & _
"Extended Properties=Excel 8.0;"
Dim da = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", ConnStr)
Dim ds As New DataSet
da.Fill(ds)
Dim conn As New SqlConnection(MasterClass.ConnStr)
Dim cmd As New SqlCommand()
cmd.Connection = conn
conn.Open()
cmd.CommandText = "INSERT INTO Student(Student_ID,Major_ID,Student_Course,Student_pre,Student_first,Student_last,Student_nick,Student_tower,Student_room,Student_pic,StudentPassword)" & _
" VALUES(@Student_ID,@Major_ID,@Student_Course,@Student_pre,@Student_first,@Student_last,@Student_nick,@Student_tower,@Student_room,@Student_pic,@StudentPassword) "
For Each dr As DataRow In ds.Tables(0).Rows
Try
cmd.Parameters.Add("@Student_ID", SqlDbType.VarChar).Value = dr("Student_ID")
cmd.Parameters.Add("@Major_ID", SqlDbType.VarChar).Value = dr("Major_ID")
cmd.Parameters.Add("@Student_Course", SqlDbType.Int).Value = dr("Student_Course")
cmd.Parameters.Add("@Student_pre", SqlDbType.VarChar).Value = dr("Student_pre")
cmd.Parameters.Add("@Student_first", SqlDbType.VarChar).Value = dr("Student_first")
cmd.Parameters.Add("@Student_last", SqlDbType.VarChar).Value = dr("Student_last")
cmd.Parameters.Add("@Student_nick", SqlDbType.VarChar).Value = dr("Student_nick")
cmd.Parameters.Add("@Student_tower", SqlDbType.VarChar).Value = dr("Student_tower")
cmd.Parameters.Add("@Student_room", SqlDbType.VarChar).Value = dr("Student_room")
cmd.Parameters.Add("@Student_pic", SqlDbType.VarChar).Value = dr("Student_ID") & ".jpg"
cmd.Parameters.Add("@StudentPassword", SqlDbType.VarChar).Value = dr("Student_ID")
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Exception
'*** Event เมื่อเกิด Error ***'
Finally
'*** (Option) Event เมื่อคำสั่งสมบูรณ์ ***'
End Try
Next
conn.Close()
MasterClass.Alert.ShowAlertMessage("นำเข้าข้อมูลเรียบร้อย")
End Sub
เช็ํค Syntax ด้วยครับ
|
 |
 |
 |
 |
Date :
2011-09-15 09:24:12 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไง อ่ะงับบบบ
|
 |
 |
 |
 |
Date :
2011-09-15 12:45:03 |
By :
mikcat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีตัวอย่างหราาา งับบบ
|
 |
 |
 |
 |
Date :
2011-09-15 12:51:10 |
By :
mikcat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนั้นไม่ใช่ตัวอย่างเหรอครับ 
|
 |
 |
 |
 |
Date :
2011-09-15 13:53:16 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอ้าหราา ไม่ได้ดู อิอิ
|
 |
 |
 |
 |
Date :
2011-09-15 14:02:07 |
By :
mikcat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ วิน งับ มันก่อ insert เข้าไม่ครบอ่ะงับ มันเป็นเพราะอะไร อ่ะพี่
|
 |
 |
 |
 |
Date :
2011-09-15 14:35:57 |
By :
mikcat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบไปแล้วครับ ถ้าอยากรู้ว่าทำไมไม่ครบจะต้อง Debug ดูครับ ว่า Record ไหนที่มีปัญหาครับ 
|
 |
 |
 |
 |
Date :
2011-09-15 17:33:41 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งับ
|
 |
 |
 |
 |
Date :
2011-09-15 22:18:29 |
By :
mikcat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|