本文介绍了DateTime.Now成SMALLDATETIME?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去获得日期和时间使用C#,然后将其插入到SQL Server中的 SMALLDATETIME 的数据类型。

Im trying to get the date and the time using C# , and then insert it into a smalldatetime data type in SQL SERVER.

这是我如何努力做到这一点:

This is how I try to do it :

DateTime date = DateTime.Now;

        sql = "INSERT INTO YTOODLE_LINKS (YTOODLE_LINKS.TASK_ID,YTOODLE_LINKS.LINK_TITLE,YTOODLE_LINKS.LINK_DESC,YTOODLE_LINKS.LINK_PATH,YTOODLE_LINKS.USER_ID,YTOODLE_LINKS.LAST_USER_EDIT)VALUES (1,'','','',2,'1',"+ date +")";

        dataObj = new DataObj();
        dataObj.InsertCommand(sql);


  connection = new SqlConnection(conn);
        connection.Open();

        cmd = new SqlCommand(sql, connection);
        cmd.ExecuteNonQuery();
        connection.Close();

然后再它给了我:附近有语法错误'16'。我想这是指我目前的时间,这是16:15现在..

and then then it gives me : "Incorrect syntax near '16'."I guess it refers to my current time , which is 16:15 right now..

推荐答案

我会建议使用的参数。 cmd.Parameters.AddWithValue(@日,与Date.toString); AddWithField 将采取适当的照顾转换。

I would suggest using parameters. cmd.Parameters.AddWithValue("@date", date.toString); The AddWithField will take care of the proper conversion.

您InsertSQL说明书就变成了:

Your InsertSQL statment becomes:

sql = "INSERT INTO YTOODLE_LINKS (YTOODLE_LINKS.TASK_ID,YTOODLE_LINKS.LINK_TITLE,YTOODLE_LINKS.LINK_DESC,YTOODLE_LINKS.LINK_PATH,YTOODLE_LINKS.USER_ID,YTOODLE_LINKS.LAST_USER_EDIT)VALUES (1,'','','',2,'1',@date)";

这篇关于DateTime.Now成SMALLDATETIME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 00:12