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 / May 2005

Tip: Looking for answers? Try searching our database.

Reading Data From Excel Via ADO.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kyle Novak - 24 May 2005 23:12 GMT
I have an application reading data from an Excel file and filling a SQL
Server table.  When I read the Excel file, I want to be able to trim
trailing spaces off the data as it's read into the dataset.  I'm connecting
to the Excel file via an OleDbDataAdapter & OleDbCommand object.  All the
code compiles and runs fine - I just want to trim any trailing spaces off of
the data.

Here is a portion of the code I'm using:

     Dim oCmdSelect As New OleDbCommand(moImportFormat.SelectFromStatement,
moConnExcel)
     'Get the values in the Excel file and verify
     mDSExcel = New DataSet()

     Dim oDA As New OleDbDataAdapter()
     oDA.AcceptChangesDuringFill = False
     oDA.SelectCommand = oCmdSelect
     oDA.Fill(mDSExcel, sTableName)

The SQL statement stored in moImportFormat.SelectFromStatement is:

SELECT STORENAME FROM [Sheet1$]

The trick, I believe, is to add a "trim" statement as in TRIM(STORENAME) or
RTRIM(STORENAME).  However when I tried TRIM & RTRIM, I receive an error
message.  When I take out the TRIM or RTRIM it works fine.  So the $1
million dollar question is, is there a function that can "trim" data when
being read in with an OleDbDataAdapter / OleDbCommand?

Thanks,

Kyle
Adrian Moore - 25 May 2005 00:12 GMT
Kyle,

Unfortunately, you'll have to iterate over the table column in the and call
TRIM on its value.

For Each r as DataRow in mDsExcel.Tables(0).Rows
 r(0) = TRIM(r(0))
Next

Ad.

>I have an application reading data from an Excel file and filling a SQL
>Server table.  When I read the Excel file, I want to be able to trim
[quoted text clipped - 28 lines]
>
> Kyle
Paul Clement - 25 May 2005 15:48 GMT
¤ I have an application reading data from an Excel file and filling a SQL
¤ Server table.  When I read the Excel file, I want to be able to trim
¤ trailing spaces off the data as it's read into the dataset.  I'm connecting
¤ to the Excel file via an OleDbDataAdapter & OleDbCommand object.  All the
¤ code compiles and runs fine - I just want to trim any trailing spaces off of
¤ the data.
¤
¤ Here is a portion of the code I'm using:
¤
¤       Dim oCmdSelect As New OleDbCommand(moImportFormat.SelectFromStatement,
¤ moConnExcel)
¤       'Get the values in the Excel file and verify
¤       mDSExcel = New DataSet()
¤
¤       Dim oDA As New OleDbDataAdapter()
¤       oDA.AcceptChangesDuringFill = False
¤       oDA.SelectCommand = oCmdSelect
¤       oDA.Fill(mDSExcel, sTableName)
¤
¤ The SQL statement stored in moImportFormat.SelectFromStatement is:
¤
¤ SELECT STORENAME FROM [Sheet1$]
¤
¤ The trick, I believe, is to add a "trim" statement as in TRIM(STORENAME) or
¤ RTRIM(STORENAME).  However when I tried TRIM & RTRIM, I receive an error
¤ message.  When I take out the TRIM or RTRIM it works fine.  So the $1
¤ million dollar question is, is there a function that can "trim" data when
¤ being read in with an OleDbDataAdapter / OleDbCommand?

You didn't mention the error message, but if you're using the Jet OLEDB Provider w/the Excel ISAM
then the Trim functions should be available:

http://support.microsoft.com/default.aspx?scid=kb;en-us;239482

Paul
~~~~
Microsoft MVP (Visual Basic)

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.