Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Dim userName As String = "user1"
 Dim sqlQuery2 As String = "INSERT INTO '" & userName & "' (Item,Qty,Price,Total,TimeString,DateString)

can this be happen?
Posted
Updated 19-Oct-13 8:41am
v3
Comments
basurajkumbhar 19-Oct-13 9:50am    
Try your self then ask .

Yes.
It won't do anything useful, in fact SQL will probably complain if you try to use it because you haven't provided any values - but as string concatenation, it's fine. Provided of course you have a table called "user1" - and I hope you haven't! :laugh:
The basic code you are looking for is:
VB
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
 
Share this answer
 
Comments
Member 10200452 19-Oct-13 17:54pm    
the problem of my Insert code is i want to have a parameter in the INSERT INTO
the one i ask is a sample..
OriginalGriff 20-Oct-13 4:12am    
The code sample I showed above illustrates how to use a parameterised query: The parameters are @C1 and @C2, and their values are replaced with myValueForColumn1 and myValueForColumn2 respectively.
You don't need to call then @C1 and @C2, you can use names that start with a '@' and represent what the value is:
@USERNAME
@MOBILENO
for example.
You should be able to work it out from that pretty simply!
To insert a new row, you have to tell sql what table and columns you are adding data to. then, in the same order, you must list the values. An sql insert statement will look simmilar to this on.
SQL
INSERT INTO [Table1] (Column1,column2,column4) VALUES (1,'MickeyMouse',DECMBER/08/1986)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900