如何在VB.net中从字符串中拆分双引号

时间:2013-12-27 11:17:46

标签: sql vb.net

我在将文本从txt文件插入SQl Compact版数据库时遇到问题。

文本文件中的行如下:

item_code,Loc,cost_price,run_spr,recd_dt,disp_flag,R_qty,s_qty,a_qty,stk_qty,O_qty,
"017245588","I01","0.000","0.000","261013","N","0.000000","-2.000000","0.000000","-2.000000","0.000000"

它有11个字段

当我在下面使用这个插入语句时:

Dim sqlstr As String
Dim sr As StreamReader = New StreamReader("C:\SKORPIO\pdt_item.txt")
Dim line As String = sr.ReadLine()
While Not (sr.EndOfStream)
    line = sr.ReadLine()

    Dim fields() As String = line.Split(",")

    sqlstr = "INSERT INTO pdt_item (item_code,loc,cost_price,run_spr,recd_dt,disp_flag,R_qty,s_qty,a_qty,stk_qty,O_qty) VALUES( '" & fields(0) & "' ,'" & fields(1) & "','" & fields(2) & "','" & fields(3) & "','" & fields(4) & "','" & fields(5) & "','" & fields(6) & "','" & fields(7) & "','" & fields(8) & "','" & fields(9) & "','" & fields(10) & "' )"
    Dim obj As SqlCeCommand = New SqlCeCommand(sqlstr, conn)
    obj.ExecuteNonQuery()

插入语句就像这样

INSERT INTO pdt_item (item_code,loc,cost_price,run_spr,recd_dt,disp_flag,R_qty,s_qty,a_qty,stk_qty,O_qty) VALUES( '"017245588"' ,'"I01"','"0.000"','"0.000"','"261013"','"N"','"0.000000"','"-2.000000"','"0.000000"','"-2.000000"','"0.000000"' )

因为有两个双引号,它给出错误

请注意,我手动测试时没有双引号,如声明正常工作。

INSERT INTO pdt_item (item_code,loc,cost_price,run_spr,recd_dt,disp_flag,R_qty,s_qty,a_qty,stk_qty,O_qty) VALUES( '017245588' ,'I01','0.000','0.000','261013','N','0.000000','-2.000000','0.000000','-2.000000','0.000000' )

那么请如何在VB中编写代码来执行此操作

3 个答案:

答案 0 :(得分:0)

首先删除引号怎么样?

而不是:

line.Split(",")

line.Replace( """", "" ).Split(",")

注意:如果您实际上有任何带引号的数据,并且您希望将引号写入数据库,那么这不是很好

答案 1 :(得分:0)

此操作的正确方法是通过参数化查询 由于字段值周围存在双引号,我认为需要在将值存储到数据库之前将其删除

然而,这可能是正确的代码......

   Dim fields() As String = line.Split(",")

   sqlstr = "INSERT INTO pdt_item " & _
   "(item_code,loc,cost_price,run_spr,recd_dt,disp_flag," & _
   "R_qty,s_qty,a_qty,stk_qty,O_qty) VALUES " & _ 
   "( @p1, @p2, @o3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)"

   Dim obj As SqlCeCommand = New SqlCeCommand(sqlstr, conn)
   obj.Parameters.AddWithValue("@p1", fields(0).Trim(""""c))
   obj.Parameters.AddWithValue("@p2", fields(1).Trim(""""c))
   obj.Parameters.AddWithValue("@p3", fields(2).Trim(""""c))
   obj.Parameters.AddWithValue("@p4", fields(3).Trim(""""c))
   obj.Parameters.AddWithValue("@p5", fields(4).Trim(""""c))
   obj.Parameters.AddWithValue("@p6", fields(5).Trim(""""c))
   obj.Parameters.AddWithValue("@p7", fields(6).Trim(""""c))
   obj.Parameters.AddWithValue("@p8", fields(7).Trim(""""c))
   obj.Parameters.AddWithValue("@p9", fields(8).Trim(""""c))
   obj.Parameters.AddWithValue("@p10", fields(9).Trim(""""c))
   obj.Parameters.AddWithValue("@p11", fields(10).Trim(""""c))
   obj.ExecuteNonQuery()

仍存在与数据表中列的数据类型相关的问题。此代码假定每列都是text类型,因此每个参数值都作为字符串传递。如果不是这种情况,则在添加参数值时需要进行转换(并检查是否可以有效转换该值)。

编辑查看下面的评论很明显,简单的拆分操作不足以正确处理文件数据。您需要使用TextFieldParser class的实例。它有一个名为HasFieldEnclosedInQuotes的属性,当设置为true时,允许在双引号内出现时忽略字段分隔符(逗号)

所以你的循环可以替换为:(警告未测试)

Using sr As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\SKORPIO\pdt_item.txt")
    sr.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    sr.Delimiters = New String() {","}
    sr.HasFieldEnclosedInQuotes = True
    Dim fields As String() = sr.ReadFields()
    While Not sr.EndOfData
        fields = sr.ReadFields()
        ......

答案 2 :(得分:-1)

请使用字符串

"INSERT INTO pdt_item (item_code,loc,cost_price,run_spr,
recd_dt,disp_flag,R_qty, qty,a_qty,stk_qty,O_qty) 
VALUES( '" + fields(0) + "' ,'" + fields(1) + "','" + fields(2) + "','" + fields(3) +  
"','" + fields(4) + "','" + fields(5) + "','" + 
fields(6) + "','" + fields(7) + "','" + fields(8) + "','" + 
fields(9) + "','" + fields(10) + "' )";