gastao Posted June 2, 2014 at 03:37 PM Report #557926 Posted June 2, 2014 at 03:37 PM sempre que coloco um datetimepicker da me o seguinte erro se calhar e uma coisa mm básica e não tou a ver o que é Dim adap As New OdbcDataAdapter("select * from customer where createdate >= " & DateTimePicker1.Text & "", conn) An unhandled exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll Additional information: ERROR [42000] [Microsoft][ODBC SQL Server Driver] Incorrect syntax near ','. se eu meter as datas a mao ele funciona na perfeição e o datetimepicker já ta formatado para as minhas datas
nelsonr Posted June 2, 2014 at 03:48 PM Report #557928 Posted June 2, 2014 at 03:48 PM Boas, o .Text deve estar a retornar a data em formato extendido, visto o erro indicar a presença de uma virgula. Experimenta substituir por algo assim: Dim adap As New OdbcDataAdapter("select * from customer where createdate >= " & DateTimePicker1.Value.ToShortDateString() & "", conn)
gastao Posted June 2, 2014 at 03:53 PM Author Report #557929 Posted June 2, 2014 at 03:53 PM já me aparece qualquer coisa mas não o resultado pretendido ele está me completamente a ignorar a minha condição e apresenta me o resultado todo a msm
nelsonr Posted June 2, 2014 at 04:04 PM Report #557931 Posted June 2, 2014 at 04:04 PM O mais certo é ser o formato da data. Tenta converter para outro formato (ex: DateTimePicker1.Value.ToString("MM/dd/yyyy") )
gastao Posted June 2, 2014 at 04:11 PM Author Report #557933 Posted June 2, 2014 at 04:11 PM eu mando para dentro de uma textbox da seguinte forma DateTimePicker1.Value.ToString("yyyy-MM-dd") e o resultado aparece me correcto mas quando executo na query nunca me da o resultado pretendido aparece me sempre o resultado todo e não a minha condiçao
nelsonr Posted June 2, 2014 at 04:17 PM Report #557935 Posted June 2, 2014 at 04:17 PM Provavelmente a forma mais correct seria criares um odbccommand com o comando SQL e usares parametros para definir os valores. Assim não tinhas problemas nem de formato nem de SQL Injection
gastao Posted June 2, 2014 at 04:30 PM Author Report #557941 Posted June 2, 2014 at 04:30 PM (edited) Dim escolhecliente As String escolhecliente = "select * from customer where createdate >= @data_de_entrada" Dim insertcommand As New OdbcCommand(escolhecliente, conn) If DateTimePicker1.Checked = True Then insertcommand.Parameters.Add("@data_de_entrada", OdbcType.Datetime).Value = DateTimePicker1.Value Else insertcommand.Parameters.Add("@data_de_entrada", OdbcType.Datetime).Value = DBNull.Value End If adap.Fill(escolhecliente, "customer") DataGridView1.DataSource = ds.Tables("customer") csgs me dar aqui uma ajuda o que tou a fazer de errado Edited June 2, 2014 at 04:36 PM by gastao
nelsonr Posted June 2, 2014 at 04:54 PM Report #557944 Posted June 2, 2014 at 04:54 PM Podes passar o command como parametro ao criar o adapter http://msdn.microsoft.com/en-us/library/h3csw312.aspx
gastao Posted June 2, 2014 at 05:14 PM Author Report #557947 Posted June 2, 2014 at 05:14 PM mesmo passando dessa maneira da me sempre erro Dim selectCommand As String = _ "SELECT * FROM Customer where createdate > @data_de_entrada" Dim adapter As OdbcDataAdapter = _ New OdbcDataAdapter(selectCommand, conn) adapter.Fill(selectCommand, "customer") DataGridView1.DataSource = ds.Tables("customer") conn.Close() conn.Dispose() conn.Close() adapter.InsertCommand.Parameters.Add( _ "@data_de_entrada", OdbcType.DateTime).Value = DateTimePicker1.Value.ToString("yyyy-MM-dd") An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Additional information: A conversão da cadeia "SELECT * FROM Customer where cre" para o tipo 'Integer' não é válida.
nelsonr Posted June 2, 2014 at 05:33 PM Report #557949 Posted June 2, 2014 at 05:33 PM Exemplo: Using conn As New OdbcConnection("DSN=testeodbc64") Dim command As New OdbcCommand("select * from customer where createdate >= ?", conn) command.Parameters.Add("@data_de_entrada", OdbcType.DateTime).Value = DateTimePicker1.Value Dim adap As New OdbcDataAdapter(command) Dim ds As New DataSet adap.Fill(ds) MessageBox.Show(ds.Tables(0).Rows.Count) End Using
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now