我遇到了一些错误消息的问题,但是我无法找到原因。
我正在尝试构建一个过程,利用我的PC上的WAMP数据库功能来存储和操作数据,而不是使用MS Access。
下面的脚本包含我一直在处理的当前powershell脚本,它创建了一个新的数据库和表来包含我的CSV数据但是当我尝试将CSV数据插入新创建的表时它会向我呈现两个重复的错误。
属性' CommandText'在这个对象上找不到。验证该属性是否存在且可以设置。
并且
无法转换价值",'"输入" System.Int32"。错误:"输入字符串的格式不正确。"
我只是不知道如何纠正这个以及它为什么会抛出错误,这对任何人来说都是显而易见的吗?
$CL2Location = 'L:\Controls\BROKER CASH RECONCILIATIONS\cl2cashpositions-331-Corrected.csv'
$dbnameone = "brokerreconciliation"
[system.reflection.assembly]::LoadWithPartialName("MySql.Data")
$mysqlConn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$mysqlConn.ConnectionString = "SERVER=localhost;DATABASE=brokerreconciliation;UID=root;PWD=''"
$mysqlConn.Open()
$ccmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$ccmd.Connection = $mysqlConn
$ccmd.CommandText = "DROP DATABASE IF EXISTS " + $dbnameone
$ccmd.ExecuteNonQuery()
$ccmd.CommandText = 'CREATE SCHEMA `' + $dbnameone + '`'
$ccmd.ExecuteNonQuery()
$dbonetablescript = @"
CREATE TABLE brokerreconciliation.CL2 (
`ID` MEDIUMINT(8) unsigned NOT NULL auto_increment,
`CL2ACCOUNTCODE` varchar(255) default NULL,
`ACBALANCE` varchar(255) default NULL,
`PARNAME1` varchar(255) default NULL,
PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;
"@
$ccmd.CommandText = $dbonetablescript
$ccmd.ExecuteNonQuery()
$ccmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$ccmd.Connection = $mysqlConn
$ccmd.CommandText = "truncate table " + $dbnameone + ".CL2;"
$ccmd.ExecuteNonQuery()
foreach ($i in Import-Csv $cl2location) {
$cmd.CommandText =
"INSERT INTO customers (id,cl2accountcode,acbalance,parname1) VALUES ("
+$i.id+",'"+$i.cl2accountcode+"','"+$i.acbalance+"','"+$i.parname+"');"
$cmd.ExecuteNonQuery()
}
Import-Csv $CL2Location ##Added to ensure that the data file was being reached it is
$mysqlConn.Close()
示例CSV数据的链接为here。
尝试解决此问题我尝试直接从通过初始sql查询创建的数据表中编写数据,以减少我需要的步骤量。下面的代码是更新,即使直接使用结果集我尝试将数据上传到本地主机时仍然遇到相同的错误。
我还编辑了CREATE TABLE
元素以完全匹配基本数据库的模式,以确保没有任何内容导致问题。
我仍然不知道如何将CSV或脚本中的信息传递给localhost上新创建的表。
[System.Reflection.Assembly]::LoadWithPartialName("Sql.Data")
$null = [Reflection.Assembly]::LoadWithPartialName("WindowsBase")
#####################
## - CREDENTIALS - ##
#####################
$MISA = 'xx.xx.x.xx'
$userName = 'IR'
$PassWord = 'IR'
$DB = 'IR'
$timeout = 0
###### - StopWatch - ######
$timeout2 = new-timespan -Minutes 5
$sw = [diagnostics.stopwatch]::StartNew()
##### sql ####
### MIS CL2 ###
$CL2CashPositionsScript = @'
SELECT CL2ACCOUNTCODE, sum(CAST(CL2ACCOUNTBALANCE AS MONEY)) AS CBALANCE, PARNAME1
FROM T5CASHL2 CL2 LEFT OUTER JOIN T5PARTICIPANT PAR
ON PAR.PARPDRPARTICIPANTID = CL2.CL2ACCOUNTCODE
WHERE CL2CLIENTNUM not like '315'
--AND CL2ACCOUNTCODE = '331'
GROUP BY CL2ACCOUNTCODE, PARNAME1, PARNAME2
ORDER BY CL2ACCOUNTCODE ASC
'@
## CREATE MIS CREDENTIALS ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection("Connection Timeout=0")
$SqlConnection.ConnectionString = "Data Source=$MISA;Initial Catalog=$DB;
Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"
## - Runs Script from Set Location
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandTimeout=$timeout;
$SqlCMD.CommandText = $CL2CashPositionsScript;
$SqlCmd.Connection = $SqlConnection;
## - Extract Data and build sql data object
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$SqlAdapter.SelectCommand = $SqlCmd;
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet);
$DataSetTable = $DataSet.Tables["Table"];
#######
$CL2Location = 'L:\Controls\BROKER CASH RECONCILIATIONS\cl2cashpositions-331-Correctedb.csv'
$dbnameone = "brokerreconciliation"
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$mysqlConn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$mysqlConn.ConnectionString = "SERVER=localhost;DATABASE=brokerreconciliation;UID=root;PWD=''"
$mysqlConn.Open()
$ccmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$ccmd.Connection = $mysqlConn
$ccmd.CommandText = "DROP DATABASE IF EXISTS " + $dbnameone
$ccmd.ExecuteNonQuery()
$ccmd.CommandText = 'CREATE SCHEMA `' + $dbnameone + '`'
$ccmd.ExecuteNonQuery()
$dbonetablescript = @"
CREATE TABLE brokerreconciliation.CL2 (
`ID` MEDIUMINT(8) unsigned NOT NULL auto_increment,
`CL2ACCOUNTCODE` char(12) default NULL,
`CBALANCE` char(20) default NULL,
`PARNAME1` varchar(30) default NULL,
PRIMARY KEY (`ID`)
) AUTO_INCREMENT=1;
"@
$ccmd.CommandText = $dbonetablescript
$ccmd.ExecuteNonQuery()
$ccmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$ccmd.Connection = $mysqlConn
$ccmd.CommandText = "truncate table " + $dbnameone + ".CL2;"
$ccmd.ExecuteNonQuery()
foreach ($i in $DataSetTable) {
$ccmd.CommandText =
"INSERT INTO customers (cl2accountcode,cbalance,parname1) VALUES ("
+$i.cl2accountcode+"';'"+$i.cbalance+"';'"+$i.parname+"');"
$ccmd.ExecuteNonQuery()
}
$mysqlConn.Close()
答案 0 :(得分:1)
你不能像这样包装字符串:
$ccmd.CommandText = "INSERT INTO customers (cl2accountcode,cbalance,parname1) VALUES (" +$i.cl2accountcode+"';'"+$i.cbalance+"';'"+$i.parname+"');"
PowerShell将解释
$ccmd.CommandText =
"INSERT INTO customers (cl2accountcode,cbalance,parname1) VALUES ("
和
+$i.cl2accountcode+"';'"+$i.cbalance+"';'"+$i.parname+"');"
作为单独的语句,因为前两行是一个完整的语句本身。然后第三行抛出一个错误,因为+$i.cl2accountcode
(即$null + [int]
)变成一个整数,而[int] + [string]
只有在字符串可以强制转换为整数时才有效(事实并非如此)对于字符串';'
)。
要使字符串连接跨越行,您需要转义换行符
$ccmd.CommandText =
"INSERT INTO customers (cl2accountcode,cbalance,parname1) VALUES (" `
+$i.cl2accountcode+"';'"+$i.cbalance+"';'"+$i.parname+"');"
或将连接运算符放在行尾(因此PowerShell知道还有更多内容)
$ccmd.CommandText =
"INSERT INTO customers (cl2accountcode,cbalance,parname1) VALUES (" +
$i.cl2accountcode+"';'"+$i.cbalance+"';'"+$i.parname+"');"