Click here to Skip to main content
13,253,507 members (56,962 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 =

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 23:55pm
Updated 2-Oct-12 0:03am
Sandeep Mewara 2-Oct-12 5:05am
Why don't you set values of parameters and then pass the string to save in DB.
sali22 2-Oct-12 5:09am
tq, can u give me an example ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Using con As New SqlConnection(strConnect)
	Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", myValueForColumn1)
		com.Parameters.AddWithValue("@C2", myValueForColumn2)
	End Using
End Using
sali22 2-Oct-12 5:43am
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:48am
That is why you use the two lines with the "Parameters.AddWithValue" part...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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'.
sali22 3-Oct-12 21:05pm
Thanks Ramis,
it seems work
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100