943,812 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 6386
  • VB.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 12th, 2008
0

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

Yes, that should do. But you have to use the same field name aliases in both procedures.

There's a SelectSQL parameter in both procedures, you can use it, for example:
VB.NET Syntax (Toggle Plain Text)
  1. Dim MappedSelect As String
  2. Dim oView As DataView
  3. MappedSelect = "FieldName1 AS NewName1, FieldName2 AS NewName2, FieldName3, FieldName4"
  4. ExportTableData(SourceConnectionString, SourceTableName, oView, MappedSelect, "")
  5. ImportTableData(TargetConnectionString, TargetTableName, oView, MappedSelect, "")
or
VB.NET Syntax (Toggle Plain Text)
  1. Dim MappedSelect As String
  2. Dim oView As DataView
  3. MappedSelect = "SrcFieldName1 AS NewName1, SrcFieldName2 AS NewName2, FieldName3, FieldName4"
  4. ExportTableData(SourceConnectionString, SourceTableName, oView, MappedSelect, "")
  5. MappedSelect = "DestFieldName1 AS NewName1, DestFieldName2 AS NewName2, FieldName3, FieldName4"
  6. ImportTableData(TargetConnectionString, TargetTableName, oView, MappedSelect, "")
In the latter case both different original field names are aliased to same names.

The point is, when dataviews are merged, they are merged by matching field names. As I probably stated before, there may be a better way to map field names. But the code above does the job anyway.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 15th, 2008
0

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

may i write this code on load....without changing the existing code.or i make changes accordingly.

VB.NET Syntax (Toggle Plain Text)
  1. MappedSelect = "SrcFieldName1 AS NewName1, SrcFieldName2 AS NewName2, FieldName3, FieldName4"
in this code do i have write from table name also????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 15th, 2008
0

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

Quote ...
may i write this code on load....without changing the existing code.or i make changes accordingly.
You may put the code on the Load event. Just make sure you "map" field names, if you need to do that, before calling ExportTableData and/or ImportTableData. The point is, as I wrote before, merging data view objects is based on field names being the same.
Quote ...
in this code do i have write from table name also????
No, you don't have to. See my first post for the implementation of ExportTableData and ImportTableData procedures, especially their ByVal SelectSQL As String argument how it is used in the code.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 15th, 2008
0

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

sir not able to implement.....things ant working
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 15th, 2008
0

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

Plz see my code(EXPORT)
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Public Sub ExportTableData(ByVal ConnectionStr As String, ByVal TableName As String, _
  3. ByRef oView As DataView, _
  4. ByVal SelectSQL As String, ByVal WhereSQL As String)
  5.  
  6. 'Export SQL table to data view
  7. Try
  8. strSQL = ""
  9. oCmd = Nothing
  10. oDA = Nothing
  11. oConn = New SqlConnection("server=192.9.200.82;database=eKalyan;user id=invite;password=invite")
  12. oCmd = oConn.CreateCommand()
  13. If SelectSQL.Length = 0 Then
  14. strSQL = "SELECT newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode FROM " & "ekalyan.welpen"
  15. 'strSQL = "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"
  16. Else
  17. strSQL = "SELECT " & SelectSQL & " FROM " & "ekalyan.welpen"
  18. End If
  19. If WhereSQL.Length > 0 Then
  20. strSQL = strSQL & " WHERE " & WhereSQL
  21. End If
  22.  
  23. 'strSQL = "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"
  24. oCmd.CommandText = strSQL
  25. oDA = New SqlDataAdapter(oCmd)
  26. oRS = New DataSet
  27. oDA.Fill(oRS, "ekalyan.welpen")
  28. oView = New DataView(oRS.Tables(0))
  29. oConnOleDb.Close()
  30. oConn = Nothing
  31. oDA = Nothing
  32. oRS = Nothing
  33. oCmd = Nothing
  34.  
  35. 'ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "", "")
  36. MessageBox.Show("data exported Successfully")
  37.  
  38.  
  39. Catch ex As Exception
  40.  
  41. End Try
  42.  
  43.  
  44.  
  45. End Sub

for import

VB.NET Syntax (Toggle Plain Text)
  1. Public Sub ImportTableData(ByVal ConnectionStr As String, ByVal welfare_pen As String, _
  2. ByRef oImportView As DataView, _
  3. ByVal SelectSQL As String, ByVal WhereSQL As String)
  4.  
  5. Try
  6.  
  7.  
  8. strSQL1 = ""
  9. oConnOleDb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb")
  10. oConnOleDb.Open()
  11. oCmdOleDb = Nothing
  12. oDAOleDb = Nothing
  13. oCmdOleDb = oConnOleDb.CreateCommand()
  14. If SelectSQL.Length = 0 Then
  15. strSQL1 = "SELECT newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode FROM " & "welfare_pen"
  16. Else
  17. strSQL = "SELECT " & SelectSQL & " FROM " & welfare_pen
  18.  
  19. End If
  20. If WhereSQL.Length > 0 Then
  21.  
  22.  
  23. strSQL1 = strSQL1 & " WHERE " & WhereSQL
  24. End If
  25. strSQL1 = "SELECT * FROM welfare_pen"
  26. oCmdOleDb.CommandText = strSQL1
  27. oDAOleDb = New OleDbDataAdapter(oCmdOleDb)
  28. oRS1 = New DataSet
  29. oDAOleDb.Fill(oRS1, "welfare_pen")
  30. oView = New DataView(oRS.Tables(0))
  31. oView.Table.Merge(oImportView.ToTable, True, MissingSchemaAction.Ignore)
  32. Dim oCmdBuilder As OleDbCommandBuilder
  33. oCmdBuilder = New OleDbCommandBuilder(oDAOleDb)
  34. oDAOleDb.Update(oView.Table)
  35. oConnOleDb.Close()
  36. oConnOleDb = Nothing
  37. oDAOleDb = Nothing
  38. oRS1 = Nothing
  39. oCmdOleDb = Nothing
  40.  
  41. 'ImportTableData("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb", "welfare_pen", oView, "", "")
  42. MessageBox.Show("data Imported Successfully")
  43. DisplayCustomerCount()
  44.  
  45. Catch ex As Exception
  46.  
  47. End Try
  48.  
  49.  
  50. End Sub

on export button click


VB.NET Syntax (Toggle Plain Text)
  1. MappedSelect = "SELECT newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode FROM " & "ekalyan.welpen"
  2.  
  3.  
  4. ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "MappedSelect", "")
  5. DisplayrecordCount()


same for import
Last edited by cscgal; Dec 15th, 2008 at 6:11 pm. Reason: Added missing end-code tag
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 15th, 2008
0

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

Both procedures contained parameters for
1) connection strings (ConnectionStr)
2) table names (TableName)
3) field names (SelectSQL)
the reason for this was (or should have been) obvious
Quote ...
MappedSelect = "SELECT newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode FROM " & "ekalyan.welpen"
VB.NET Syntax (Toggle Plain Text)
  1. MappedSelect = "newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode "
i.e. just field names and/or aliases, no SQL statements, procedures will handle them.
Quote ...
ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "MappedSelect", "")
DisplayrecordCount()
VB.NET Syntax (Toggle Plain Text)
  1. ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, MappedSelect, "")
"MappedSelect" should be MappedSelect of course to pass the value to procedure.

Put the breakpoint in the lines oCmdOleDb.CommandText = strSQL and oCmd.CommandText = strSQL , dump the strSQL variables and compare the field lists. Field names (or aliases of the fields) should be same on both procedures.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 16th, 2008
0

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

did not undrestand these lines
Put the breakpoint in the lines oCmdOleDb.CommandText = strSQL and oCmd.CommandText = strSQL , dump the strSQL variables and compare the field lists.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 16th, 2008
0

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

Forget that If you remove SELECT and FROM from the MappedSelect variable (procedures will handle those), pass it (without quotes!) to both (unmodified, original) procedures, does it work? If not, compare the value of the strSQL variable from both export and import procedures (same number of fields and same names). If you get some error message, post it here.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Dec 16th, 2008
0

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

sir i have a confusion probably...we have used MappedSelect...for alising.that means i have to replace the vareiable strSQL with MappedSelect???
and remove the statement
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. strSQL1 = "SELECT newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode FROM " & "welfare_pen"

with
VB.NET Syntax (Toggle Plain Text)
  1. MappedSelect = "newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode "MappedSelect = "newuniqueid as UNIQ_ID,newteh_code as TEH_CD,newpcate_cd as PCATE_CD,newplano as PLA_NO, pen_nm,rel_cd,pen_fnm,ccate_cd,pan_code as PAN_CD,pvillage as VILLAGE,ppo_cd as PO_CD,spo_cd,start_qtr,last_qtr,bundle_typ,bundle_no,mo_srno,pen_amt,pen_comm,status,operatorcode "


please help me out....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kritiohri is offline Offline
24 posts
since Nov 2008
Dec 22nd, 2008
0

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

sir....not able to do aliasing..if any help it would be highly appreciated.
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