how to import data from sql to ms access in vb.net through coding

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

how to import data from sql to ms access in vb.net through coding

 
0
  #1
Nov 26th, 2008
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. connetionString = ("server=192.9.200.82;database=eKalyan;user id=invite;password=invite")
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. MessageBox.Show("connection established")
  13. sql = ("select newuniqueid,newteh_code,newpcate_cd,newplano, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code,pvillage,ppo_cd,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode from ekalyan.welpen")
  14.  
  15. cnn = New SqlConnection(connetionString)
  16. Try
  17. cnn.Open()
  18. cmdd = New SqlCommand(sql, cnn)
  19. cmdd.ExecuteNonQuery()
  20.  
  21. cnn.Close()
  22. MsgBox(" ExecuteNonQuery in SqlCommand executed !!")
  23.  
  24. Catch ex As Exception
  25. MsgBox("cannot open the connection")
  26. End Try
  27.  
  28. 'Dim objdataadapter1 As New SqlDataAdapter("select Teh_code,Pcate_Cd,Wtng_No,Applicationdate,Pen_Nm,Rel_Cd1,Pen_Fnm,pan_code,Po_Cd,disabilitypercentage,Ccate_Cd,IndiviualsIncome,Village from ekalyan.WtngN", objconnection)
  29.  
  30.  
  31.  
  32. End Sub
  33.  
  34. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  35. Try
  36. Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb")
  37. cn.Open()
  38.  
  39.  
  40. str = "insert into Table1(newuniqueid,newteh_code,newpcate_cd,newplano, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code,pvillage,ppo_cd,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode) values (@newuniqueid,@newteh_code,@newpcate_cd,@newplano, @pen_nm,@rel_cd,@pen_fnm,@ccate_cd,@pan_code,@pvillage,@ppo_cd,@spo_cd,@start_qtr,@last_qtr,@bundle_typ,@bundle_no,@mo_srno,@pen_amt,@pen_comm,@status,@operatorcode)IN 'C:\Table1.mdb'"
  41. cmd = New OleDbCommand(str, cn)
  42. icount = cmd.ExecuteNonQuery
  43. cn = Nothing
  44. MessageBox.Show(icount)
  45.  
  46. Catch
  47. End Try
  48. cn.Close()
  49. End Sub
  50. End Class
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #2
Nov 26th, 2008
I had a code which uses data view to transfer from Access to SQL Server. All you have to modify to transfer from SQL Server to Access, is to change OleDB* variables (objects) to Sql* variables (objects) and vice versa.

Read Access table to data view:
  1. Public Sub ExportTableData(ByVal ConnectionStr As String, ByVal TableName As String, _
  2. ByRef oView As DataView, _
  3. ByVal SelectSQL As String, ByVal WhereSQL As String)
  4. '
  5. ' Export Access table to data view
  6. Dim strSQL As String
  7. Dim oRS As DataSet
  8. Dim oConnOleDb As OleDbConnection
  9. Dim oCmdOleDb As OleDbCommand
  10. Dim oDAOleDb As OleDbDataAdapter
  11.  
  12. Try
  13. strSQL = ""
  14. oCmdOleDb = Nothing
  15. oDAOleDb = Nothing
  16. oConnOleDb = New OleDbConnection(ConnectionStr)
  17. oCmdOleDb = oConnOleDb.CreateCommand()
  18. If SelectSQL.Length = 0 Then
  19. strSQL = "SELECT * FROM " & TableName
  20. Else
  21. strSQL = "SELECT " & SelectSQL & " FROM " & TableName
  22. End If
  23. If WhereSQL.Length > 0 Then
  24. strSQL = strSQL & " WHERE " & WhereSQL
  25. End If
  26. ' Execute
  27. oCmdOleDb.CommandText = strSQL
  28. oDAOleDb = New OleDbDataAdapter(oCmdOleDb)
  29. oRS = New DataSet
  30. oDAOleDb.Fill(oRS, TableName)
  31. oView = New DataView(oRS.Tables(0))
  32. oConnOleDb.Close()
  33. oConnOleDb = Nothing
  34. oDAOleDb = Nothing
  35. oRS = Nothing
  36. oCmdOleDb = Nothing
  37. Catch ex As Exception
  38.  
  39. End Try
  40.  
  41. End Sub
And insert data view to SQL Server:
  1. Public Sub ImportTableData(ByVal ConnectionStr As String, ByVal TableName As String, _
  2. ByRef oImportView As DataView, _
  3. ByVal SelectSQL As String, ByVal WhereSQL As String)
  4. '
  5. ' Import table from data view
  6. Dim strSQL As String
  7. Dim oConn As SqlConnection
  8. Dim oCmd As SqlCommand
  9. Dim oDA As SqlDataAdapter
  10. Dim oRS As DataSet
  11. Dim oView As DataView
  12.  
  13. Try
  14. strSQL = ""
  15. oConn = New SqlConnection(ConnectionStr)
  16. oConn.Open()
  17. oCmd = Nothing
  18. oDA = Nothing
  19. oCmd = oConn.CreateCommand()
  20. If SelectSQL.Length = 0 Then
  21. strSQL = "SELECT * FROM " & TableName
  22. Else
  23. strSQL = "SELECT " & SelectSQL & " FROM " & TableName
  24. End If
  25. If WhereSQL.Length > 0 Then
  26. strSQL = strSQL & " WHERE " & WhereSQL
  27. End If
  28. ' Execute
  29. oCmd.CommandText = strSQL
  30. oDA = New SqlDataAdapter(oCmd)
  31. oRS = New DataSet
  32. oDA.Fill(oRS, TableName)
  33.  
  34. oView = New DataView(oRS.Tables(0))
  35. ' Set rowstates so that rows are inserted
  36. oView.Table.Merge(oImportView.ToTable, True, MissingSchemaAction.Ignore)
  37. ' The trick is to use CommandBuilder to create INSERT statement "automatically"
  38. Dim oCmdBuilder As SqlCommandBuilder
  39. oCmdBuilder = New SqlCommandBuilder(oDA)
  40. ' Finally call update to commit changes to database table
  41. oDA.Update(oView.Table)
  42.  
  43. oConn.Close()
  44. oConn = Nothing
  45. oDA = Nothing
  46. oRS = Nothing
  47. oCmd = Nothing
  48. Catch ex As Exception
  49.  
  50. End Try
  51.  
  52. End Sub
And here's how you use it
  1. Dim oView As DataView
  2. ExportTableData(SourceConnectionString, SourceTableName, oView, "", "")
  3. ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
Just provide connection strings and table name(s). Naturally you should handle errors properly etc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #3
Nov 27th, 2008
Where this code has to be written????
  1. Dim oView As DataView
  2. ExportTableData(SourceConnectionString, SourceTableName, oView, "", "")
  3. ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #4
Nov 27th, 2008
Okay, your Button1 reads data and Button2 writes data. If you want to keep it that way, set Dim oView As DataView as global variable in your class (form), like Private oView As DataView . Put ExportTableData(SourceConnectionString, SourceTableName, oView, "", "") in your Button1 and ImportTableData(TargetConnectionString, TargetTableName, oView, "", "") to Button2. Finally remember to modify my original code (see my previous reply for what and why) and provide proper values to connection strings and table names.

When I tested it (from SQL Server to Access in my case), I had this whole code
Dim oView As DataView
ExportTableData(SourceConnectionString, SourceTableName, oView, "", "")
ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
in one button's click event handler.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #5
Nov 29th, 2008
I want to ask when we are importing the data into sql why we are using select command in SQL.....it should only be used in MS access and in sql we should write a insert statement.....
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #6
Nov 29th, 2008
just want to thank you..for helping me out!!!!I don know if we can put messages like this but from my heart iam really thankful for your help.

I have got the records in MS access....For further help il look forward to you!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #7
Dec 1st, 2008
Hi! If your question is solved, please mark the thread as solved. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #8
Dec 8th, 2008
no doubts i have accomplished the task.I have given the names of coloumns in MS access same as that in the SQl table and then the data is imported successfully but i want to change the names of coloumns of ms access.can we do alising so that we can refer a coloumn name by another name also and getting the data imported successfully
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #9
Dec 8th, 2008
Yes, you can do that. Change "SELECT * FROM" to "SELECT field1 AS NewName1, field2 AS NewName2... FROM". That will work.

There might be a more sophisticated way to do column mapping, but can't get any good ideas straight away in to my mind.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: kritiohri is an unknown quantity at this point 
Solved Threads: 0
kritiohri kritiohri is offline Offline
Newbie Poster

Re: how to import data from sql to ms access in vb.net through coding

 
0
  #10
Dec 12th, 2008
do i need to write select fieldname as newfieldname ........in SQL part of the code only???coz i have tried and not able to import data
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC