943,853 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 6387
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2008
0

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

Expand Post »
VB.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Nov 26th, 2008
0

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

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:
VB.NET Syntax (Toggle Plain Text)
  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:
VB.NET Syntax (Toggle Plain Text)
  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
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 27th, 2008
0

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

Where this code has to be written????
VB.NET Syntax (Toggle Plain Text)
  1. Dim oView As DataView
  2. ExportTableData(SourceConnectionString, SourceTableName, oView, "", "")
  3. ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Nov 27th, 2008
0

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

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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Nov 29th, 2008
0

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

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.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Nov 29th, 2008
0

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

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!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 1st, 2008
0

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

Hi! If your question is solved, please mark the thread as solved. Thank you!
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 8th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 8th, 2008
0

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

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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 12th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How to Delete a Record by searching that record in two tables
Next Thread in VB.NET Forum Timeline: Please Help Me Urgent Crystal Report





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC