dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersPhagu Mahato
Ranjith Kumar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » .NET »

How to add date time in sql databae using c#


Posted Date: 17 Sep 2012      Posted By:: PRANAV KUMAR     Member Level: Bronze    Member Rank: 0     Points: 1   Responses: 6




hi

How to add date time in sql databae using c#




Responses

#688750    Author: Paritosh Mohapatra      Member Level: Diamond      Member Rank: 6     Date: 17/Sep/2012   Rating: 2 out of 52 out of 5     Points: 3

SQL Server supports some specific date formats. You may use yyyy-MM-dd format.

Please check the following code:


string dt = DateTime.Now.ToString("yyyy-MM-dd");


You should not worry about how the date is stored. You can display the date in any format you want and it can be done in SQL Server as well as .Net.


Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM



 
#688753    Author: Asheej T K        Member Level: Diamond      Member Rank: 2     Date: 17/Sep/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

I guess you wanted to insert date in the database using c#. Please check below code.


protected void btnSave_Click(object sender, EventArgs e)
{
//Get the connection string details frm web.config
con = new SqlConnection(constr);
con.Open();
cmd = new SqlCommand("insert into Employee (FName,LName,Address,DOB)values (@FName,@LName,@Address,@DOB)", con);

cmd.Parameters.Add(new SqlParameter("@FName", txtFName.Text));
cmd.Parameters.Add(new SqlParameter("@LName", txtLName.Text));
cmd.Parameters.Add(new SqlParameter("@Address", txtAddress.Text));
cmd.Parameters.Add(new SqlParameter("@DOB", txtDOB.Text));

cmd.ExecuteNonQuery();

}



Regards,
Asheej T K
Microsoft MVP[ASP.NET/IIS]
DotNetSpider MVM

Dotnet Galaxy



 
#688757    Author: SQL Programmers      Member Level: Gold      Member Rank: 176     Date: 17/Sep/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi,

You can check the following code

SqlCommand cmd = new SqlCommand("INSERT INTO <tablename> (<column>) VALUES (@values)", connection);
cmd.Parameters.AddWithValue("@value", dateTimeVariable);

cmd.ExecuteNonQuery();

SQL Server Programmers and Consultants
http://www.sql-programmers.com/






 
#688765    Author: Pawan Awasthi      Member Level: Diamond      Member Rank: 4     Date: 17/Sep/2012   Rating: 2 out of 52 out of 5     Points: 2

Hai Pavan Kumar,
To add the date with time in sql server, you need to send from C# as the date with its format like mm/dd/yyyy hh:mm:ss and then it will be saved as the date with the current time.
Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc



 
#688820    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 17/Sep/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Create table like this


create table empdt(dte datetime)


And insert like this format yyyy-MM-dd

using System.Data;
using System.Data.SqlClient;
public partial class InsertNewRecord : System.Web.UI.Page
{
SqlConnection sqlcon=new SqlConnection(@"Server=RAVI-PC\SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;"); //chaneg server details
SqlCommand sqlcmd=new SqlCommand();
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand sqlcmd = new SqlCommand("insert into empdt(dte) values('" + Convert.ToDateTime(TextBox1.Text).ToString("yyyy-MM-dd") + '), sqlcon);
sqlcmd.CommandType = CommandType.Text;
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}


Regards
N.Ravindran
Your Hard work never fails



 
#689002    Author: John Bhatt      Member Level: Gold      Member Rank: 78     Date: 18/Sep/2012   Rating: 2 out of 52 out of 5     Points: 2


string Date = DateTime.Now.ToString();

You can save this in Database in the Column Type of Date, DateTime, varchar, nvarchar and Text.

Glad to be,
John Bhatt
P.Yar.B Complex



 
Post Reply
You must Sign In to post a response.

Next : How to use the .net controls at runtime
Previous : Execution of portable executable file
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.