Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / June 2004

Tip: Looking for answers? Try searching our database.

Update sql server table with dataset from xls file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chris - 09 Jun 2004 16:01 GMT
I have a program that imports data from an excel spreadsheet into a dataset. I then create a connection to sql server and check to see if a table exist, drop it and recreate it. I cant figure out how to poulate the newly create sql server table with the dataset from excel. any help would be great. Here is the code:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Create connection string variable for excel.
Dim a As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=c:\supplier requirements.xls;" _
& "Extended Properties=Excel 8.0;"

' Create the connection object by using the preceding connection string.
Dim objConn As New OleDbConnection(a)
' Open connection with the database.
objConn.Open()
' The code to follow uses a SQL SELECT command to display the data from the worksheet.
' Create new OleDbCommand to return data from worksheet.
Dim objCmdSelect As New OleDbCommand("Select * from [supplier requirements$]", objConn)
' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter As New OleDbDataAdapter()
' Pass the Select command to the adapter.
objAdapter.SelectCommand = objCmdSelect
' Create new DataSet to hold information from the worksheet.
Dim objDataset As New DataSet()
' Fill the DataSet with the information from the worksheet.
objAdapter.AcceptChangesDuringFill = False
objAdapter.Fill(objDataset, "XLData")
' Build a table from the original data.
DataGrid1.DataSource = objDataset.Tables(0).DefaultView

'get row count

Dim rows As DataRow()
Dim numberofrows As Integer
rows = objDataset.Tables(0).Select()
numberofrows = rows.Length

Me.DataGrid1.CaptionText = CStr(numberofrows) + " rows succesfully loaded into dataset."

Dim SQLConnection As New System.Data.SqlClient.SqlConnection()
SQLConnection.ConnectionString = "user id=sa; password=sa; initial catalog=akgpilot; server=akg-chris-l\vsdotnet;"
SQLConnection.Open()

Dim sqlString As String = "If exists (SELECT name FROM sysobjects WHERE name = N'JD_EDI' AND type = 'U') DROP TABLE JD_EDI"
Dim sqlString2 As String = "Create Table JD_EDI (Type varchar)"

Dim sqlStringCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(sqlString, SQLConnection)
sqlStringCommand.ExecuteNonQuery()

Dim sqlStringCommand2 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(sqlString2, SQLConnection)
sqlStringCommand2.ExecuteNonQuery()
William Ryan eMVP - 09 Jun 2004 17:02 GMT
Create another adapter for the sQL Server table. Set AcceptChangesDuringFill
to false on the adapter you call .Fill with, then just call update on the
same dataset with the second adapter.  Here's
http://www.knowdotnet.com/articles/datasetmerge.html and example of doing it
in between tables, but the same principle applies.

Signature

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/

> I have a program that imports data from an excel spreadsheet into a dataset. I then create a connection to sql server and check to see if a
table exist, drop it and recreate it. I cant figure out how to poulate the
newly create sql server table with the dataset from excel. any help would be
great. Here is the code:

> Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
>
[quoted text clipped - 44 lines]
> Dim sqlStringCommand2 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(sqlString2, SQLConnection)
> sqlStringCommand2.ExecuteNonQuery()

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.