[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to supply a string for a datetime column in ADO.net

Posted on 2009-04-27
Medium Priority
Last Modified: 2013-12-17
I want to write a select statement in which I supply a string value for a timestamp column (in Sybase Sql Anywhere  database)

For example:
string dateTimeStr = "2009-04-27 12:34:23";
string selectStr = " Select * from Table where DateTimeColumn =' "+dateTimeStr+" ' ";

When I execute this select statement, the Odbc driver gives an error like :
Can not convert ... to timestamp.
When I use the OdbcParameterCollection of the command and add a c# DateTime type value as the parameter to the select statement, the command executes without error.
Isn't there another way other than using parametres?
It would be really good if I could give a string in the where condition..
Thanks in advance..
Question by:visne
LVL 15

Accepted Solution

oobayly earned 500 total points
ID: 24245397
I would recommend always using parameters. It may seem cumbersome at first but it far easier for readability, especially coming back to commands you've written 6 months ago.

The problem is that while some servers can implicitly convert a string to a datetime or timestamp value, other may not (I can't comment on Sybase as I've only used it a handful of times for our phone system), there may be issues with localisation on the server.

Assisted Solution

SatTechSolutions earned 500 total points
ID: 24592122
The following code will work for what you need, in addition it has the benefit protecting you from SQL injected.  You will need to add System.Data and System.Data.SqlClient to your USING's.

            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnl;
            sqlCommand.CommandText = "SELECT * FROM Table WHERE FieldName = @FieldValue";
            sqlCommand.Parameters.Add("@FieldValue", SqlDbType.DateTime).Value = DateTime.Parse("12/1/2009 13:34:20");
            SqlDataReader drRecords = sqlCommand.ExecuteReader();

Open in new window


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question