Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Date Selecting Query in C#

Author  Topic 

jp2code
Posting Yak Master

175 Posts

Posted - 2008-10-03 : 10:09:20
I like using store procedures because it allows me to specify what the format of my values are going to be ahead of time; however, the SQL command "SELECT GetDate()" does not have this option.

Below is how I solved it, and I am looking for advice as to whether or not it is a good technique or if there is something more elegant:

Particularly of interest: Do I need to cast the CurrentDateTime to a DateTime object, or should the SqlDataReader already be returning it in this format?
DateTim sqlTime;
try {
m_db.Open();
using (SqlCommand cmd = new SqlCommand("SELECT GetDate() AS [CurrentDateTime]", m_db)) {
SqlDataReader r = cmd.ExecuteReader();
while (r.Read() == true) {
sqlTime = (DateTime)r["CurrentDateTime"];
}
}
} catch (InvalidOperationException e) {
Console.WriteLine(e.StackTrace);
sqlTime = DateTime.Now;
} catch (SqlException e) {
Console.WriteLine(e.StackTrace);
sqlTime = DateTime.Now;
} finally {
m_db.Close();
}



Avoid Sears Home Improvement

jp2code
Posting Yak Master

175 Posts

Posted - 2008-10-03 : 10:20:13
Why did my post come out as a hyperlink?


Avoid Sears Home Improvement
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-03 : 10:23:54
quote:
Originally posted by jp2code

Why did my post come out as a hyperlink?


Avoid Sears Home Improvement


may be you clicked the Insert Hyperlink icon on top by mistake.
[url]just like this[/url]
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2008-10-03 : 10:28:06
I think it has something to do with the C# code.

I tried posting it again, but it turned the post into a hyperlink again.

Let me try this in two parts: One with the question, and one with the code.

Here's the question (again):
quote:
I like using store procedures because it allows me to specify what the format of my values are going to be ahead of time however, the SQL command SELECT GetDate does not have this option.

Below is how I solved it, and I am looking for advice as to whether or not it is a good technique or if there is something more elegant:

Particularly of interest: Do I need to cast the CurrentDateTime to a DateTime object, or should the SqlDataReader already be returning it in this format?



Avoid Sears Home Improvement
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2008-10-03 : 10:30:43
Ah! It does not like the brackets used in C#.

Here is the code with single quotes (') replacing the brackets:
DateTime sqlTime;
try {
m_db.Open;
using (SqlCommand cmd = new SqlCommand("SELECT GetDate AS 'CurrentDateTime'", m_db)) {
SqlDataReader r = cmd.ExecuteReader();
while (r.Read() == true) {
sqlTime = (DateTime)r'"CurrentDateTime"';
}
}
} catch (InvalidOperationException e) {
Console.WriteLine(e.StackTrace);
sqlTime = DateTime.Now;
} catch (SqlException e) {
Console.WriteLine(e.StackTrace);
sqlTime = DateTime.Now;
} finally {
m_db.Close();
}



Avoid Sears Home Improvement
Go to Top of Page
   

- Advertisement -