protected void BT_SubTz_Click(object sender, EventArgs e)
    {
        string title = TB_Title.Text.Trim();
        string words = TB_Words.Text.Trim();
        string companyname;
        string SQLstr;        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);             
        int type = Convert.ToInt32(DDL_Type.SelectedValue.ToString());
        if (type != 1)
        {
            TB_CompanyName.Visible = false;
            companyname = "指导员";
        }
        else
        {
            companyname = TB_CompanyName.Text.Trim();
        }        SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,LastAnswerTime,HitCount,Type,CompanyName)"
                + "values('" + title + "','"
                + words + "',"
                + "'" + Session["UserName"].ToString() + "',"
                + DateTime.Now +","
                + DateTime.Now +","
                + "0," + type + ","
                + "'" + companyname + "')";
        SqlCommand myCmd = new SqlCommand(SQLstr, conn);       
        try
        {
            conn.Open();
            myCmd.ExecuteNonQuery();            
        }
        catch 
        {
            throw new Exception("连接服务器出错");
        }
        finally
        {
            myCmd.Dispose();
            conn.Close();
        }
    }这是一个向数据库插入数据的函数,但是提示错误,不解,是因为DateTime插入格式的问题?数据库DateTime

解决方案 »

  1.   

    改成这种写法把
    http://bbs.csdn.net/topics/350119289
    不要拼字符串了~另外学学3层吧~~学了3层后再学学EF吧~
      

  2.   

    sql server可以设置日期格式。
    建议使用默认的日期格式,yyyy-mm-dd Thh:mm:ss.mmm ,这样无论sql server被设置成什么格式,什么语言,都可以识别.一下来自msdn:Examples:
    2004-05-23T14:25:10
    2004-05-23T14:25:10.487
    To use the ISO 8601 format, you must specify each element in the format. This also includes the T, the colons (:), and the period (.) that are shown in the format.
    The brackets indicate that the fraction of second component is optional. The time component is specified in the 24-hour format.
    The T indicates the start of the time part of the datetime value.
    The advantage in using the ISO 8601 format is that it is an international standard with unambiguous specification. Also, this format is not affected by the SET DATEFORMAT or SET LANGUAGE setting.http://msdn.microsoft.com/en-us/library/ms187819.aspx
      

  3.   

    使用DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")
      

  4.   

    SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,[LastAnswerTime],HitCount,[Type],CompanyName)"
                    + "values('" + title + "','"
                    + words + "',"
                    + "'" + Session["UserName"].ToString() + "','"
                    + DateTime.Now +"','"
                    + DateTime.Now +"',"
                    + "0," + type + ","
                    + "'" + companyname + "')";
      

  5.   

    你数据库中CreateTime 字段是什么类型啊?DateTime.Now  也该加单引号啊!只有数字不用加单引号!
      

  6.   

    时间格式得转换一下啊,DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"),还有就是使用
    + DateTime.Now +"','"
    + DateTime.Now +"','"
      

  7.   

    打个断点调试一下,把拼接好的sql语句复制出来。在数据库里面执行一下。看看错误信息就知道了。
      

  8.   

    时间按字符串插入
    SQLstr = "insert into tb_Content(subject,words,UserName,CreateTime,[LastAnswerTime],HitCount,[Type],CompanyName)"
                    + "values('" + title + "','"
                    + words + "',"
                    + "'" + Session["UserName"].ToString() + "','"
                    + DateTime.Now.ToString() +"','"
                    + DateTime.Now.ToString() +"',"
                    + "0," + type + ","
                    + "'" + companyname + "')";
      

  9.   

    无耻的我一气之下把数据库那两个日期的类型改成了Varchar(50),整个世界安静了
      

  10.   


    那你以后怎么做report和统计啊。看我8,9楼的回答,以字符串格式输入,格式为DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")
      

  11.   


    那你以后怎么做report和统计啊。看我8,9楼的回答,以字符串格式输入,格式为DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")看帖子以后正往回改呢有得必有失嘛,投机取巧也是不得已而为之