首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

更新数据库表中datetime字段解决方法

2012-01-19 
更新数据库表中datetime字段数据库表user中有字段times,想添加记录,代码如下:publicintadduser(stringid){

更新数据库表中datetime字段
数据库表user中有字段times   ,想添加记录,代码如下:
public   int   adduser(string   id)
                {
                        SqlConnection   myconn   =   new   SqlConnection(mystring);
                        string   cmdtext   =   "insert   into   readerinfo(   id,times   )   values   ( "   +   " ' "   +   id   +   " ', "   +   " ' "   +           thetime         +   " '   ) ";
                        SqlCommand   mycommand   =   new   SqlCommand(cmdtext,   myconn);
                        int   nresult   =   -1;
                        try
                        {
                                myconn.Open();
                                nresult   =   mycommand.ExecuteNonQuery();
                        }
                        catch   (SqlException   ex)
                        {
                                throw   new   Exception(ex.Message,   ex);
                        }
                        finally
                        {
                                myconn.Close();
                        }
                        return   nresult;
                }

请问thetime   应该用什么替换?

[解决办法]
DATEADD (mm, 2, getdate())
[解决办法]
System.DateTime.Now.AddMonths(2)

[解决办法]
用DATEADD (mm, 2, getdate())可能更好些,因为推迟的时间是和数据库服务器比对而不是和Web服务器比较

热点排行