Reputation: 1784
I have this code
Dim conex As SqlConnection = New SqlConnection(conxst)
Dim caixa As Integer = ComboBox1.SelectedItem
Dim verdat As Date = DateTimePicker1.Text
Dim verdat1 As Date = "05/07/2012"
conex.Open()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New SqlDataAdapter("select codigo,data,horario from alteraca where data = '" & verdat1 & "' ", conex)
da.Fill(dt)
this code work when verdat1 is in the format "mm/dd/yyyy" , how i convert the date from mine datetimepicker (dd/mm/yyyy) to the format "mm/dd/yyyy" to place in the statment??? Thanks.
Upvotes: 0
Views: 20960
Reputation: 353
With
verdat1.ToString("MM/dd/yyyy")
You can select how the date is converted to a string.
Also, you can set the dateTimePicker
Custom Format as you wish :
dateTimePicker1.Format = DateTimePickerFormat.Custom
dateTimePicker1.CustomFormat = "MM/dd/yyyy"
Upvotes: 1
Reputation: 4354
Actually, a better (and safer way) to do the query is to use Parameters and Using
statements. The Using
statement automatically closes the connection, commands and adapter.
Dim caixa As Integer = ComboBox1.SelectedItem
Dim verdat As Date = DateTimePicker1.Text
Dim verdat1 As Date = "05/07/2012"
Dim ds As New DataSet
Dim dt As New DataTable
Using conex as New SQLConnection(conxst)
conex.Open()
Using cmdex as New SQLCommand("select codigo,data,horario from alteraca where data = @DATE " , conxst)
cmdex.Parameters.AddWithValue("@DATE",verdat1)
Using da As New SqlDataAdapter(cmdex)
da.Fill(dt)
End Using
End Using
End Using
Upvotes: 1
Reputation: 78185
You shouldn't use string representation of a value when you already have binary representation.
Dim dt As New DataTable
Using conex As New SqlConnection(conxst)
conex.Open()
Using cmd As New SqlCommand("select codigo, data, horario from alteraca where data = @data", conex)
cmd.Parameters.AddWithValue("@data", DateTimePicker1.Value)
Using da As New SqlDataAdapter(cmd)
da.Fill(dt)
End Using
End Using
End Using
Upvotes: 4
Reputation: 43743
You can convert the date like this:
verdat1.ToString("MM/dd/yyyy")
Upvotes: 0
Reputation: 159
In sql query -use to_date to convert String to date -and to char for reverse
In your case
select codigo,data,horario from alteraca where data = to_date('" & verdat1 & "','dd/mm/yyyy') ", conex
here 'dd/mm/yyyy'
is format of your variable...
Upvotes: 1