 |
|
Syntex Error in INSERT INTO Stament จะต้องเข้าไปแก้ตรงไหนคับ |
|
 |
|
|
 |
 |
|
อันนี้ code หัวโปรแกรม
Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Public Class FrmConnection
Inherits System.Windows.Forms.Form
Dim dbConn As OleDbConnection
Dim dtAdapter As OleDbDataAdapter
Dim cmdBuilder As OleDbCommandBuilder
Dim mycmd As OleDbCommand
Dim dtSet As DataSet
Dim dtTable As DataTable
Dim objCurrencyMgr As CurrencyManager
Dim currRecPos As Integer
Dim fdname As String
Dim addnewMode As Boolean
Dim sql As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ozni\app1\app1\data.mdb;")
dtAdapter = New OleDbDataAdapter("SELECT * FROM tbData", dbConn)
dtSet = New DataSet()
dtTable = New DataTable()
cmdBuilder = New OleDbCommandBuilder(dtAdapter)
dtAdapter.Fill(dtSet, "tbData")
dtTable = dtSet.Tables(0)
objCurrencyMgr = Me.BindingContext(dtTable, "")
txID.DataBindings.Add("text", dtTable, "ID")
txIsbn10.DataBindings.Add("text", dtTable, "isbn10")
txIsbn13.DataBindings.Add("text", dtTable, "isbn13")
txBook.DataBindings.Add("text", dtTable, "book")
txAuthor.DataBindings.Add("text", dtTable, "author")
txPublis.DataBindings.Add("text", dtTable, "publis")
txPublis_add.DataBindings.Add("text", dtTable, "publis_add")
txDistri.DataBindings.Add("text", dtTable, "distri")
txDistri_add.DataBindings.Add("text", dtTable, "distri_add")
txPrice.DataBindings.Add("text", dtTable, "price")
txPrint.DataBindings.Add("text", dtTable, "print")
txPrintdate.DataBindings.Add("text", dtTable, "printdate")
txMaintain.DataBindings.Add("text", dtTable, "maintain")
txMaintaindate.DataBindings.Add("text", dtTable, "maintaindate")
txMaintainID.DataBindings.Add("text", dtTable, "maintainID")
txDonor.DataBindings.Add("text", dtTable, "donor")
txStatus.DataBindings.Add("text", dtTable, "status")
txNote.DataBindings.Add("text", dtTable, "note")
DisableReadWrite()
AdjustPosition(sender, e)
AddHandler objCurrencyMgr.PositionChanged, AddressOf AdjustPosition
AddHandler btndelete.Click, AddressOf AdjustPosition
AddHandler btncancel.Click, AddressOf AdjustPosition
AddHandler btnfind.Click, AddressOf AdjustPosition
AddHandler btnfilter.Click, AddressOf AdjustPosition
End Sub
อันนี้ code ปุ่ม save
Code (VB.NET)
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
'ปุ่มบันทึก
Try
objCurrencyMgr.EndCurrentEdit()
If addnewMode = True Then
dbConn.Open()
mycmd = New OleDbCommand("INSERT INTO tbData(ID, isbn10, isbn13, book, author, publis, publis_add, distri, distri_add, price, print, printdate, maintain, maintaindate, maintainID, donor, status, Note) VALUES ('" & txID.Text & "','" & txIsbn10.Text & "','" & txIsbn13.Text & "','" & txBook.Text & "','" & txAuthor.Text & "','" & txPublis.Text & "','" & txPublis_add.Text & "','" & txDistri.Text & "','" & txDistri_add.Text & "','" & txPrice.Text & "','" & txPrint.Text & "','" & txPrintdate.Text & "','" & txMaintain.Text & "','" & txMaintaindate.Text & "','" & txMaintainID.Text & "','" & txDonor.Text & "','" & txStatus.Text & "','" & txNote.Text & "')", dbConn)
'sql &= "VALUES(pa1, pa2, pa3, pa4, pa5, pa6, pa7, pa8, pa9, pa10, pa11, pa12, pa13, pa14, pa15, pa16, pa17, pa18)"
mycmd.ExecuteNonQuery()
'mycmd.Parameters.AddWithValue("pa1", txID.Text)
'mycmd.Parameters.AddWithValue("pa2", txIsbn10.Text)
'mycmd.Parameters.AddWithValue("pa3", txIsbn13.Text)
'mycmd.Parameters.AddWithValue("pa4", txBook.Text)
'mycmd.Parameters.AddWithValue("pa5", txAuthor.Text)
'mycmd.Parameters.AddWithValue("pa6", txPublis.Text)
'mycmd.Parameters.AddWithValue("pa7", txPublis_add.Text)
'mycmd.Parameters.AddWithValue("pa8", txDistri.Text)
'mycmd.Parameters.AddWithValue("pa9", txDistri_add.Text)
'mycmd.Parameters.AddWithValue("pa10", txPrice.Text)
'mycmd.Parameters.AddWithValue("pa11", txPrint.Text)
'mycmd.Parameters.AddWithValue("pa12", txPrintdate.Text)
'mycmd.Parameters.AddWithValue("pa13", txMaintain.Text)
'mycmd.Parameters.AddWithValue("pa14", txMaintaindate.Text)
'mycmd.Parameters.AddWithValue("pa15", txMaintainID.Text)
'mycmd.Parameters.AddWithValue("pa16", txDonor.Text)
'mycmd.Parameters.AddWithValue("pa17", txStatus.Text)
'mycmd.Parameters.AddWithValue("pa18", txNote.Text)
End If
If txBook.Text = " " Then
MessageBox.Show("ต้องระบุชื่อหนังสือถึงจะทำการบันทึกได้", "เตือน !", MessageBoxButtons.OK)
txBook.Focus()
End If
cmdBuilder = New OleDbCommandBuilder(dtAdapter)
dtAdapter.Update(dtTable)
MessageBox.Show("บันทึกข้อมูลแล้ว", "Save Data", MessageBoxButtons.OK)
Catch ErrFromSave As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้!" & ErrFromSave.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
อันนี้รูป ที่ Error

ผมเข้าใจว่า Syntex Error คือ ผมเขียนคำสั่งหรือไวยากรณ์ผิด แต่ที่ผมไม่รู้คือ ผมจะต้องแก้อย่างไร แก้ตรงไหน
เพราะผมใช้ DataAdapter ดึงข้อมูลเข้ามาใน DataSet แล้วใช้ DataTables อีกทีหนึ่ง ตารางมีอันเดียว
ลบได้ แก้ไขได้ แต่เพิ่มไม่ได้ ในฐานข้อมูลผมตั้งค่าให้เป็น Text หมดเลย ID ก็เป็น Text
ตอนนี้ที่ผมเช้าใจคือ โปรแกรมของผมสามารถดึงข้อมูลเข้ามาได้แล้ว เชื่อมต่อกับฐานข้อมูลแล้ว แล้วทำไมมันถึงเพิ่มข้อมูลไม่ได้สักที
คอลัมม์ มีทั้งหมด 18 คอลัมม์ ผมใช้คำสงวนตรงไหนรึป่าว ช่วยบอกผมหน่อยนะคับ ติดมา อาทิตย์กว่าแล้ว ขอบคุณคับ
อันนี้รูปในหน้า tbData.xsd

Tag : .NET, Ms Access, VB.NET, VS 2005 (.NET 2.x), VS 2008 (.NET 3.x)
|
|
 |
 |
 |
 |
Date :
2011-06-01 05:53:47 |
By :
oniza |
View :
1809 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่ค่า Values เข้าไปให้เหมือนกับที่ทำได้จาก Query builder นะครับ ตัวอย่าง Values (?,?,?,?,?,....n) ใส่เครื่องหมาย ? ให้เท่ากับจำนวน field ข้อมูลของเราครับผม ลองดูนะครับ
|
 |
 |
 |
 |
Date :
2011-06-01 09:47:35 |
By :
woraponc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รอดูผล ยังงงอยู่
|
ประวัติการแก้ไข 2011-06-01 10:22:55 2011-06-01 10:23:30
 |
 |
 |
 |
Date :
2011-06-01 10:18:59 |
By :
mardede |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ สามารถ INSERT ข้อมูลได้แล้วคับ จุดผิดมันอยู่ตรง Note คับ คอลัมม์สุดท้ายของผม
ซึ่งเป็นการแก้ไขแบบอังเอิญมากคับ พอดีนอนฝันตอนบ่ายว่าทำข้อมูลตรงไหนผิดบ้างเลย ไล่ดูในความฝัน
พอตื่นขึ้นมาผมก็ยังนึกอยู่ว่าคิดมากจนฝันเลย ยังหัวเราะตัวเองอยู่ แต่พอมากนั่งคิดถึงความฝันดูดี ๆ เลยเจอคับ
[note] มันต่างจากเพื่อน ดูตรงภาพที่ 2 คับ มันมีปีกกาแข็งครอบไว้ ซึ่งผมไม่ได้เป็นคนกำหนดมัน ผมเลยลองมาแก้ดู
แต่ว่าแก้ไม่ได้ มันไม่ยอมเปลี่ยน ผมเลย เข้าไปแก้ในฐานข้อมูล เปลี่ยนจาก note เป็น note_more
แล้วก็มาไล่แก้เป็น note_more ใน vb
แต่ว่า เจอข้อความใหม่
" The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or filelds that contain duplicte data, remove the index, or redefine the index to permit duplicate entries and try again. "
มันให้ผมลบอะไรนี้หล่ะ ตอนนี้ยังไม่รู้คับ ใครรู้ช่วยบอกทีนะ จะได้ทำเสร็จสักที โปรแกรมนี้ ขอบคุณคับ
|
 |
 |
 |
 |
Date :
2011-06-01 16:05:51 |
By :
oniza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันบอกว่า Key ซ้ำครับ คือคุณได้กำหนด Primary Key หรือ Unique Key แล้ว Insert ค่าซ้ำลงไปครับ 
|
 |
 |
 |
 |
Date :
2011-06-01 16:12:18 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืม. . . ผมเข้าใจแล้วคับท่าน mr.win แต่ว่า แล้วผมจะต้องทำยังไง คับ เวลาบันทึก จะได้ไม่หลงคีย์ข้อมูลซ้ำ
แบบ ว่าเตือนยังไง ประมาณนี้คับ ตอนนี้กำลัง หาอ่านวิธี เพิ่ม ID แบบให้มันเพิ่มเอง โดยที่เราไม่ต้องไปกำหนด
แบบว่าอ่านค่า เรคคอร์ด คอลัมม์ ID ตัวสุดท้าย แล้วเอามา + 1 ให้ เรคคอร์ด คอลัมม์ใหม่ ประมาณนี้
แต่ว่าถ้าลบระหว่างเรคคอร์ด เช่น 4 5 6 7 เราลบ 5 แล้ว อยากให้ 6 เปลี่ยนเป็น 5 6 เป็นลำดับไปคับ
ทำยังไง
|
 |
 |
 |
 |
Date :
2011-06-01 17:30:35 |
By :
oniza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ID ถ้ามันถูกลบก็ข้ามไปเลยครับ ไม่ต้องสนใจครับ
|
 |
 |
 |
 |
Date :
2011-06-01 17:39:35 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Public Class frmCustomer
Dim strConn As String
Dim Conn As New OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dt As DataTable
Dim dr As DataRow
Dim CurrentCustomers As Integer
Dim IsFind As Boolean = False
Dim btnChk As String
Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strconn As String
txtID.Enabled = False
btnSave.Enabled = False
btnCancel.Enabled = False
btnUpdate.Enabled = False
btnDetele.Enabled = False
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\WindowsApplication3\WindowsApplication3\tblCustomer.accdb"
With Conn 'เริ่มต้นการเชื่อมต่อฐานข้อมูล
If .State = ConnectionState.Open Then Close()
.ConnectionString = strconn
.Open() 'เชื่อมต่อกับฐานข้อมูล
End With
ShowAllCustomers()
FormatAllCustomers()
End Sub
Private Sub FormatAllCustomers()
Dim cs As New DataGridViewCellStyle()
cs.Font = New Font("Ms Sans Serif", 10, FontStyle.Bold)
With dgvAIICustomer
.ColumnHeadersDefaultCellStyle = cs
.Columns(0).HeaderText = "รหัสพนักงาน"
.Columns(1).HeaderText = "ชื่อพนักงาน"
.Columns(2).HeaderText = "ตำแหน่ง"
.Columns(3).HeaderText = "เพศ"
.Columns(4).HeaderText = "เงินเดือน"
.Columns(0).Width = 50
.Columns(1).Width = 100
.Columns(2).Width = 100
.Columns(3).Width = 100
.Columns(4).Width = 100
End With
End Sub
Private Sub ShowAllCustomers()
Dim sqlCust As String
sqlCust = "SELECT * "
sqlCust &= "FROM tblCustomer"
If IsFind = True Then
ds.Tables("tblCustomer").Clear()
End If
da = New OleDbDataAdapter(sqlCust, Conn)
da.Fill(ds, "tblCustomer")
If ds.Tables("tblCustomer").Rows.Count <> 0 Then
IsFind = True
With dgvAIICustomer
.ReadOnly = True
.DataSource = ds.Tables("tblCustomer")
End With
Else
IsFind = False
End If
End Sub
Private Sub dgvAllCusoumers_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAIICustomer.CellContentClick, dgvAIICustomer.CellContentClick
If e.RowIndex = dgvAIICustomer.Rows.Count - 1 Then
Exit Sub
End If
With dgvAIICustomer
txtID.Text = .Rows.Item(e.RowIndex).Cells(0).Value.ToString()
txtName.Text = .Rows.Item(e.RowIndex).Cells(1).Value.ToString()
txtPosition.Text = .Rows.Item(e.RowIndex).Cells(2).Value.ToString()
txtSex.Text = .Rows.Item(e.RowIndex).Cells(3).Value.ToString()
txtSalary.Text = .Rows.Item(e.RowIndex).Cells(4).Value.ToString()
End With
btnUpdate.Enabled = True
btnDetele.Enabled = True
End Sub
Private Sub btnInsert_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
btnChk = "Add"
btnInsert.Enabled = False
btnUpdate.Enabled = False
btnDetele.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
txtID.Text = ""
txtName.Text = ""
txtPosition.Text = ""
txtSex.Text = ""
txtSalary.Text = ""
txtName.Focus()
End Sub
Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
btnChk = "Update" 'เก็บค่าการคลิกปุ่ม โดยให้มีค่าเป็น Update
btnInsert.Enabled = False 'ปุ่ม เพิ่ม เปิดการทำงาน
btnUpdate.Enabled = False 'ปุ่ม แก้ไข เปิดการทำงาน
btnDetele.Enabled = False 'ปุ่ม ลบ เปิดการทำงาน
btnSave.Enabled = True 'ปุ่ม ตกลง เปิดการทำงาน
btnCancel.Enabled = True 'ปุ่ม ยกเลิก เปิดการทำงาน
End Sub
Private Sub btnDetele_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetele.Click
Dim sqlDelete As String
Dim comAdd As New OleDbCommand
If MessageBox.Show("ต้องการลบข้อมูล", "ยืนยันการลบ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
sqlDelete = "Delete From tblCustomer"
sqlDelete &= " WHERE (CustID=" & CInt(txtID.Text) & ")"
With comAdd
.CommandType = CommandType.Text
.CommandText = sqlDelete
.Connection = Conn
.ExecuteNonQuery()
End With
ShowAllCustomers()
End If
End Sub
Private Sub btnSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlAdd As String
Dim sqlEdit As String
Dim comAdd As New OleDbCommand
Select Case btnChk 'ตรวจสอบการคลิกปุ่ม
Case "Add" 'กรณีถ้าคลิกปุ่ม เพิ่ม
'ตรวจสอบการป้อนข้อมูลว่าครบถ้วนหรือไม่
If (txtName.Text = "") Or (txtPosition.Text = "") Or (txtSex.Text = "") Or (txtSalary.Text = "") Then
MsgBox("โปรดป้อนข้อมูลให้ครบ")
txtName.Focus()
Exit Sub
End If
'คำสั่ง SQL เพื่อแทรกข้อมูลในตาราง tblCustomer
sqlAdd = "Insert Into tblCustomer (CustName,Position,Sex,Salary)"
sqlAdd &= " VALUES ('" & txtName.Text & "',"
sqlAdd &= "'" & txtPosition.Text & "',"
sqlAdd &= "'" & txtSex.Text & "',"
sqlAdd &= "'" & txtSalary.Text & "')" 'การสั่งออปเจค comAdd ให้ทำงาน
With comAdd
.CommandType = CommandType.Text 'เซตประเภทคอมมาน
.CommandText = sqlAdd 'ใส่ชุดคำสั่ง SQL
.Connection = Conn 'บอกว่าจะไปทางไหน
.ExecuteNonQuery() 'ไปทำงานได้ (ส่งคำสั่งไปแบบไม่ต้องการผลลัพธ์คืนมา)
End With
MsgBox("บันทึกข้อมูลเรียบร้อย") 'แสดง MessageBox ว่าได้บันทึกข้อมูลแล้ว
ShowAllCustomers() 'แสดงข้อมูลใน DataGridView
Case "Update" 'กรณีถ้าคลิกปุ่ม แก้ไข
'คำสั่ง SQL ในการปรับปรุงข้อมูลในตาราง tblCustomer ตาม รหัสพนักงาน
sqlEdit = "UPDATE tblCustomer"
sqlEdit &= " SET CustName = '" & txtName.Text & "',"
sqlEdit &= " Position = '" & txtPosition.Text & "',"
sqlEdit &= " Sex = '" & txtSex.Text & "',"
sqlEdit &= " Salary = '" & txtSalary.Text & "'"
sqlEdit &= " WHERE (CustID = " & CInt(txtID.Text) & ")"
'การสั่งออปเจค comAdd ให้ทำงาน
With comAdd
.CommandType = CommandType.Text
.CommandText = sqlEdit
.Connection = Conn
.ExecuteNonQuery()
End With
MsgBox("แก้ไขข้อมูลเรียบร้อย") 'แสดง MessageBox ว่าได้แก้ไขข้อมูลแล้ว
ShowAllCustomers() 'แสดงข้อมูลใน DataGridView
Case Else
MsgBox("ไม่มีการทำงาน")
End Select
btnInsert.Enabled = True 'ปุ่ม เพิ่ม เปิดการทำงาน
btnUpdate.Enabled = True 'ปุ่ม แก้ไข เปิดการทำงาน
btnDelete.Enabled = True 'ปุ่ม ลบ เปิดการทำงาน
btnSave.Enabled = False 'ปุ่ม ตกลง เปิดการทำงาน
btnCancel.Enabled = False 'ปุ่ม ยกเลิก เปิดการทำงาน
End Sub
Private Sub btnCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
btnChk = ""
btnInsert.Enabled = True
btnUpdate.Enabled = True
btnDetele.Enabled = True
btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
End Class
ตรงInsert into บอกว่าไวยากรไม่ถูก
|
 |
 |
 |
 |
Date :
2012-10-02 17:39:18 |
By :
สส |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|