| | |
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:
Solved Threads: 0
VB.NET Syntax (Toggle Plain Text)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click connetionString = ("server=192.9.200.82;database=eKalyan;user id=invite;password=invite") MessageBox.Show("connection established") 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") cnn = New SqlConnection(connetionString) Try cnn.Open() cmdd = New SqlCommand(sql, cnn) cmdd.ExecuteNonQuery() cnn.Close() MsgBox(" ExecuteNonQuery in SqlCommand executed !!") Catch ex As Exception MsgBox("cannot open the connection") End Try '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) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb") cn.Open() 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'" cmd = New OleDbCommand(str, cn) icount = cmd.ExecuteNonQuery cn = Nothing MessageBox.Show(icount) Catch End Try cn.Close() End Sub End Class
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:
And insert data view to SQL Server:
And here's how you use it
Just provide connection strings and table name(s). Naturally you should handle errors properly etc.
Read Access table to data view:
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 Access table to data view Dim strSQL As String Dim oRS As DataSet Dim oConnOleDb As OleDbConnection Dim oCmdOleDb As OleDbCommand Dim oDAOleDb As OleDbDataAdapter Try strSQL = "" oCmdOleDb = Nothing oDAOleDb = Nothing oConnOleDb = New OleDbConnection(ConnectionStr) oCmdOleDb = oConnOleDb.CreateCommand() If SelectSQL.Length = 0 Then strSQL = "SELECT * FROM " & TableName Else strSQL = "SELECT " & SelectSQL & " FROM " & TableName End If If WhereSQL.Length > 0 Then strSQL = strSQL & " WHERE " & WhereSQL End If ' Execute oCmdOleDb.CommandText = strSQL oDAOleDb = New OleDbDataAdapter(oCmdOleDb) oRS = New DataSet oDAOleDb.Fill(oRS, TableName) oView = New DataView(oRS.Tables(0)) oConnOleDb.Close() oConnOleDb = Nothing oDAOleDb = Nothing oRS = Nothing oCmdOleDb = Nothing Catch ex As Exception End Try End Sub
VB.NET Syntax (Toggle Plain Text)
Public Sub ImportTableData(ByVal ConnectionStr As String, ByVal TableName As String, _ ByRef oImportView As DataView, _ ByVal SelectSQL As String, ByVal WhereSQL As String) ' ' Import table from data view Dim strSQL As String Dim oConn As SqlConnection Dim oCmd As SqlCommand Dim oDA As SqlDataAdapter Dim oRS As DataSet Dim oView As DataView Try strSQL = "" oConn = New SqlConnection(ConnectionStr) oConn.Open() oCmd = Nothing oDA = Nothing oCmd = oConn.CreateCommand() If SelectSQL.Length = 0 Then strSQL = "SELECT * FROM " & TableName Else strSQL = "SELECT " & SelectSQL & " FROM " & TableName End If If WhereSQL.Length > 0 Then strSQL = strSQL & " WHERE " & WhereSQL End If ' Execute oCmd.CommandText = strSQL oDA = New SqlDataAdapter(oCmd) oRS = New DataSet oDA.Fill(oRS, TableName) oView = New DataView(oRS.Tables(0)) ' Set rowstates so that rows are inserted oView.Table.Merge(oImportView.ToTable, True, MissingSchemaAction.Ignore) ' The trick is to use CommandBuilder to create INSERT statement "automatically" Dim oCmdBuilder As SqlCommandBuilder oCmdBuilder = New SqlCommandBuilder(oDA) ' Finally call update to commit changes to database table oDA.Update(oView.Table) oConn.Close() oConn = Nothing oDA = Nothing oRS = Nothing oCmd = Nothing Catch ex As Exception End Try End Sub
VB.NET Syntax (Toggle Plain Text)
Dim oView As DataView ExportTableData(SourceConnectionString, SourceTableName, oView, "", "") ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 24
Reputation:
Solved Threads: 0
Where this code has to be written????
VB.NET Syntax (Toggle Plain Text)
Dim oView As DataView ExportTableData(SourceConnectionString, SourceTableName, oView, "", "") ImportTableData(TargetConnectionString, TargetTableName, oView, "", "")
Okay, your Button1 reads data and Button2 writes data. If you want to keep it that way, set
When I tested it (from SQL Server to Access in my case), I had this whole code
in one button's click event handler.
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, "", "")
Teme64 @ Windows Developer Blog
Hi! If your question is solved, please mark the thread as solved. Thank you!
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 24
Reputation:
Solved Threads: 0
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
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.
There might be a more sophisticated way to do column mapping, but can't get any good ideas straight away in to my mind.
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Simple ASP.Net Login Page using C# (C#)
- 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
| Thread Tools | Search this Thread |
.net 30minutes 2005 2008 access account arithmetic array arrays basic binary bing button buttons c# center check checkbox code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy image images inline insert intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project reports" save searchbox searchvb.net select serial server soap sql table tcp text textbox timer toolbox trim update updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





