Click here to Skip to main content
12,628,328 members (35,364 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB SQL VB.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 23:55pm
sali22391
Updated 2-Oct-12 0:03am
v4
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 1

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: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
 
good
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'.
  Permalink  
Comments
sali22 3-Oct-12 21:05pm
   
Thanks Ramis,
it seems work
Rate this: bad
 
good
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
  Permalink  
v2

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 | Mobile
Web01 | 2.8.161205.3 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2016
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