【问题标题】:asp.net only inserts 255 characters into the databaseasp.net 只向数据库中插入 255 个字符
【发布时间】:2013-02-25 13:44:48
【问题描述】:

我正在做一个项目,我开始测试它的一部分。我意识到我有一个小问题,但无法理解。我正在尝试向数据库中插入一些数据,其中有一列是 nvarchar(4000)。但每当我尝试插入任意长度的字符串时,它只插入 255 个字符。为什么只需要 255 个字符?

这是我的代码,

dbCommand = db.GetStoredProcCommand("New_Post");
        db.AddInParameter(dbCommand, "PostHeader", DbType.String, txt_post_header.Text.Trim());
        db.AddInParameter(dbCommand, "PostBody", DbType.String, txt_post_body.Text.Trim());
        db.AddInParameter(dbCommand, "UserId", DbType.Guid, new Guid(Session["SessionUserID"].ToString()));
        db.AddInParameter(dbCommand, "PollDescription", DbType.String, txt_poll.Text);
        db.AddInParameter(dbCommand, "CityId", DbType.Int16, 1);
        db.AddInParameter(dbCommand, "SpecialtyID", DbType.Int16, Convert.ToInt16(Session["SessionSpecialityId"].ToString()));
        db.ExecuteNonQuery(dbCommand);

【问题讨论】:

  • 存储过程New_Post的参数类型是什么? nvarchar(4000) 也是? (而且……它在里面做什么?)
  • 存储过程是否可能将参数限制为 255 个字符?否则:您能否使用db.AddInParameter(...) 为您的DbType.String 参数指定长度 - 除非您另外指定,否则可能默认为255....
  • 你是男人 :) 非常感谢。在程序中,它被宣布为 255。
  • 您在使用数据访问应用程序块吗?

标签: asp.net sql-server insert


【解决方案1】:

由于您没有声明带有限制的参数,您仅有的两个选项是数据库字段有 256 作为限制或存储过程中的参数,请检查并解决您的问题。

【讨论】:

    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2015-06-03
    相关资源
    最近更新 更多