Click here to Skip to main content
Sign Up to vote bad
good
See more: SQLVB.NET
can anyone help me how I can store SQL query (statement as log) in database when I use parameter to execute a query?...
 
My prefer language = vb.net
 
for example :
sql = "insert into model (name, age, date_of_confinement,type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
                   " values (@name, @age, @dateOfConfinement, @sicknessType, @fluidType, @bottleAmount, @dropRate)"
I want to store SQL with entered values, but it stores the same as above code
 
Thanks in advance
Posted 1 Oct '12 - 22:55
sali22534
Edited 1 Oct '12 - 23:03

Comments
Sandeep Mewara - 2 Oct '12 - 5:05
Why don't you set values of parameters and then pass the string to save in DB.
sali22 - 2 Oct '12 - 5:09
tq, can u give me an example ?

3 solutions

Try:
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", myValueForColumn1)
		com.Parameters.AddWithValue("@C2", myValueForColumn2)
		com.ExecuteNonQuery()
	End Using
End Using
  Permalink  
Comments
sali22 - 2 Oct '12 - 5:43
Ok, Now if I want to store "com " in database, exactly "com" will be stored without the values ,, in this example is like this : INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2) BUT i want to store it into database with real values not with (@c1,@c2)
OriginalGriff - 2 Oct '12 - 5:48
That is why you use the two lines with the "Parameters.AddWithValue" part...
Try this way:
Dim sql As String = "INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)"
       Dim sql_to_log As String = sql
       sql_to_log = sql_to_log.Replace("@C1", myValueForColumn1.tostring)
       sql_to_log = sql_to_log.Replace("@C2", myValueForColumn2.tostring)
 
Save to database 'sql_to_log'.
  Permalink  
Comments
sali22 - 3 Oct '12 - 21:05
Thanks Ramis, it seems work
there is no other solution !!?
 
let me explain again,,
I want to display the query string after execution when parameters have been used ,
 
I hope you will get what do i mean
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 273
1 Sergey Alexandrovich Kryukov 254
2 Maciej Los 136
3 Richard MacCutchan 125
4 Zoltán Zörgő 90
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid