Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 22:55pm
sali22417
Edited 1-Oct-12 23:03pm
v4
Comments
Sandeep Mewara at 2-Oct-12 5:05am
   
Why don't you set values of parameters and then pass the string to save in DB.
sali22 at 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 at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 418
1 OriginalGriff 344
2 George Jonsson 233
3 Shemeemsha RA 138
4 Animesh Datta 130
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
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