Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

looping to a folder list

Posted on 2011-03-01
14
272 Views
Last Modified: 2012-05-11
I have a folder that list the name of GarageID for eg.
In c:\Process, the files in it are GarageID's
6666.xls
6667.xls
6668.xls
so it is an array..

I d lke to use a Sql statement
that something like  "Select xxx,sss, vvv from table where GarageId = 'filename' in that foder'
How is that possible?
Wjat code will implement reading that array(loop) adn constructing the statement? Thanks
0
Comment
Question by:zachvaldez
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35014509
Are you looking for a t-SQL solution for this? i.e. Reading the file names from a folder on a drive?
0
 

Author Comment

by:zachvaldez
ID: 35014535
vb.net because it has to be done inline code... any ideas if stored proc can provide the logic/
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35014733
I asked you because you mentioned that you want to query using a SQL statement. If you are looking for SQL statement, let me know. will try.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 37

Expert Comment

by:ValentinoV
ID: 35014861
If you're looking at looping through files while applying a filter in VB.NET, have a look at the EnumerateFiles method in the System.IO.Disrectory class: http://msdn.microsoft.com/en-us/library/dd413233.aspx

It allows you to return a collection of file names while applying a search pattern.

0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 35016905
Hi,

Don't get it... Are you looking to pick up each file in the array and do something with it or what? How is a sql statement relevant when files are stored in a folder? To iterate over the files in the folder you simply do something like:
        Dim dir As DirectoryInfo = New DirectoryInfo("C:\Process")
        For Each f As FileInfo In dir.GetFiles("*.xls")
            'Here you have a whole bunch of properties to play around with!
        Next

Open in new window


/Carl.
0
 

Author Comment

by:zachvaldez
ID: 35017236
Something like I want to store in a variable the filename as I loop
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 175 total points
ID: 35017821
Hi,

Well to put the filename into a sql table you can do something like:
Imports System.IO
Imports System.Data.SqlClient

Partial Class Default14
    Inherits System.Web.UI.Page

    Private Sub DoFileStuff()

        Dim _conn As New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionStringGoesHere").ConnectionString)
        Dim strInsertGarageID As String = "INSERT INTO tblGarageID (GarageID) VALUES (@GarageID)"
        Dim sqlInsertGarageID As New SqlCommand(strInsertGarageID, _conn)
        Dim dir As DirectoryInfo = New DirectoryInfo("C:\Process")
        If Not _conn.State = Data.ConnectionState.Open Then
            _conn.Open()
        End If
        Try
            For Each f As FileInfo In dir.GetFiles("*.xls")
                sqlInsertGarageID.Parameters.Clear()
                sqlInsertGarageID.Parameters.AddWithValue("@GarageID", f.Name.ToString()) 'Will put the filename into the sql table
                sqlInsertGarageID.ExecuteNonQuery()
            Next
        Catch ex As Exception
            'TODO: Implement error handling logic
        Finally
            If Not _conn.State = Data.ConnectionState.Closed Then
                _conn.Close()
            End If
        End Try


    End Sub
End Class

Open in new window


And then construct your sql statements something like:
SELECT Table.xxx, Table.sss, Table.vvv FROM Table INNER JOIN tblGarageID ON Table.GarageId=tblGarageID.GarageID ORDER BY tblGarageID.GarageID

Open in new window


/Carl.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 175 total points
ID: 35021069
Are you looking for something like below?



Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")

Dim dbadp As New SqlDataAdapter("", "connectionstring")
For each File in Files
   dbadp.SelectCommand.CommandText = "Select * From Table Where FileName='" & File & "'"
   dbadp.Fill(datatable)
   ...
Next
dbadp.Dispose

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35023987
In the loop,I am thinking to run a stored proc by passing the file name jn the where clause
Something like";select email from table where garageid is the filename
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 175 total points
ID: 35035362
Then try this



Dim Root As New DirectoryInfo("C:\")
Dim Files As FileInfo() = Root.GetFiles("*.*")

Dim dbadp As New SqlDataAdapter("", "connectionstring")
dbadp.SelectCommand.CommandType = CommandType.StoredProcedure
dbadp.SelectCommand.CommandText = "stored procedure name"
For each File in Files
   dbadp.SelectCommand.Parameters.AddWithValue("@filename", File)
   dbadp.Fill(datatable)
   dbadp.Parameters.Clear()
   ...
Next
dbadp.Dispose

Open in new window

0
 

Author Comment

by:zachvaldez
ID: 35039063
I am getting
'Parameters is not a member of  System.Data.SQLClient.SQLDataAdapter

in line

 dbadp.Parameters.Clear()

Here's what Im trying to do.

Inside the For Each loop
I pass the filename without the extenxion.That will be the ID that I pass in the stored proc.
My SP statement is something like..Select email from xtablle where GarageId = '@fileId'
What ever is returned I stored it in variable
sMsgTo:(email)
I already got the email routine. ...
So every loop I get the email in a variable.

so does this proposition fit in the loop?
0
 
LVL 18

Assisted Solution

by:carlnorrbom
carlnorrbom earned 175 total points
ID: 35039504
Hi,

You need to change it to:
dbadp.SelectParameters.Clear()

Open in new window


/Carl.
0
 

Author Comment

by:zachvaldez
ID: 35040070
For each File in Files
   dbadp.SelectCommand.Parameters.AddWithValue("@filename", File)
   dbadp.Fill(datatable)
  dbadp.SelectParameters.Clear()
Next

where will the return value stored here in the loop
0
 

Author Closing Comment

by:zachvaldez
ID: 35083219
Well  done!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question