R
rsine
I am using Excel as a small database to store settings of a comm program
I am writing. While I can read from excel fine, I am stuck on how to
write any changes back to the database. I have been trying to use an
INSERT INTO but I get the following error:
Operation must use an updateable query
I am really new with using ADO.Net and this is my first application
trying to use Excel as a datasource. Any guidance or suggestions on how
to go about fixing my error would be appreciated.
The code I am using is as follows:
'clear the dataset
dsSettings.Tables("Settings").Clear()
'insert a new row into the datatable
Dim dtSettings As DataTable = dsSettings.Tables("Settings")
Dim drNewRow As DataRow = dtSettings.NewRow
drNewRow.Item("Baudrate") = sBaudRate
drNewRow.Item("Parity") = sParity
drNewRow.Item("Handshake") = sHandShake
drNewRow.Item("Databits") = sDataBits
drNewRow.Item("Stopbits") = sStopBits
'add new row to Settings table
dtSettings.Rows.Add(drNewRow)
Dim sSQL As String
sSQL = "INSERT INTO [Settings$] "
sSQL = sSQL & "(Baudrate,"
sSQL = sSQL & "Parity,"
sSQL = sSQL & "Handshake,"
sSQL = sSQL & "Databits,"
sSQL = sSQL & "Stopbits) "
sSQL = sSQL & "VALUES ("
sSQL = sSQL & "@Baudrate,"
sSQL = sSQL & "@Parity,"
sSQL = sSQL & "@Handshake,"
sSQL = sSQL & "@Databits,"
sSQL = sSQL & "@Stopbits)"
Dim mExcelConnection As OleDb.OleDbConnection = New
OleDb.OleDbConnection
mExcelConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sAppPath &
"Settings.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim mExcelAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
mExcelAdapter.InsertCommand = New OleDb.OleDbCommand(sSQL,
mExcelConnection)
mExcelAdapter.InsertCommand.Parameters.Add("@Baudrate",
System.Data.OleDb.OleDbType.VarChar, 10, "Baudrate")
mExcelAdapter.InsertCommand.Parameters.Add("@Parity",
System.Data.OleDb.OleDbType.VarChar, 10, "Parity")
mExcelAdapter.InsertCommand.Parameters.Add("@Handshake",
System.Data.OleDb.OleDbType.VarChar, 10, "Handshake")
mExcelAdapter.InsertCommand.Parameters.Add("@Databits",
System.Data.OleDb.OleDbType.VarChar, 10, "Databits")
mExcelAdapter.InsertCommand.Parameters.Add("@Stopbits",
System.Data.OleDb.OleDbType.VarChar, 10, "Stopbits")
mExcelConnection.Open()
mExcelAdapter.Update(dsSettings, "Settings")
mExcelConnection.Close()
I am writing. While I can read from excel fine, I am stuck on how to
write any changes back to the database. I have been trying to use an
INSERT INTO but I get the following error:
Operation must use an updateable query
I am really new with using ADO.Net and this is my first application
trying to use Excel as a datasource. Any guidance or suggestions on how
to go about fixing my error would be appreciated.
The code I am using is as follows:
'clear the dataset
dsSettings.Tables("Settings").Clear()
'insert a new row into the datatable
Dim dtSettings As DataTable = dsSettings.Tables("Settings")
Dim drNewRow As DataRow = dtSettings.NewRow
drNewRow.Item("Baudrate") = sBaudRate
drNewRow.Item("Parity") = sParity
drNewRow.Item("Handshake") = sHandShake
drNewRow.Item("Databits") = sDataBits
drNewRow.Item("Stopbits") = sStopBits
'add new row to Settings table
dtSettings.Rows.Add(drNewRow)
Dim sSQL As String
sSQL = "INSERT INTO [Settings$] "
sSQL = sSQL & "(Baudrate,"
sSQL = sSQL & "Parity,"
sSQL = sSQL & "Handshake,"
sSQL = sSQL & "Databits,"
sSQL = sSQL & "Stopbits) "
sSQL = sSQL & "VALUES ("
sSQL = sSQL & "@Baudrate,"
sSQL = sSQL & "@Parity,"
sSQL = sSQL & "@Handshake,"
sSQL = sSQL & "@Databits,"
sSQL = sSQL & "@Stopbits)"
Dim mExcelConnection As OleDb.OleDbConnection = New
OleDb.OleDbConnection
mExcelConnection.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sAppPath &
"Settings.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim mExcelAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
mExcelAdapter.InsertCommand = New OleDb.OleDbCommand(sSQL,
mExcelConnection)
mExcelAdapter.InsertCommand.Parameters.Add("@Baudrate",
System.Data.OleDb.OleDbType.VarChar, 10, "Baudrate")
mExcelAdapter.InsertCommand.Parameters.Add("@Parity",
System.Data.OleDb.OleDbType.VarChar, 10, "Parity")
mExcelAdapter.InsertCommand.Parameters.Add("@Handshake",
System.Data.OleDb.OleDbType.VarChar, 10, "Handshake")
mExcelAdapter.InsertCommand.Parameters.Add("@Databits",
System.Data.OleDb.OleDbType.VarChar, 10, "Databits")
mExcelAdapter.InsertCommand.Parameters.Add("@Stopbits",
System.Data.OleDb.OleDbType.VarChar, 10, "Stopbits")
mExcelConnection.Open()
mExcelAdapter.Update(dsSettings, "Settings")
mExcelConnection.Close()