sqldatetime溢出异常c#.NET

string con = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

        SqlConnection cn = new SqlConnection(con);
        string insert_jobseeker = "INSERT INTO JobSeeker_Registration(Password,HintQuestion,Answer,Date)"
      + " values (@Password,@HintQuestion,@Answer,@Date)";

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = insert_jobseeker;

 cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 50));
            cmd.Parameters["@Password"].Value = txtPassword.Text;
            cmd.Parameters.Add(new SqlParameter("@HintQuestion", SqlDbType.VarChar, 50));
            cmd.Parameters["@HintQuestion"].Value = ddlQuestion.Text;
            cmd.Parameters.Add(new SqlParameter("@Answer", SqlDbType.VarChar, 50));
            cmd.Parameters["@Answer"].Value = txtAnswer.Text;

            **cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
            cmd.Parameters["@Date"].Value = System.DateTime.Now**

我收到了错误

“SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM.”

这是什么解决方案?

解决方法:

尝试将SQL Server端的@Date类型更改为DATETIME2(7)

然后在您的代码中使用此行代替:

cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime2)); 

你的代码看起来没问题如图所示,但由于本地化问题或者你的Region / Time设置有问题,转换可能会继续进行,所以看看是否有效.

上一篇:java – 在context.xml中使用两个DataSource的用户’root’@’localhost’访问被拒绝


下一篇:CentOS 7 Squid代理服务器正向代理-透明代理