将以下脚本存储为Set-Dbf.ps1,使用Powershell调用该脚本该脚本,传入两个参数即可
##################################################################################
#
#
# Script name: Set-Dbf.ps1
#
# Author: yumenma
# Homepage: http://hi.csdn.net/yumenma
# Company: www.autonavi.com
#
##################################################################################
############################################################################
#
#参数讲解
#传入的两个参数:
#$Query:SQL查询语句,例如:"update table set name = 'something' where id = 1"
#$FilesAddress:DBF数据存在的文件地址,例如:"C:/myvfpdbfolder"
#
#############################################################################
param([string]$Query,[string]$FilesAddress)
# Default Connection String
$ConnString =
"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=$FilesAddress;Exclusive=No; Collate=Machine;NULL=NO;DELETED=YES;BACKGROUNDFETCH=NO;"
$Connection = New-Object System.Data.Odbc.OdbcConnection
$Connection.ConnectionString = $ConnString
$Connection.Open()
$Command = New-Object System.Data.Odbc.OdbcCommand
$Command.Connection = $Connection
$Command.CommandText = $Query
return $Reader = $Command.ExecuteNonQuery()
$Connection.Close()
############################################################################
例子:
ps>./Set-Dbf.ps1 "update table set name = 'something' where id = 1" "c:/myvfpdbfloder"
此时已经将table里的id =1的name修改了,如果想查看,请使用Get-Dbf方法
ps>./Get-Dbf.ps1 "select * from table where id = 1" "c:/myvfpdbfloder"