0

I have a CSV file of products I am parsing to create a SQL file and eventually run on the database to add the products.

I am creating my query like this:

 Dim sql As String = "INSERT INTO ""APP"".""PRODUCTS"" (ID,REFERENCE,CODE,CODETYPE,NAME,PRICEBUY,PRICESELL,CATEGORY,TAXCAT,ATTRIBUTESET_ID,STOCKCOST,STOCKVOLUME,IMAGE,ISCOM,ISSCALE,ISKITCHEN,ATTRIBUTES) VALUES (@ID,@REFERENCE,@CODE,@CODETYPE,@NAME,@PRICEBUY,@PRICESELL,@CATEGORY,@TAXCAT,@ATTRIBUTESET_ID,@STOCKCOST,@STOCKVOLUME,@IMAGE,@ISCOM,@ISSCALE,@ISKITCHEN,@ATTRIBUTES)"
        Dim cmd As New SqlCommand(sql)
        cmd.Parameters.AddWithValue("@ID", ID)
        cmd.Parameters.AddWithValue("@REFERENCE", REFERENCE)
        cmd.Parameters.AddWithValue("@CODE", CODE)
        cmd.Parameters.AddWithValue("@CODETYPE", CODETYPE)
        cmd.Parameters.AddWithValue("@NAME", NAME)
        cmd.Parameters.AddWithValue("@PRICEBUY", PRICEBUY)
        cmd.Parameters.AddWithValue("@PRICESELL", PRICESELL)
        cmd.Parameters.AddWithValue("@CATEGORY", CATEGORY)
        cmd.Parameters.AddWithValue("@TAXCAT", TAXCAT)
        cmd.Parameters.AddWithValue("@ATTRIBUTESET_ID", ATTRIBUTESET_ID)
        cmd.Parameters.AddWithValue("@STOCKCOST", STOCKCOST)
        cmd.Parameters.AddWithValue("@STOCKVOLUME", STOCKVOLUME)
        cmd.Parameters.AddWithValue("@IMAGE", IMAGE)
        cmd.Parameters.AddWithValue("@ISCOM", ISCOM)
        cmd.Parameters.AddWithValue("@ISSCALE", ISSCALE)
        cmd.Parameters.AddWithValue("@ISKITCHEN", ISKITCHEN_NEW)
        cmd.Parameters.AddWithValue("@ATTRIBUTES", ATTRIBUTES)

However I do not wish to execute them via .NET, how can I simply export the queries (with the parameters replaced) to a text box, one line per query. (this code is running in a loop and this is for one product in that loop)

I tried cmd.CommandText but it just lists the SQL query with @ID instead of the actual value of the ID variable.

Thanks guys

Nookster
  • 107
  • 1
  • 2
  • 13
  • Can you write some desired output. So that it would be more clear and easy to understand – Sami Oct 19 '12 at 23:58
  • 1
    Duplicate of http://stackoverflow.com/questions/2789476/is-it-possible-to-get-the-parsed-text-of-a-sqlcommand-with-sqlparameters – Luc Morin Oct 20 '12 at 00:10
  • 1
    why you want to export it from sqlcommand? why not use something like: Dim sql As String = "INSERT INTO ""APP"".""PRODUCTS"" (ID,REFERENCE) VALUES ({0}, {1})" sql=string.Format(sql, id, reference) to get the sql query? – urlreader Oct 20 '12 at 05:42

1 Answers1

0

I am assuming that this is an adhoc work since you, essentially, want to create a text file of INSERT statements. In that case, you don't even want to deal with SQL objects. I wouldn't even bother with VB.NET, just use VB Script. Or you can just use Excel to format your INSERT commands and copy and paste it a text file.

Robert Co
  • 1,715
  • 8
  • 14