2016-08-22 71 views
0

我遇到了一些問題,一直出現錯誤消息,我找不到原因。在導入時將CSV導入到本地主機錯誤

我正在嘗試構建一個在我的PC上利用WAMP的數據庫功能來存儲和操作數據而不是使用MS Access的進程。

下面的腳本包含我一直在處理的當前powershell腳本,它創建一個新的數據庫和表來包含我的CSV數據,但是當我試圖將CSV數據插入新創建的表格時,有兩個重複的錯誤。

The property 'CommandText' cannot be found on this object. Verify that the property exists and can be set.

而且

Cannot convert value ",'" to type "System.Int32". Error: "Input string was not in a correct format."

我只是茫然地如何糾正這一點,爲什麼它拋出的錯誤,這是有目共睹的人嗎?

$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或腳本傳遞到本地主機上新創建的表,我仍然感到不知所措。

[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

在'foreach($ i中的Import-Csv $ cl2location)'塊中,您拼寫了'$ cmd',缺少'c' –

+0

變量名應該是'$ ccmd',而不僅僅是'$ cmd'。另外,您可能需要[直接導入CSV](https://dev.mysql.com/doc/refman/5.7/en/load-data。HTML)而不是做多個插入。 –

+0

嗨,大家好,是的,我注意到了$ ccmd錯誤並更新了它,但即使更新也存在相同的錯誤。我嘗試過以各種方式格式化csv,但它拒絕插入。 –

回答

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+"');" 

單獨的語句,因爲前兩行是一個完整的聲明d自己。因爲+$i.cl2accountcode(即$null + [int])變成了一個整數,並且[int] + [string]僅在字符串可以轉換爲整數(對於字符串​​3210不是這種情況)時纔有效,所以第三行會拋出錯誤。

爲了使字符串連接工作跨越你需要或者逃避線的線條打破

$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+"');"