Страница: 1 |
|
Вопрос: Как обновить данные в БД Access из VS 2005 (VB.
|
Добавлено: 08.07.08 22:30
|
|
Автор вопроса: den
|
Всех приветствую. Поможите кто чем может :(
Работаю в VS 2005. Пишу приложение на VB.net к БД Access.
Как записать изменения в БД?
Есть форма. В форме есть DataGridview.
Заполняю Dataset. Таблицу из Dataset привязываю к DataGridview.
В форме вношу руками изменения (напр., добавляю строку).
Жму кнопку "Обновить":
....
Try
ndop.Update(ds)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
.....
ndop - OleDbDataAdapter;
ds - Dataset.
Код Insert для DataAdapter следующий:
With ndop
.InsertCommand = New OleDbCommand()
With .InsertCommand
.Connection = connect
.CommandText = "INSERT INTO Size_doplats(ID, Size) VALUES(?, ?)" 'не получается ни так...
'.CommandText = "INSERT INTO `Size_doplats` (`Size`) VALUES (?)" 'ни так...
.UpdatedRowSource = UpdateRowSource.Both
End With
End With
Пишет ошибку:
system.Data.Oledb.OledbException: Ошибка синтаксиса в инструкции INSERT INTO....
Ответить
|
Номер ответа: 3 Автор ответа: den
Вопросов: 18 Ответов: 14
|
Профиль | | #3
|
Добавлено: 09.07.08 21:20
|
Ok.
Привожу весь листинг формы:
Class Sdoplat
Inherits Spravochnik
Public connect As New OleDbConnection(connectionString)
Private Sub Sdoplat_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ndop 'OleDbDataAdapter
.SelectCommand = New OleDbCommand()
With .SelectCommand
.Connection = connect
.CommandText = "SELECT Size_doplats.* FROM Size_doplats"
End With
.InsertCommand = New OleDbCommand()
With .InsertCommand
.Connection = connect
.CommandText = "INSERT INTO Size_doplats(ID, Size) VALUES(?, ?)"
.UpdatedRowSource = UpdateRowSource.Both
End With
.UpdateCommand = New OleDbCommand()
With .UpdateCommand
.Connection = connect
.CommandText = "update Size_doplats set Size = ? where ID = ?"
End With
.DeleteCommand = New OleDbCommand()
With .DeleteCommand
.Connection = connect
.CommandText = "delete from Size_doplats where ID = ?"
End With
.AcceptChangesDuringFill = True
.TableMappings.Add("Table", "Size_doplats"
.MissingSchemaAction = MissingSchemaAction.AddWithKey
End With
Try
ds.Clear()
ndop.Fill(ds)
MyGridView.DataSource = ds.Tables("Size_doplats"
Catch ex As Exception
MsgBox("Error occured:" & ex.Message)
End Try
With ds.Tables("Size_doplats"
.Columns("Size".ColumnName = "Размер доплаты"
End With
With MyGridView
.AutoResizeColumns()
End With
End Sub
Private Sub mybut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut.Click
del()
End Sub
Public Overrides Sub del()
If MyGridView.SelectedRows.Count > 0 AndAlso Not MyGridView.SelectedRows(0).Index = _
MyGridView.Rows.Count - 1 Then
MyGridView.Rows.RemoveAt(MyGridView.SelectedRows(0).Index)
End If
End Sub
Private Sub mybut1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut1.Click
canc()
End Sub
Public Overrides Sub canc()
ds.RejectChanges()
End Sub
Private Sub mybut2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut2.Click
Save()
End Sub
Public Overrides Sub Save()
'ndop.InsertCommand.UpdatedRowSource = UpdateRowSource.Both
'ndop.Update(ds)
Try
ndop.Update(ds)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Текст ошибки:
System.Data.OleDb.OleDbException was caught
ErrorCode=-2147217900
Message="Ошибка синтаксиса в инструкции INSERT INTO."
Source="Microsoft JET Database Engine"
StackTrace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
at doplats.Sdoplat.Save() in G:\My eBooks\учёба\Доплаты\doplats\doplats\Sdoplat.vb:line 92
Ответить
|
Номер ответа: 4 Автор ответа: den
Вопросов: 18 Ответов: 14
|
Профиль | | #4
|
Добавлено: 09.07.08 21:22
|
Ok.
Привожу весь листинг формы:
Class Sdoplat
Inherits Spravochnik
Public connect As New OleDbConnection(connectionString)
Private Sub Sdoplat_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ndop 'OleDbDataAdapter
.SelectCommand = New OleDbCommand()
With .SelectCommand
.Connection = connect
.CommandText = "SELECT Size_doplats.* FROM Size_doplats"
End With
.InsertCommand = New OleDbCommand()
With .InsertCommand
.Connection = connect
.CommandText = "INSERT INTO Size_doplats(ID, Size) VALUES(?, ?)"
.UpdatedRowSource = UpdateRowSource.Both
End With
.UpdateCommand = New OleDbCommand()
With .UpdateCommand
.Connection = connect
.CommandText = "update Size_doplats set Size = ? where ID = ?"
End With
.DeleteCommand = New OleDbCommand()
With .DeleteCommand
.Connection = connect
.CommandText = "delete from Size_doplats where ID = ?"
End With
.AcceptChangesDuringFill = True
.TableMappings.Add("Table", "Size_doplats"
.MissingSchemaAction = MissingSchemaAction.AddWithKey
End With
Try
ds.Clear()
ndop.Fill(ds)
MyGridView.DataSource = ds.Tables("Size_doplats"
Catch ex As Exception
MsgBox("Error occured:" & ex.Message)
End Try
With ds.Tables("Size_doplats"
.Columns("Size".ColumnName = "Размер доплаты"
End With
With MyGridView
.AutoResizeColumns()
End With
End Sub
Private Sub mybut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut.Click
del()
End Sub
Public Overrides Sub del()
If MyGridView.SelectedRows.Count > 0 AndAlso Not MyGridView.SelectedRows(0).Index = _
MyGridView.Rows.Count - 1 Then
MyGridView.Rows.RemoveAt(MyGridView.SelectedRows(0).Index)
End If
End Sub
Private Sub mybut1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut1.Click
canc()
End Sub
Public Overrides Sub canc()
ds.RejectChanges()
End Sub
Private Sub mybut2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mybut2.Click
Save()
End Sub
Public Overrides Sub Save()
Try
ndop.Update(ds)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Текст ошибки:
System.Data.OleDb.OleDbException was caught
ErrorCode=-2147217900
Message="Ошибка синтаксиса в инструкции INSERT INTO."
Source="Microsoft JET Database Engine"
StackTrace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
at doplats.Sdoplat.Save() in G:\My eBooks\учёба\Доплаты\doplats\doplats\Sdoplat.vb:line 92
Ответить
|
Номер ответа: 10 Автор ответа: Старый
Вопросов: 6 Ответов: 13
|
Профиль | | #10
|
Добавлено: 26.07.08 00:35
|
Id не счетчик ли, его же тогда не надо добавлять ...
Ответить
|
Страница: 1 |
Поиск по форуму