Hi Friend,
Try the below functions to solve your scenario. One important thing which I want to say before starting is that, below code has written for SQL Server Database, it means you have to make the necessary changes in the code to make it compatible with MS Access. I hope you won't mind that....
Import the below namesapces:
CODE
Imports System.Data
Imports System.Data.SqlClient
This sub will delete the data from databse:
CODE
Public Sub DeleteData()
Dim sqlConString As String
sqlConString = "user id=<PROVIDE USER ID>;password=<PROVIDE PASSWORD>;Database=<PROVIDE DATABASE NAME>;server=<PROVIDE SERVER NAME>;"
Dim myConn As New SqlConnection(sqlConString)
Dim DtAdapter As New SqlDataAdapter()
Dim myDs As New DataSet()
Try
DtAdapter = New SqlDataAdapter("<WRITE YOUR SQL DELETE STATEMENT HERE>", myConn)
DtAdapter.Fill(myDs)
MessageBox.Show("Successfully Deleted", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch exp As Exception
MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
myConn.Close()
DtAdapter = Nothing
myDs = Nothing
End Try
End Sub
Below 3 functions will help you out in trasnfering data from excel to database:
CODE
Private Function GetConnection(ByVal fstrExcelFileName As String) As String
Return "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & fstrExcelFileName & ";" & _
"Extended Properties=""Excel 8.0;"""
End Function
CODE
Public Function GetDataFromExcel(ByVal foQuery As ArrayList, ByVal fstrDataSetName As String) As DataSet
Dim oMyData As OleDb.OleDbDataAdapter = Nothing
Dim oMyTable As DataTable = Nothing
Dim oMyDataSet As DataSet = Nothing
Dim strQuery As String
Dim oConn As OleDb.OleDbConnection = Nothing
Dim strXLFileName As String = "<PROVIDE YOUR XL FILE PATH HERE>"
Try
oConn = New OleDb.OleDbConnection()
oConn.ConnectionString = GetConnection(strXLFileName)
oConn.Open()
oMyDataSet = New DataSet()
If Not IsNothing(foQuery) AndAlso foQuery.Count > 0 Then
Dim iLoopIndex As Integer
Dim iLoopCount As Integer = foQuery.Count - 1
For iLoopIndex = 0 To iLoopCount
strQuery = foQuery(iLoopIndex)
oMyData = New OleDb.OleDbDataAdapter(strQuery, oConn)
oMyTable = New DataTable()
oMyTable.TableName = Replace(strQuery.Substring(strQuery.LastIndexOf(CChar("[")) + 1), "$]", "")
oMyData.Fill(oMyTable)
oMyDataSet.Tables.Add(AddRowNo(oMyTable))
Next
End If
oMyDataSet.DataSetName = fstrDataSetName
Return oMyDataSet
Catch oEx As Exception
Throw oEx
Finally
oMyTable = Nothing
oMyDataSet = Nothing
oMyData.Dispose()
oMyData = Nothing
oConn.Dispose()
oConn = Nothing
End Try
End Function
CODE
Private Function AddRowNo(ByVal foTable As DataTable) As DataTable
Try
Dim iLoopIndex As Integer
Dim iLoopCount As Integer = foTable.Rows.Count - 1
For iLoopIndex = 0 To iLoopCount
foTable.Rows(iLoopIndex).Item("ROW_NO") = iLoopIndex + 2
Next
Return foTable
Catch oEx As Exception
Throw oEx
End Try
End Function
I hope this will be usefull to you....
Get back to me for any Clarification....
This post has been edited by dineeshd: 1 Jul, 2008 - 07:30 AM