c# - 在 asp.net 中插入日期时出错

标签 c# asp.net sql-server-2008 ado.net

在通过 asp.net 将日期插入 SQL Server 2008 时发生错误。

插入语句

Insert into comment(CommentFrom, CommentTo, Comment, Cmntonscrap, Cmnttime) 
Values('" + Session["UserName"] + "','" + email.Text.ToString() + "','"  + 
       txt.Text.ToString().Trim() + "','" + cscrap.Text.ToString().Trim()  + 
       "','" + DateTime.Now.ToString().Trim() + "')";

错误:

string or binary data would be truncated,Statement is terminating

最佳答案

你的问题不在于日期。
它的字符串数据对于您尝试插入数据的列(可能是 varchar)来说太大了。

我会检查您的 Commenttxt.Text 的长度,看看是否可以插入数据。

为您的DateTime 使用sql 类型datetime。在文本列中存储日期是 inviting a lot of trouble .

  • 需要更多的存储空间。
  • 搜索这些数据将非常困难。
  • 您永远无法更改时区。

关于c# - 在 asp.net 中插入日期时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129763/

相关文章:

c# - IEnumerable 问题 : Best performance?

c# - 如何访问我的 GridView 的 HeaderTemplate 中的控件

ASP.NET MVC - 重定向到外部页面?

javascript - 在 Gridview 中查找按钮并从代码隐藏中单击它(c#)

sql-server - SQL服务器2008 : Copy a file and rename it

c# - 如何显示文件中的 PNG?

c# - 转换所有参数、返回类型、类定义以使用接口(interface)

c# - ReportViewer 控件不显示报告

sql - SQL Server中的数据根据​​父子关系排序

sql - DATEADD MS -1 不执行任何操作