Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB
code using variables?
The following statement is failing!
sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _
str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1"
dt1 = getdate()
str2 = "col2"
str3 = "col3"
Thanks 11 15749
Hi,
Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" &
dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two
consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB
code using variables?
The following statement is failing!
sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _
str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1"
dt1 = getdate()
str2 = "col2"
str3 = "col3"
Thanks
Thanks Shiva.
I have added quotes to the string values. As follows:
sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _
"'str1' , dt1 , 'str2' , 'str3')"
Now I get an error at dt1
"Shiva" wrote: Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
I have a question though, won't SQL server consider the string variable
itself as the
value, if the value is enclosed in quotes?
"Shiva" wrote: Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
Hi,
What is the value passed for dt1? Is it in appropriate format? Try this
format: YYYY-MM-DD HH:mm:SS, enclosed in single quotes.
"sm" <sm@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
Thanks Shiva.
I have added quotes to the string values. As follows:
sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _
"'str1' , dt1 , 'str2' , 'str3')"
Now I get an error at dt1
"Shiva" wrote: Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through
VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
You are not enclosing the variable itself, but just its value. If for
example s1 = "abc", then the expression "'" & s1 & "'" will produce 'abc'
I hope I got your question right.
"sm" <sm@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
I have a question though, won't SQL server consider the string variable
itself as the
value, if the value is enclosed in quotes?
"Shiva" wrote: Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through
VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
Hi,
With respect to the date, I have a string
str ="2004/04/04 15:52"
which I am converting to date using CDate(str) and passing str to the INSERT
statement. Where do I specify the format YYYY-MM-DD HH:mm:SS?
Thanks
"Shiva" wrote: Hi, What is the value passed for dt1? Is it in appropriate format? Try this format: YYYY-MM-DD HH:mm:SS, enclosed in single quotes.
"sm" <sm@discussions.microsoft.com> wrote in message news:30**********************************@microsof t.com...
Thanks Shiva.
I have added quotes to the string values. As follows: sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ "'str1' , dt1 , 'str2' , 'str3')" Now I get an error at dt1
"Shiva" wrote:
Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
Pass the date string as is without doing the conversion. But, include it
inside single quotes. I think it should work.
"sm" <sm@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
Hi,
With respect to the date, I have a string
str ="2004/04/04 15:52"
which I am converting to date using CDate(str) and passing str to the INSERT
statement. Where do I specify the format YYYY-MM-DD HH:mm:SS?
Thanks
"Shiva" wrote: Hi, What is the value passed for dt1? Is it in appropriate format? Try this format: YYYY-MM-DD HH:mm:SS, enclosed in single quotes.
"sm" <sm@discussions.microsoft.com> wrote in message news:30**********************************@microsof t.com...
Thanks Shiva.
I have added quotes to the string values. As follows: sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ "'str1' , dt1 , 'str2' , 'str3')" Now I get an error at dt1
"Shiva" wrote:
Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','"
& dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with
two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server
through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" &
_ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
"sm" <sm@discussions.microsoft.com> schrieb: Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
Use an 'InsertCommand' + parameters instead:
Using Parameters with a 'DataAdapter'
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusingparameterswithdataadapters.asp>
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Thanks Shiva; the insert is successful now.
But the table shows 3:52 PM instead of 15:52! Is there some way to get the
datetime in the table in this format "2004/04/04 15:52"?
Thanks once again.
"Shiva" wrote: Pass the date string as is without doing the conversion. But, include it inside single quotes. I think it should work.
"sm" <sm@discussions.microsoft.com> wrote in message news:6C**********************************@microsof t.com... Hi,
With respect to the date, I have a string str ="2004/04/04 15:52" which I am converting to date using CDate(str) and passing str to the INSERT statement. Where do I specify the format YYYY-MM-DD HH:mm:SS?
Thanks
"Shiva" wrote:
Hi, What is the value passed for dt1? Is it in appropriate format? Try this format: YYYY-MM-DD HH:mm:SS, enclosed in single quotes.
"sm" <sm@discussions.microsoft.com> wrote in message news:30**********************************@microsof t.com...
Thanks Shiva.
I have added quotes to the string values. As follows: sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ "'str1' , dt1 , 'str2' , 'str3')" Now I get an error at dt1
"Shiva" wrote:
Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 & "','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
It is just the formatting (current regional setttings) that it uses to
display the value. Have a look at CAST and CONVERT operators to customize
the output values.
"sm" <sm@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
Thanks Shiva; the insert is successful now.
But the table shows 3:52 PM instead of 15:52! Is there some way to get the
datetime in the table in this format "2004/04/04 15:52"?
Thanks once again.
"Shiva" wrote: Pass the date string as is without doing the conversion. But, include it inside single quotes. I think it should work.
"sm" <sm@discussions.microsoft.com> wrote in message news:6C**********************************@microsof t.com... Hi,
With respect to the date, I have a string str ="2004/04/04 15:52" which I am converting to date using CDate(str) and passing str to the
INSERT statement. Where do I specify the format YYYY-MM-DD HH:mm:SS?
Thanks
"Shiva" wrote:
Hi, What is the value passed for dt1? Is it in appropriate format? Try this format: YYYY-MM-DD HH:mm:SS, enclosed in single quotes.
"sm" <sm@discussions.microsoft.com> wrote in message news:30**********************************@microsof t.com...
Thanks Shiva.
I have added quotes to the string values. As follows: sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" &
_ "'str1' , dt1 , 'str2' , 'str3')" Now I get an error at dt1
"Shiva" wrote:
Hi, Enclose all string values in single quotes:
"INSERT INTO TestTab (Col1, col2, col3, col4) VALUES ('" & str1 &
"','" & dt1 & "','" & str2 & "','" & str3 & "')"
If string values contain inturn contain single quote escape them with two consequtive single quotes.
HTH.
"sm" <sm@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values("
& _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks
SM,
I sand this sample that I made some days ago to this newsgroup, I have now
changed it a little bit in this message to make it more globalized usable.
You see in this used the parameters.
I hope this helps?
Cor
\\\You can use for this sample the Northwind database in SQL
Public Class Main
Public Shared Sub Main()
Dim Conn As New SqlClient.SqlConnection _
("Server=localhost;DataBase=Northwind;Integrate d Security=SSPI")
Try
Dim strSQL As String = "INSERT INTO Employees " & _
"(LastName, FirstName, HireDate)" & _
"VALUES ('Kevin', 'Hodgson', @HireDate)"
Dim cmd As New SqlClient.SqlCommand(strSQL, Conn)
Dim myparam As New SqlClient.SqlParameter
myparam.DbType = DbType.DateTime
myparam.ParameterName = "@HireDate"
myparam.Value = New Date(2004, 11, 2)
cmd.Parameters.Add(myparam)
Conn.Open()
cmd.ExecuteNonQuery()
Catch ex As SqlClient.SqlException
MessageBox.Show(Ex.ToString)
Catch ex As Exception
MessageBox.Show(Ex.ToString)
End Try
End Sub
End Class
///
"sm" <sm@discussions.microsoft.com> Hi All,
Can anybody give me the syntax to insert a record into SQL server through VB code using variables?
The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")"
where str1 = "col1" dt1 = getdate() str2 = "col2" str3 = "col3"
Thanks This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by newbie_mw |
last post: by
|
reply
views
Thread by Ted Greek |
last post: by
|
2 posts
views
Thread by mvr |
last post: by
|
6 posts
views
Thread by Kathy Burke |
last post: by
|
7 posts
views
Thread by Cindy H |
last post: by
| | |
1 post
views
Thread by bcap |
last post: by
| | | | | | | | | | | |