2017-04-19 63 views
-2

我有一個數組像下面如何向所有數組項添加一些字符串?

(0) = "apple" 
(1) = "orange" 

我怎樣才能一些字符串數組添加到所有的項目嗎?像蘋果成爲 '蘋果',橙色成爲 '橙色'

編輯

Private Sub test() 
     Dim txtReader As TextReader = New StreamReader("data.csv") 
     Dim parser = New CsvParser(txtReader) 
     Dim str As String = "" 

     'Ignore first line 
     parser.Read() 

     While True 
      Dim row = parser.Read() 
      If row Is Nothing Then 
       Exit While 
      End If 

      str &= $"({String.Join(",", row)})," 
     End While 

     str_record = str.TrimEnd(",") 
End Sub 

Private Sub Model_Insert() 
    Dim data As String = "" 
    Dim query As String = "INSERT INTO main_item(item_code,item_name,item_desc,item_unitprice,item_barcode,dept_id,cat_id,gst_id,set_item,active)" & 
          "VALUES " & str_record & "" 

    Using cmd = New MySqlCommand(query, conn) 
     cmd.ExecuteNonQuery() 
    End Using 
End Sub 

我試着去創建一個字符串,並用它在INSERT INTO

+0

你可以嘗試更清楚,因爲它沒有多大意義陣列通常像這樣的工作:'點心水果( )As String = {「apple」,「orange」,「...」}' – Mederic

+0

現在你已經完全修改了你的問題。陣列在哪裏?你爲什麼不在第一時間發佈你真正的問題? –

+1

請不要這樣做。使用參數化查詢,不要使用字符串連接 – Pikoh

回答

4

使用For -loop:

Dim array = {"apple", "orange"} 
For i As Int32 = 0 To array.Length - 1 
    array(i) = $"'{array(i)}'" 
Next 

如果您還不能使用字符串插值,請使用String.Format(或字符串連接):

For i As Int32 = 0 To array.Length - 1 
    array(i) = String.Format("'{0}'", array(i)) 
Next 
+0

是否有可能在你的第二個解決方案中加入str&= $「({String.Join(」,「,array)})」,所以我不必使用for循環? – vbnewbie

+0

@vbnewbie:不,但爲什麼你不想使用for循環?我以爲你需要修改數組中的所有字符串。或者你想要一個單一的字符串作爲結果? –

+0

@vbnewbie:我已經給你演示瞭如何使用sql參數 –

0

如果你不介意重新創建數組,你可以使用Linq Select

Dim testarray As String() = New String() {"orange", "apple"} 
testarray = testarray.Select(Function(x) String.Format("'{0}'", x)).ToArray()