在数据库表字段中插入datetime [英] insert datetime in database table field

查看:137
本文介绍了在数据库表字段中插入datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想获取当前时间/日期并将其插入数据库表但我收到错误。



 DateTime DateTimeVariable = DateTime.Now.Date; 

SqlConnection con1 = new SqlConnection(Data Source = MZC-RWAHDAN \\ MYPROJECT2015; Persist Security Info = True; User ID = sa; Password = Fatima2010; Initial Catalog = Testing_database);

SqlCommand command1 = new SqlCommand(insert into TrackIt(FullName,UserName,actiontype,actiondesc,actiontime)VALUES(@ param1,@ param2,@ param3,@ param4,@ param5,con1);
con1.Open();


command1.Parameters.Add(new SqlParameter(@ param1,DataReader [FullName]));
command1 .Parameters.Add(new SqlParameter(@ param2,DataReader [username]));
command1.Parameters.Add(new SqlParameter(@ param3,Logged_IN));
command1.Parameters.Add(new SqlParameter(@ param4,以管理员身份登录到Admin_Page));
command1.Parameters.Add(new SqlParameter(@ param5,DateTimeVariable));

command1.ExecuteNonQuery();







我得到了全名和先前查询的用户名数据,但其余的(3,4)我输入文本和最后一个一(5)我需要输入数据/时间进行跟踪。我收到错误:



 '  @ param5'





请帮助,



谢谢。

解决方案

如果你的表中有datetime数据类型,那么只插入日期时间格式就是为什么你会收到错误。 Datetime.Now将解决您的问题。



使用:

 DateTime DateTimeVariable = DateTime.Now; 


你错过了结束括号。



  insert   into  TrackIt(FullName,UserName,actiontype,actiondesc,actiontime) VALUES  @ param1  @ param2  @ param3  @ param4  @ param5  





正确的SQL是:



 插入 TrackIt(FullName,UserName,actiontype,actiondesc,actiontime) VALUES (< span class =code-sdkkeyword> @ param1  @ pa ram2  @ param3  @ param4  @ param5   





修复是



 SqlCommand command1 =  new  SqlCommand( 插入TrackIt(FullName,UserName,actiontype,actiondesc,actiontime)VALUES(@ param1,@ param2,@ param3,@ param4,@ param5),con1); 


Hi,

I am trying to get the current time/date and insert it in database table but i get error.

DateTime DateTimeVariable = DateTime.Now.Date;

                       SqlConnection con1 = new SqlConnection("Data Source=MZC-RWAHDAN\\MYPROJECT2015;Persist Security Info=True;User ID=sa; Password=Fatima2010;Initial Catalog=Testing_database");

                       SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5", con1);
                      con1.Open();


                      command1.Parameters.Add(new SqlParameter("@param1", DataReader["FullName"]));
                      command1.Parameters.Add(new SqlParameter("@param2", DataReader["username"]));
                      command1.Parameters.Add(new SqlParameter("@param3", "Logged_IN"));
                      command1.Parameters.Add(new SqlParameter("@param4", "Logged in as Admin to Admin_Page"));
                      command1.Parameters.Add(new SqlParameter("@param5", DateTimeVariable));

                      command1.ExecuteNonQuery();




I am getting the full name and the username data from earlier query but the rest (3,4) i am entering text and the last one (5) i need to enter data/time for tracking. i get the error:

Incorrect syntax near '@param5'.



Please help,

Thanks.

解决方案

If you have datetime datatype in your table than insert the datetime format only thats why you are getting error. Datetime.Now will solve your problem.

Use:

DateTime DateTimeVariable = DateTime.Now;


You have missed the closing bracket.

insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5



Correct SQL is :

insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)



Fix is

SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)", con1);


这篇关于在数据库表字段中插入datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆