 |
|
สอบถามปัญหาการ insert sql2000 โดยใช้ loop for แต่มันลงฐานข้อมูลแค่คนเดียวครับ(ดึงข้อมูลมาจาก Excel) .net 2005 ครับ |
|
 |
|
|
 |
 |
|
ปัญหามันก้มีอยู่ว่า ผมดึงข้อมูลมาจาก excel มาไว้ใน Datagridview แล้ว ก็บันทึกลงฐานข้อมูล
sql2000 ข้อมูลใน excel แสดงถูกต้อง แต่ข้อมูลที่เราวนลูปลงไปมันมีแค่คนเดียว แต่ข้อมูลไปทุก row ครับ
ปัญหาที่ตอนที่แล้วให้ union นั่นเหละครับ อาจารย์บอกให้ไปแก้ตอนที่เรา import โดยใช้ if เลยแสดงข้อมุลง่ายดี
โดยดูที่ค่า xx30 (หมายถึงตัวอ่านที่ column31) ถ้าว่ามีเกรียตินิยมหรือไม่ 1 คือที่1 2 คือที่สอง สามคือไม่มีนั่นเอง
ผิดตรงไหนช่วยแนะนำด้วยครับ ขอบคุณมากครับผม ใกล้ถึงความจริงแล้ว เหอๆ 
Code (VB.NET)
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim i As Integer = 0
Dim sqlAdd As String = ""
Dim xx As OleDb.OleDbDataReader
comOdb = New OleDbCommand("SELECT * FROM [ต้นฉบับ$]", Conn1)
xx = comOdb.ExecuteReader
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim dgNum As Integer = DataGridView1.RowCount
Dim comAdd As New SqlCommand
Dim ss As String = ""
If xx.Read Then
' 3 , 4 , 28 , 30 เป็นตัวเลขครับผม
For i = 1 To dgNum
sqlAdd = "INSERT INTO Graduate (GdtID,EdtID,GdtRank,GdtTitle,GdtFname,"
sqlAdd &= " GdtLname,GdtAdd,GdtTumbol,GdtAumphur,GdtProName,GdtProID,GdtHonor)"
sqlAdd &= " VALUES(" & xx(3).ToString & ","
sqlAdd &= "" & xx(14).ToString & ","
sqlAdd &= "'" & xx(4).ToString & "',"
sqlAdd &= "'" & xx(5).ToString & "',"
sqlAdd &= "'" & xx(6).ToString & "',"
sqlAdd &= "'" & xx(7).ToString & "',"
sqlAdd &= "'" & xx(25).ToString & "',"
sqlAdd &= "'" & xx(26).ToString & "',"
sqlAdd &= "'" & xx(27).ToString & "',"
sqlAdd &= "'" & xx(11).ToString & "',"
sqlAdd &= "" & xx(28).ToString & ","
If xx(30).ToString = "0" Then
ss = "3"
Else
ss = xx(30)
End If
sqlAdd &= "" & ss & ")"
With comAdd
.CommandType = CommandType.Text
.CommandText = sqlAdd
.Connection = Conn
.ExecuteNonQuery()
End With
If i = dgNum Then
MessageBox.Show("บันทึกลงฐานข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Hide()
End If
Next i
Exit Sub
Else
MessageBox.Show("เกิดข้อผิดพลาด !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Conn1.Close()
DtApt = Nothing
Conn1 = Nothing
End Sub
Tag : .NET, MySQL, Excel (Excel.Application), VB.NET, VS 2005 (.NET 2.x)
|
|
 |
 |
 |
 |
Date :
2010-09-03 19:12:40 |
By :
AoFzaIT |
View :
1511 |
Reply :
12 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Import Excel Data To DataTable
Dim sqlQuery As String = "SELECT * FROM [ต้นฉบับ$]"
Dim objCommQuery As New OleDbCommand(sqlQuery, Conn1)
Dim Dt As DataTable
Dim objDataAdapter1 As New OleDbDataAdapter(objCommQuery)
objDataAdapter1.Fill(Dt)
' Import DataTable To Sql Server Database
Dim sqlInsert As String = "INSERT INTO [Graduate] ([GdtID], [EdtID], [GdtRank], [GdtTitle], [GdtFname], [GdtLname], [GdtAdd], [GdtTumbol], [GdtAumphur], [GdtProName], [GdtProID], [GdtHonor]) VALUES (@GdtID, @EdtID, @GdtRank, @GdtTitle, @GdtFname, @GdtLname, @GdtAdd, @GdtTumbol, @GdtAumphur, @GdtProName, @GdtProID, @GdtHonor)"
Dim objCommInsert As New OleDbCommand(sqlInsert, Conn)
objCommInsert.Parameters.Add("@GdtID", SqlDbType.NVarChar, 50, "Field1") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@EdtID", SqlDbType.NVarChar, 50, "Field2") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtRank", SqlDbType.NVarChar, 50, "Field3") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtTitle", SqlDbType.NVarChar, 50, "Field4") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtFname", SqlDbType.NVarChar, 50, "Field5") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtLname", SqlDbType.NVarChar, 50, "Field6") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtAdd", SqlDbType.NVarChar, 50, "Field7") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtTumbol", SqlDbType.NVarChar, 50, "Field8") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtAumphur", SqlDbType.NVarChar, 50, "Field9") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtProName", SqlDbType.NVarChar, 50, "Field10") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtProID", SqlDbType.NVarChar, 50, "Field11") ' Field ของ excel กำหนดเอาเอง
objCommInsert.Parameters.Add("@GdtHonor",SqlDbType.NVarChar, 50, "Field12") ' Field ของ excel กำหนดเอาเอง
Dim objDataAdapter2 As New OleDbDataAdapter()
objDataAdapter2.InsertCommand = objCommInsert
objDataAdapter2.Update(Dt)
Conn1.Close()
Conn1 = Nothing
End Sub
|
 |
 |
 |
 |
Date :
2010-09-03 22:17:41 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ถ้าสงสัยยังไงรบกวนอีกทันะครับ
แหะๆ (ผมใช้ sql2000 ไมุ่รู้เหมือนกันรึเปล่า - -a)
|
 |
 |
 |
 |
Date :
2010-09-04 14:12:17 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันไม่สามารถใส่ข้อมูลใน DataTable ได้อ่ะครับ
งง ลองใส่ Ds ดูมันก็เอาข้อมูลเข้า Dt ไม่ได้อยู่ดี ช่วยดูให้หน่อยครับ
ขอบคุณอีกครังครับผม 

|
 |
 |
 |
 |
Date :
2010-09-05 11:25:33 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim Dt As New DataTable()
|
 |
 |
 |
 |
Date :
2010-09-05 12:07:11 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันก็ไม่ได้อยู่ดีครับ 
กดปุ่ม ครั้งที่ 1 ไม่ทำงาน
ครั้งที่สอง ขึ้น
ผมก็ งง อยู่ดี มันไม่ต้องใช้ลูบในการ insert หรอครับมันมีข้อมูล 2000 กว่า อ่ะครับ
ผมลองเอา for next ไปใส่มันก็ไม่ได้ เหอๆๆ

|
ประวัติการแก้ไข 2010-09-05 13:45:19 2010-09-05 15:51:25
 |
 |
 |
 |
Date :
2010-09-05 13:43:17 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ต้องวนลูปหรอก insert แบบทั้งตารางอย่างนี้ใครใช้วนลูปถือว่าโครตเชย
เขาเอา table ดัมพ์ลง database เลย แตกำหนด field ให้ถูกๆ แล้วกัน
แต่เอา conn1 มาดูดิ๊ ว่าติดต่อ excel ยังไง ???
|
 |
 |
 |
 |
Date :
2010-09-05 20:52:21 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class frmAddDatabase
Dim Conn1 As New OleDb.OleDbConnection
Dim DtApt As New OleDbDataAdapter
Dim comOdb As OleDbCommand
Dim strConn2 As String
Dim ToClose As Boolean = False
เนี่ยครับ sql ใช้ Module
|
 |
 |
 |
 |
Date :
2010-09-05 20:59:19 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไหนอ่ะ connection string ที่ใช้ติดต่อ excel ?????
|
 |
 |
 |
 |
Date :
2010-09-05 21:06:31 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อ โทษทีครับ
Code (VB.NET)
Private Sub btnBorwse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorwse.Click
Dim strFileName As String
With ofDlg
.Title = "กรุณาเลือกไฟล์ฐานข้อมูล Excel"
.Filter = "ไฟล์ฐานข้อมูล Excel (*.mdb)|*.xls"
.FileName = ""
.Multiselect = False
.FilterIndex = 0
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
strFileName = .FileName
txtSql.Text = strFileName
Dim objConn As New OleDbConnection
strConn2 = "Provider=Microsoft.JET.OLEDB.4.0;" & "Data source=" & strFileName & ";Extended Properties=Excel 8.0;"
objConn = New OleDbConnection(strConn2)
objConn.Open()
With Conn1
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn2
.Open()
End With
Else
strFileName = ""
txtSql.Text = ""
End If
End With
DataGridView1.DataSource = Nothing
grbSearch.Enabled = False
grbManage.Enabled = True
btnSave.Enabled = False
btnShow.Enabled = True
End Sub
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Dim sqlShow As String
Dim ds As New DataSet
Dim sqlCountGdt As String = ""
sqlShow = "select * from [ต้นฉบับ$]"
DtApt = New OleDbDataAdapter(sqlShow, Conn1)
DtApt.Fill(ds, "[ต้นฉบับ$]")
DataGridView1.DataSource = ds.Tables("[ต้นฉบับ$]")
DataGridView1.Refresh()
grbSearch.Enabled = False
grbManage.Enabled = True
btnSave.Enabled = True
btnShow.Enabled = False
End Sub
|
 |
 |
 |
 |
Date :
2010-09-05 21:17:36 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
event ล่ะมั้ง
ต้องเรียก opendialog ก่อนมันถึงจะสร้าง object conn1
คงไปเรียกมันก่อนจะสร้างล่ะสิ
|
ประวัติการแก้ไข 2010-09-05 21:53:03
 |
 |
 |
 |
Date :
2010-09-05 21:49:27 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขี้เกียจตอบแระ ไปไล่ debug เอาเองแล้วกัน
พวก non sense error เนี่ย แก้เอาเองตามที่มันฟ้องแล้วกันนะ
|
 |
 |
 |
 |
Date :
2010-09-05 21:56:41 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับที่แนะนำ 
สู้ๆ สู้ตาย!!
|
ประวัติการแก้ไข 2010-09-12 20:34:56 2010-09-12 20:37:57
 |
 |
 |
 |
Date :
2010-09-05 22:11:55 |
By :
AoFzaIT |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|