473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Powershell and Updating Databases

Problem is that I want to use PowerShell and a csv file to update a database
(Access, SQL, whatever). I do this now with VBScript. I have made a simple
example of the ps1 script and text file and it works BUT only inserts one
row. If the sample "simpleTest.ps1" is run from powershell the extraction is
fine. I have commented out the database stuff which works when a instance of
Access "Adatabase" with a "Table1" with all fields set to datatype "text" has
been created, but only one row gets added. I have also tried this with a
do-while and do-until loops with movenext and eof stuff without success.

The csv file - "simpleList.txt" - contains:

a,1,2
b,3,4
c,5,6
d,7,8
e,8,0

The ps1 file - "simpleTest.ps1" - contains:

#$adOpenStatic = 3
#$adLockOptimistic = 3
#$objConnection = New-Object -com "ADODB.Connection"
#$objRecordSet = New-Object -com "ADODB.Recordset"
#$objConnection.Open("Provider=Microsoft.Jet.OLEDB .4.0;Data Source =
c:\Adatabase.mdb")
#$objRecordSet.Open("DELETE * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimistic)
#$objRecordSet.Open("SELECT * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimistic)
$toDay = Get-Date -Format yyMMdd
$fileA = "C:\SimpleList.txt"
#$objRecordSet.AddNew()
ForEach ($a in Get-Content $fileA)
{
$i = $a.split(",")
$toDay + " " + $i[0] + " " + $i[1] + " " + $i[2]
# $objRecordSet.Fields.Item("date").value = $toDay
# $objRecordSet.Fields.Item("field01").value = $i[0]
# $objRecordSet.Fields.Item("field02").value = $i[1]
# $objRecordSet.Fields.Item("field03").value = $i[2]
# $objRecordSet.Update()
}
#$objRecordSet.Close()
#$objConnection.Close()

What am I missing?
Nov 7 '08 #1
1 3065
"leroycarl" <le*******@discussions.microsoft.comwrote in message
news:99**********************************@microsof t.com...
Problem is that I want to use PowerShell and a csv file to update a
database
(Access, SQL, whatever). I do this now with VBScript. I have made a
simple
example of the ps1 script and text file and it works BUT only inserts one
row. If the sample "simpleTest.ps1" is run from powershell the extraction
is
fine. I have commented out the database stuff which works when a instance
of
Access "Adatabase" with a "Table1" with all fields set to datatype "text"
has
been created, but only one row gets added. I have also tried this with a
do-while and do-until loops with movenext and eof stuff without success.

The csv file - "simpleList.txt" - contains:

a,1,2
b,3,4
c,5,6
d,7,8
e,8,0

The ps1 file - "simpleTest.ps1" - contains:

#$adOpenStatic = 3
#$adLockOptimistic = 3
#$objConnection = New-Object -com "ADODB.Connection"
#$objRecordSet = New-Object -com "ADODB.Recordset"
#$objConnection.Open("Provider=Microsoft.Jet.OLEDB .4.0;Data Source =
c:\Adatabase.mdb")
#$objRecordSet.Open("DELETE * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimistic)
#$objRecordSet.Open("SELECT * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimistic)
$toDay = Get-Date -Format yyMMdd
$fileA = "C:\SimpleList.txt"
#$objRecordSet.AddNew()
ForEach ($a in Get-Content $fileA)
{
$i = $a.split(",")
$toDay + " " + $i[0] + " " + $i[1] + " " + $i[2]
# $objRecordSet.Fields.Item("date").value = $toDay
# $objRecordSet.Fields.Item("field01").value = $i[0]
# $objRecordSet.Fields.Item("field02").value = $i[1]
# $objRecordSet.Fields.Item("field03").value = $i[2]
# $objRecordSet.Update()
}
#$objRecordSet.Close()
#$objConnection.Close()

What am I missing?
I think you need to put $objRecordSet.AddNew() inside the ForEach loop.

Nov 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil Chambers | last post by:
Hi All, I'm looking to see if it's feasible to use the SortObjectCommand included in the Microsoft.Powershell.Commands assembly. I have a Dictionary Dictionary<string, intd = new...
3
by: =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?= | last post by:
1. Is there a dedicated discussion group for Windows Powershell? I couldn't find one, but that doesn't necessarily mean there isn't :-) 2. I'm just starting in on learning to use Powershell and...
6
by: Mesan | last post by:
Does anyone have an idea what it takes to administer DB2 through PowerShell? I want to write some scripts to automate my common DB2 management tasks and I'd really like to be able to use...
1
by: Alexander Vasilevsky | last post by:
Hi! How to debug Powershell commandlets? -- http://www.alvas.net - Audio tools for C# and VB.Net developers
5
by: josepharmbruster | last post by:
I am all about using the right tool for the right purposes, which is why I have started reading the GettingStarted guide to PowerShell. I am curious if any other pythoneers have ventured into the...
3
by: sridhard2406 | last post by:
Hi I didn't find a forum for powershell so i'm my query here itself. I'm runnung a powershell script and i want to redirect the output to a xml after redirecting the output i need to search a...
0
by: =?Utf-8?B?SmVycnk=?= | last post by:
Hi, I just don't get it :( I'm using VB.NET code, within this code I want to call some Powershell code. (To create a user mailbox in Exchange 2007). How can I run Powershell code in vb.net?...
1
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i read the MS kb: Using Exchange Management Shell Commands With Managed Code http://msdn.microsoft.com/en-us/library/bb332449.aspx how ever i'm getting "{"No Windows PowerShell Snap-ins are...
1
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.