In this blog you will find ASP.NET 3.5 articles and some code samples. These code samples are created by me and should be working. Thanks for visiting my blog! Happy Dot Netting

Thursday, August 20, 2009

Updating excel with ADO.NET


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim m_sConn1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\Vikas\AutoExcel\myexcel.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes"""
Dim conn2 As New OleDbConnection(m_sConn1)
Dim da As New OleDbDataAdapter("Select * From [Cities$]", conn2)
Dim ds As DataSet = New DataSet()
da.Fill(ds, "MyInventoryTable")

' Generate the UpdateCommand and add the parameters for the command.
da.UpdateCommand = New OleDbCommand( _
"UPDATE [Cities$] SET Qty = ?, Price=? WHERE Product = ?", conn2)
da.UpdateCommand.Parameters.Add("@Qty", OleDbType.Numeric).SourceColumn = "Qty"
da.UpdateCommand.Parameters.Add("@Price", OleDbType.Currency).SourceColumn = "Price"
da.UpdateCommand.Parameters.Add("@Product", OleDbType.VarChar, 255, "Product")

' Update the first two records.
ds.Tables(0).Rows(0)("Qty") = 1000
ds.Tables(0).Rows(0)("Price") = 10.1
ds.Tables(0).Rows(1)("Qty") = 2000
ds.Tables(0).Rows(1)("Price") = 20.2

' Apply the dataset changes to the actual data source (the workbook).
da.Update(ds, "MyInventoryTable")
conn2.Close()
End Sub

No comments: