How to put an array into a table

P

Peter Afonin

Hello,

It should be a simple solution to this, I've just never done it and cannot
find any information so far.

I'm getting an array - the list of the files in the directory:

Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib")
Dim files() As FileInfo = dir.GetFiles("*.eps")

I need to put this array into a SQL server table. How can I do this?

I would appreciate your advice very much.

Thank you,
 
H

Hermit Dave

// create connection object
// open the database connection
foreach(FileInfo myFile in files)
{
// read the file into whatever variables if you please.
// create a command object
// populate the stored procedure if you are using one
// or populate the insert command

// execute the command using ExecuteNonQuery
// dispose the object
}
// close the connection
// dispose the connection

i am saying that you need to open the connection before the foreach because
you might have hell lot of files and you dont want to keep opening database
connection.
i have never reused command object but even that might be worth a try.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
P

Peter Afonin

Thank you very much, Hermit, it worked!

Peter

Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib")
Dim files() As FileInfo = dir.GetFiles("*.eps")
Dim file As FileInfo
oCnn.ConnectionString = smi_class.Constants.Wip7bConnectionString
oCmd.Connection = oCnn
oCmd.CommandType = CommandType.StoredProcedure
oCmd.CommandText = "dbo.uspAddEPS"
oCnn.Open()
With oCmd.Parameters
..Add("@File", SqlDbType.Char, 10).Direction = ParameterDirection.Input
End With
For Each file In files
oCmd.Parameters.Item("@File").Value = Left(file.Name.ToString,
Len(file.Name.ToString) - 4)
oCmd.ExecuteNonQuery()
Next

Hermit Dave said:
// create connection object
// open the database connection
foreach(FileInfo myFile in files)
{
// read the file into whatever variables if you please.
// create a command object
// populate the stored procedure if you are using one
// or populate the insert command

// execute the command using ExecuteNonQuery
// dispose the object
}
// close the connection
// dispose the connection

i am saying that you need to open the connection before the foreach because
you might have hell lot of files and you dont want to keep opening database
connection.
i have never reused command object but even that might be worth a try.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
H

Hermit Dave

Great... hope you are closing and disposing the connection object :)

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Peter Afonin said:
Thank you very much, Hermit, it worked!

Peter

Dim dir As DirectoryInfo = New DirectoryInfo("\\10.0.0.150\FormLib")
Dim files() As FileInfo = dir.GetFiles("*.eps")
Dim file As FileInfo
oCnn.ConnectionString = smi_class.Constants.Wip7bConnectionString
oCmd.Connection = oCnn
oCmd.CommandType = CommandType.StoredProcedure
oCmd.CommandText = "dbo.uspAddEPS"
oCnn.Open()
With oCmd.Parameters
.Add("@File", SqlDbType.Char, 10).Direction = ParameterDirection.Input
End With
For Each file In files
oCmd.Parameters.Item("@File").Value = Left(file.Name.ToString,
Len(file.Name.ToString) - 4)
oCmd.ExecuteNonQuery()
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top