gpt4 book ai didi

c# - sqldatetime溢出异常c#.NET

转载 作者:太空狗 更新时间:2023-10-29 23:39:51 24 4
gpt4 key购买 nike

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)); 

如图所示,您的代码看起来没问题,但由于本地化问题或您的区域/时间设置有问题,转换可能会发生某些事情,因此请查看这是否有效。

关于c# - sqldatetime溢出异常c#.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com