#在vs调试时发现数据库语句错误,但又不知道具体错误,此时需要找到执行语句来查看数据库的具体报错
#在vs中通过debug获取的对象信息为文本模式
$list='Account "185xxxxxxxx" string
AuditDateTime null System.DateTime?
AuditStatus null string
AuditUserId null string
AuditUserName null string
BankName null string
Birthday null System.DateTime?
CardId "230302xxxxxxxx4011" string
ChangePasswordDate null System.DateTime?
Code "GT1805140001" string
Commission null int?
CompanyId "090f2a23-d641-4c01-bee9-dc5f80691935" string
CreateDate null System.DateTime?
CreateUserId null string
CreateUserName null string
CreditID " " string
CreditName " " string
DeleteMark null int?
DepartmentId null string
Email " " string
Enabled 1 int?
FirstVisit null System.DateTime?
Gender "男" string
InnerUser 0 int?
LastVisit null System.DateTime?
LogOnCount null int?
Mobile "185xxxxxxxx" string
ModifyDate 2018/5/15 15:45:36 System.DateTime?
ModifyUserId "System" string
ModifyUserName "超级管理员" string
OICQ null string
Online null int?
OpenId null int?
Password null string
PreviousVisit null System.DateTime?
RealName "kay" string
Remark " " string
Secretkey null string
SortCode null int?
Spell "请选择" string
Telephone " " string
UserId "29F31DE3-002E-44F2-9C6B-F40AC9F99D2F" string
emailstate null string
imgurl1 null string
imgurl2 null string
mAddress " " string
mamount null int?
mheader null string
sponsor " " string
weixin "12345678" string'
#通过mssql监控程序获得的代码为带变量的文本
$sql='Update Base_User
Set CompanyId=@CompanyId,
InnerUser=@InnerUser,
Code=@Code,
Account=@Account,
RealName=@RealName,
Spell=@Spell,
Gender=@Gender,
Mobile=@Mobile,
Telephone=@Telephone,
Email=@Email,
Remark=@Remark,
Enabled=@Enabled,
ModifyDate=@ModifyDate,
ModifyUserId=@ModifyUserId,
ModifyUserName=@ModifyUserName,
CardId=@CardId,
weixin=@weixin,
mAddress=@mAddress,
sponsor=@sponsor,
CreditID=@CreditID,
CreditName=@CreditName
Where UserId=@UserId'
#将第一个文本转换为带name,value,type属性的列表
$itemlist=New-Object System.Collections.ArrayList
foreach($item in $list.Split("`n")){
if($item){
$item=$item.split('',[StringSplitOptions]::RemoveEmptyEntries)
$obj=New-Object psobject|select name,value,type
$obj.name=$item[0] #为属性赋值
$obj.value=$item[1]
$obj.type=$item[2]
[void]$itemlist.add($obj)#加入到列表
}
}
#通过正则表达式替换变量为数值
[regex]::Replace($sql,'@\w+',{param($str)
$find=$str.tostring().TrimStart("@")
$item=$itemlist|?{$_.name -eq $find}
if($item.type -notmatch "int"){
"'"+$item.value.replace('"',$null)+"'" } #如果不是数字类型,去掉双引号,补上单引号,不直接替换是因为日期不带双引号但也需要单引号
else{$item.value}
})
在控制台获得的结果:
Update Base_User
Set CompanyId='090f2a23-d641-4c01-bee9-dc5f80691935',
InnerUser=0,
Code='GT1805140001',
Account='185xxxxxxxx',
RealName='kay',
Spell='请选择',
Gender='男',
Mobile='185xxxxxxxx',
Telephone=' ',
Email=' ',
Remark=' ',
Enabled=1,
ModifyDate='2018/5/15',
ModifyUserId='System',
ModifyUserName='超级管理员',
CardId='230302xxxxxxxx4011',
weixin='12345678',
mAddress=' ',
sponsor=' ',
CreditID=' ',
CreditName=' '
Where UserId='29F31DE3-002E-44F2-9C6B-F40AC9F99D2F'