Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:06:33+00:00 2026-06-10T07:06:33+00:00

I am using C# and SQL Server 2005. I want to store date and

  • 0

I am using C# and SQL Server 2005. I want to store date and time in my table. In SQL Server, I use smalldatetime datatype. I am using this code.

TimeZoneInfo ob1 = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime dt = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, ob1);

and insert data through this query

insert into tablename (name,sub_date) values (@name,'{0}')",dt.ToShortTimeString()

but, the problem is that in my table date and time is stored like this 1/1/1900 5:15:00 PM. Actual date is not stored. How to store actual date and time in database.

Note: I want to show all the records of table according to date and time. So, date and time both are important for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:06:33+00:00Added an answer on June 10, 2026 at 7:06 am

    Try something like this:

    // Define insert statement with parameters
    string insertStmt = "INSERT INTO dbo.tablename (name, sub_date) VALUES (@name, @mydate)";
    
    // setup connection and command objects
    using(SqlConnection conn = new SqlConnection("your-connection-string-here"))
    using(SqlCommand cmd = new SqlCommand(insertStmt, conn))
    {
        // add parameters to command
        cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = ".....";
        cmd.Parameters.Add("@mydate", SqlDbType.DateTime).Value = dt;
    
        // open connection, execute query, close connection
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    

    That should do the trick! No messy conversion from DateTime to string – the DateTime value is preserved “as is” and stored in that same format in SQL Server. By using parameters, you’ve both eliminated the possibility of a SQL injection attack, and also, this query can be re-used if you need to call it multiple times with different values, thus increasing performance, too!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[using SQL Server 2005] I have a table full of users, I want to
To store only time from DateTime into SQL Server 2005. I am using vs.net
I am using SQL server 2005. I have a table like this - ID
Using SQL Server 2005 I want to filter the column where column value not
Using VB6 and SQL Server 2005 I want to write a sql connection for
I want to connect from home using SQL Server 2005 to another PC. I
Using SQL Server 2005 Database1 Table1 ID Date Status 001 23-02-2009 Worked 001 24-02-2009
Using SQL Server 2005 and 2008. I've got a potentially very large table (potentially
I am using ms sql server 2005 and I want to do a mass
I'm using SQL Server 2005, and want a user defined function to implement the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.