| | |
how to import data from sql to ms access in vb.net through coding
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
or
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.
There's a SelectSQL parameter in both procedures, you can use it, for example:
VB.NET Syntax (Toggle Plain Text)
Dim MappedSelect As String Dim oView As DataView MappedSelect = "FieldName1 AS NewName1, FieldName2 AS NewName2, FieldName3, FieldName4" ExportTableData(SourceConnectionString, SourceTableName, oView, MappedSelect, "") ImportTableData(TargetConnectionString, TargetTableName, oView, MappedSelect, "")
VB.NET Syntax (Toggle Plain Text)
Dim MappedSelect As String Dim oView As DataView MappedSelect = "SrcFieldName1 AS NewName1, SrcFieldName2 AS NewName2, FieldName3, FieldName4" ExportTableData(SourceConnectionString, SourceTableName, oView, MappedSelect, "") MappedSelect = "DestFieldName1 AS NewName1, DestFieldName2 AS NewName2, FieldName3, FieldName4" ImportTableData(TargetConnectionString, TargetTableName, oView, MappedSelect, "")
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.
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 24
Reputation:
Solved Threads: 0
may i write this code on load....without changing the existing code.or i make changes accordingly.
in this code do i have write from table name also????
VB.NET Syntax (Toggle Plain Text)
MappedSelect = "SrcFieldName1 AS NewName1, SrcFieldName2 AS NewName2, FieldName3, FieldName4"
•
•
•
•
may i write this code on load....without changing the existing code.or i make changes accordingly.
•
•
•
•
in this code do i have write from table name also????
ByVal SelectSQL As String argument how it is used in the code. Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 24
Reputation:
Solved Threads: 0
Plz see my code(EXPORT)
for import
on export button click
same for import
VB.NET Syntax (Toggle Plain Text)
Public Sub ExportTableData(ByVal ConnectionStr As String, ByVal TableName As String, _ ByRef oView As DataView, _ ByVal SelectSQL As String, ByVal WhereSQL As String) 'Export SQL table to data view Try strSQL = "" oCmd = Nothing oDA = Nothing oConn = New SqlConnection("server=192.9.200.82;database=eKalyan;user id=invite;password=invite") oCmd = oConn.CreateCommand() If SelectSQL.Length = 0 Then 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" '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" Else strSQL = "SELECT " & SelectSQL & " FROM " & "ekalyan.welpen" End If If WhereSQL.Length > 0 Then strSQL = strSQL & " WHERE " & WhereSQL End If '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" oCmd.CommandText = strSQL oDA = New SqlDataAdapter(oCmd) oRS = New DataSet oDA.Fill(oRS, "ekalyan.welpen") oView = New DataView(oRS.Tables(0)) oConnOleDb.Close() oConn = Nothing oDA = Nothing oRS = Nothing oCmd = Nothing 'ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "", "") MessageBox.Show("data exported Successfully") Catch ex As Exception End Try End Sub
for import
VB.NET Syntax (Toggle Plain Text)
Public Sub ImportTableData(ByVal ConnectionStr As String, ByVal welfare_pen As String, _ ByRef oImportView As DataView, _ ByVal SelectSQL As String, ByVal WhereSQL As String) Try strSQL1 = "" oConnOleDb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb") oConnOleDb.Open() oCmdOleDb = Nothing oDAOleDb = Nothing oCmdOleDb = oConnOleDb.CreateCommand() If SelectSQL.Length = 0 Then 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" Else strSQL = "SELECT " & SelectSQL & " FROM " & welfare_pen End If If WhereSQL.Length > 0 Then strSQL1 = strSQL1 & " WHERE " & WhereSQL End If strSQL1 = "SELECT * FROM welfare_pen" oCmdOleDb.CommandText = strSQL1 oDAOleDb = New OleDbDataAdapter(oCmdOleDb) oRS1 = New DataSet oDAOleDb.Fill(oRS1, "welfare_pen") oView = New DataView(oRS.Tables(0)) oView.Table.Merge(oImportView.ToTable, True, MissingSchemaAction.Ignore) Dim oCmdBuilder As OleDbCommandBuilder oCmdBuilder = New OleDbCommandBuilder(oDAOleDb) oDAOleDb.Update(oView.Table) oConnOleDb.Close() oConnOleDb = Nothing oDAOleDb = Nothing oRS1 = Nothing oCmdOleDb = Nothing 'ImportTableData("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb", "welfare_pen", oView, "", "") MessageBox.Show("data Imported Successfully") DisplayCustomerCount() Catch ex As Exception End Try End Sub
on export button click
VB.NET Syntax (Toggle Plain Text)
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" ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "MappedSelect", "") DisplayrecordCount()
same for import
Last edited by cscgal; Dec 15th, 2008 at 6:11 pm. Reason: Added missing end-code tag
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
i.e. just field names and/or aliases, no SQL statements, procedures will handle them.
"MappedSelect" should be MappedSelect of course to pass the value to procedure.
Put the breakpoint in the lines
1) connection strings (ConnectionStr)
2) table names (TableName)
3) field names (SelectSQL)
the reason for this was (or should have been) obvious
•
•
•
•
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)
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 "
•
•
•
•
ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, "MappedSelect", "")
DisplayrecordCount()
VB.NET Syntax (Toggle Plain Text)
ExportTableData("server=192.9.200.82;database=eKalyan;user id=invite;password=invite", "ekalyan.welpen", oView, MappedSelect, "")
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. Teme64 @ Windows Developer Blog
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.
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. Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 24
Reputation:
Solved Threads: 0
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
with
please help me out....
and remove the statement
VB.NET Syntax (Toggle Plain Text)
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)
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....
![]() |
Similar Threads
- Simple ASP.Net Login Page using C# (C#)
- Updated : Simple ASP.Net Login Page (ASP.NET)
- ASP.Net Security 101 Part 1 (ASP.NET)
- Experienced C# .NET developer looking for offsite job! (Post your Resume)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
- ASP and SQL ? (MS SQL)
Other Threads in the VB.NET Forum
- Previous Thread: VB 2008 Express Compact SQL UPDATE query error
- Next Thread: displaying the values in the combobox
Views: 3369 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access advanced application array basic beginner browser button buttons center class click client code combo convert cuesent data database datagrid datagridview date datetimepicker design designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function html images lib listview map mobile module msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read refresh regex richtextbox right-to-left save search serial settings shutdown socket sorting sqldatbase sqlserver studio temperature textbox timer timespan transparency txttoxmlconverter usercontol validation vb vb.net vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms winsock wpf wrapingcode xml year





