 |
|
ช่วยดูโค้ด ปุ่มเพิ่มข้อมูลลงใน DataGridView ทีละแถวให้หน่อย มัน Error และควรจะแก้อย่างไร |
|
 |
|
|
 |
 |
|
Code (VB.NET)
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection if it is not yet open
con.Open()
End If
cmd.Connection = con
'add data to table
cmd.CommandText = "INSERT INTO tb_Out_Product(ID_Product,จำนวน,ชื่อคนยืม,ชื่อคนจ่าย,วันและเวลา)" & _
"VALUES (" & Me.ComboBox1.Text & ",'" & Me.TextBox3.Text & "','" & _
Me.TextBox4.Text & "','" & Me.TextBox5.Text & "','" & _
Me.DateTimePicker1.Text & "')"
cmd.ExecuteNonQuery()
'close connection
con.Close()
มันErrorตรง cmd.ExecuteNonQuery() ว่า
Code
Syntax error (missing operator) in query expression '202ส้ม'.
Tag : .NET, Ms Access, VB.NET, VS 2012 (.NET 4.x)
|
ประวัติการแก้ไข 2015-07-10 22:14:13
|
 |
 |
 |
 |
Date :
2015-07-10 22:12:25 |
By :
peemes101 |
View :
937 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รู้สึกว่าตรง VALUES ลืม ' นะVALUES ('" & Me.ComboBox1.Text & "','" &
|
 |
 |
 |
 |
Date :
2015-07-10 22:44:03 |
By :
โทรศัพท์พิมยาก |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังerror ตรง cmd.ExecuteNonQuery() อยู่ดี
|
 |
 |
 |
 |
Date :
2015-07-11 00:01:57 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองสั่ง
Msgbox(cmd.CommandText)
|
 |
 |
 |
 |
Date :
2015-07-11 08:18:39 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก่อนหน้านี้ได้ผ่านการเช็คยังครับว่า มี หรือ ไม่มี ข้อมูล ID_Product นี้อยู่
รูปแบบการบันทึก database
check ID ....
if have ID then
Update
else
insert
end if
ในการบึนทึกข้อมูลแต่ละครั้งควรจะมีรูปแบบประมาณนั้ครับ(แล้วแต่ว่าแต่ละคนจะประยุคต์แบบไหน)
ยกตัวอย่างจาก data wizard ครับ
Code (C#)
Private Sub InitAdapter()
Me._adapter = New Global.System.Data.OleDb.OleDbDataAdapter()
Dim tableMapping As Global.System.Data.Common.DataTableMapping = New Global.System.Data.Common.DataTableMapping()
tableMapping.SourceTable = "Table"
tableMapping.DataSetTable = "Table1"
tableMapping.ColumnMappings.Add("ID", "ID")
tableMapping.ColumnMappings.Add("type", "type")
tableMapping.ColumnMappings.Add("count", "count")
Me._adapter.TableMappings.Add(tableMapping)
Me._adapter.DeleteCommand = New Global.System.Data.OleDb.OleDbCommand()
Me._adapter.DeleteCommand.Connection = Me.Connection
Me._adapter.DeleteCommand.CommandText = "DELETE FROM `Table1` WHERE ((`ID` = ?) AND ((? = 1 AND `type` IS NULL) OR (`type`" & _
" = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?)))"
Me._adapter.DeleteCommand.CommandType = Global.System.Data.CommandType.Text
Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_ID", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "ID", Global.System.Data.DataRowVersion.Original, False, Nothing))
Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, True, Nothing))
Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, False, Nothing))
Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, True, Nothing))
Me._adapter.DeleteCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, False, Nothing))
Me._adapter.InsertCommand = New Global.System.Data.OleDb.OleDbCommand()
Me._adapter.InsertCommand.Connection = Me.Connection
Me._adapter.InsertCommand.CommandText = "INSERT INTO `Table1` (`type`, `count`) VALUES (?, ?)"
Me._adapter.InsertCommand.CommandType = Global.System.Data.CommandType.Text
Me._adapter.InsertCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Current, False, Nothing))
Me._adapter.InsertCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Current, False, Nothing))
Me._adapter.UpdateCommand = New Global.System.Data.OleDb.OleDbCommand()
Me._adapter.UpdateCommand.Connection = Me.Connection
Me._adapter.UpdateCommand.CommandText = "UPDATE `Table1` SET `type` = ?, `count` = ? WHERE ((`ID` = ?) AND ((? = 1 AND `ty" & _
"pe` IS NULL) OR (`type` = ?)) AND ((? = 1 AND `count` IS NULL) OR (`count` = ?))" & _
")"
Me._adapter.UpdateCommand.CommandType = Global.System.Data.CommandType.Text
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Current, False, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Current, False, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_ID", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "ID", Global.System.Data.DataRowVersion.Original, False, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, True, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_type", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "type", Global.System.Data.DataRowVersion.Original, False, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("IsNull_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, True, Nothing))
Me._adapter.UpdateCommand.Parameters.Add(New Global.System.Data.OleDb.OleDbParameter("Original_count", Global.System.Data.OleDb.OleDbType.[Integer], 0, Global.System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte), "count", Global.System.Data.DataRowVersion.Original, False, Nothing))
End Sub
Code (C#)
Public Overridable Overloads Function GetData() As ฐานข้อมูล1DataSet.Table1DataTable
Me.Adapter.SelectCommand = Me.CommandCollection(0)
Dim dataTable As ฐานข้อมูล1DataSet.Table1DataTable = New ฐานข้อมูล1DataSet.Table1DataTable()
Me.Adapter.Fill(dataTable)
Return dataTable
End Function
Public Overridable Overloads Function Delete(ByVal Original_ID As Integer, ByVal Original_type As Global.System.Nullable(Of Integer), ByVal Original_count As Global.System.Nullable(Of Integer)) As Integer
Me.Adapter.DeleteCommand.Parameters(0).Value = CType(Original_ID, Integer)
If (Original_type.HasValue = True) Then
Me.Adapter.DeleteCommand.Parameters(1).Value = CType(0, Object)
Me.Adapter.DeleteCommand.Parameters(2).Value = CType(Original_type.Value, Integer)
Else
Me.Adapter.DeleteCommand.Parameters(1).Value = CType(1, Object)
Me.Adapter.DeleteCommand.Parameters(2).Value = Global.System.DBNull.Value
End If
If (Original_count.HasValue = True) Then
Me.Adapter.DeleteCommand.Parameters(3).Value = CType(0, Object)
Me.Adapter.DeleteCommand.Parameters(4).Value = CType(Original_count.Value, Integer)
Else
Me.Adapter.DeleteCommand.Parameters(3).Value = CType(1, Object)
Me.Adapter.DeleteCommand.Parameters(4).Value = Global.System.DBNull.Value
End If
Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.DeleteCommand.Connection.State
If ((Me.Adapter.DeleteCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
<> Global.System.Data.ConnectionState.Open) Then
Me.Adapter.DeleteCommand.Connection.Open()
End If
Try
Dim returnValue As Integer = Me.Adapter.DeleteCommand.ExecuteNonQuery
Return returnValue
Finally
If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
Me.Adapter.DeleteCommand.Connection.Close()
End If
End Try
End Function
Public Overridable Overloads Function Insert(ByVal type As Global.System.Nullable(Of Integer), ByVal count As Global.System.Nullable(Of Integer)) As Integer
If (type.HasValue = True) Then
Me.Adapter.InsertCommand.Parameters(0).Value = CType(type.Value, Integer)
Else
Me.Adapter.InsertCommand.Parameters(0).Value = Global.System.DBNull.Value
End If
If (count.HasValue = True) Then
Me.Adapter.InsertCommand.Parameters(1).Value = CType(count.Value, Integer)
Else
Me.Adapter.InsertCommand.Parameters(1).Value = Global.System.DBNull.Value
End If
Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.InsertCommand.Connection.State
If ((Me.Adapter.InsertCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
<> Global.System.Data.ConnectionState.Open) Then
Me.Adapter.InsertCommand.Connection.Open()
End If
Try
Dim returnValue As Integer = Me.Adapter.InsertCommand.ExecuteNonQuery
Return returnValue
Finally
If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
Me.Adapter.InsertCommand.Connection.Close()
End If
End Try
End Function
Public Overridable Overloads Function Update(ByVal type As Global.System.Nullable(Of Integer), ByVal count As Global.System.Nullable(Of Integer), ByVal Original_ID As Integer, ByVal Original_type As Global.System.Nullable(Of Integer), ByVal Original_count As Global.System.Nullable(Of Integer)) As Integer
If (type.HasValue = True) Then
Me.Adapter.UpdateCommand.Parameters(0).Value = CType(type.Value, Integer)
Else
Me.Adapter.UpdateCommand.Parameters(0).Value = Global.System.DBNull.Value
End If
If (count.HasValue = True) Then
Me.Adapter.UpdateCommand.Parameters(1).Value = CType(count.Value, Integer)
Else
Me.Adapter.UpdateCommand.Parameters(1).Value = Global.System.DBNull.Value
End If
Me.Adapter.UpdateCommand.Parameters(2).Value = CType(Original_ID, Integer)
If (Original_type.HasValue = True) Then
Me.Adapter.UpdateCommand.Parameters(3).Value = CType(0, Object)
Me.Adapter.UpdateCommand.Parameters(4).Value = CType(Original_type.Value, Integer)
Else
Me.Adapter.UpdateCommand.Parameters(3).Value = CType(1, Object)
Me.Adapter.UpdateCommand.Parameters(4).Value = Global.System.DBNull.Value
End If
If (Original_count.HasValue = True) Then
Me.Adapter.UpdateCommand.Parameters(5).Value = CType(0, Object)
Me.Adapter.UpdateCommand.Parameters(6).Value = CType(Original_count.Value, Integer)
Else
Me.Adapter.UpdateCommand.Parameters(5).Value = CType(1, Object)
Me.Adapter.UpdateCommand.Parameters(6).Value = Global.System.DBNull.Value
End If
Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.UpdateCommand.Connection.State
If ((Me.Adapter.UpdateCommand.Connection.State And Global.System.Data.ConnectionState.Open) _
<> Global.System.Data.ConnectionState.Open) Then
Me.Adapter.UpdateCommand.Connection.Open()
End If
Try
Dim returnValue As Integer = Me.Adapter.UpdateCommand.ExecuteNonQuery
Return returnValue
Finally
If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
Me.Adapter.UpdateCommand.Connection.Close()
End If
End Try
End Function
ทำ Function ออกมาแบบนี้ก็ดูดีไปอีกแบบ
แต่ต้องออกตัวก่อนว่าข้อมูลข้างต้นมาจาก data wizard ล้วนๆผมไม่ได้คิดเอง 5555
|
 |
 |
 |
 |
Date :
2015-07-11 08:31:27 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|