-2

I'm fetching date from the database and following is my command for it:

SqlCommand cmd = new SqlCommand("select dob from sample Where cardnum = '" + TextBox1.Text + "'");

How do i save the output of this command into datetime?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Partha
  • 21
  • 1
  • 1
  • 5

1 Answers1

0

At the simplest:

var when = (DateTime)cmd.ExecuteScalar();

However, in the more general case you woulnd need to know about readers and parameters. Or: use a tool like dapper:

var when = conn.Query<DateTime>(
    "select dob from sample Where cardnum = @num",
    new { num = TextBox1.Text } // parameters, done right
).Single();

But dapper will read entire objects too (mapping properties to columns), not just single values.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900