因为字符串超出数据库字段定义的长度,出现异常。“更新条目时出错。有关详细信息,请参见内部异常。”然后我就用substring(0,399) ,但是没有超出的字符串,比如只有200个字符,就会报错:索引和长度必须引用该字符串内的位置。 参数名: length用substring之前,必须要判断下:
book.Author = (latestBook.Author.Length > 400 ? latestBook.Author.Substring(0, 400) : latestBook.Author);我觉得还是挺长的,有没有string自带的更短的函数?
/// <summary>
/// 更新 Library 的状态:文档pdf2swf转换后,更新状态、swf页数等信息。
/// 被引用:ConvertToSwf.cs
/// </summary>
/// <param name="latestBook"></param>
public static void Update(Library latestBook)
{
using (var db = new WEBVODEntities())
{
Library book = db.Library.SingleOrDefault(a => a.Guid == latestBook.Guid);
book.Status = latestBook.Status;
book.FlvCount = latestBook.FlvCount;
book.Content = latestBook.Content;
book.FlvTime = DateTime.Now;
book.HasPic = latestBook.HasPic;
book.Author = latestBook.Author;
book.AuthorOrg = latestBook.AuthorOrg;
book.MagName = latestBook.MagName;
book.TimePdf2Swf = latestBook.TimePdf2Swf;
db.SaveChanges();
}
}
book.Author = (latestBook.Author.Length > 400 ? latestBook.Author.Substring(0, 400) : latestBook.Author);我觉得还是挺长的,有没有string自带的更短的函数?
/// <summary>
/// 更新 Library 的状态:文档pdf2swf转换后,更新状态、swf页数等信息。
/// 被引用:ConvertToSwf.cs
/// </summary>
/// <param name="latestBook"></param>
public static void Update(Library latestBook)
{
using (var db = new WEBVODEntities())
{
Library book = db.Library.SingleOrDefault(a => a.Guid == latestBook.Guid);
book.Status = latestBook.Status;
book.FlvCount = latestBook.FlvCount;
book.Content = latestBook.Content;
book.FlvTime = DateTime.Now;
book.HasPic = latestBook.HasPic;
book.Author = latestBook.Author;
book.AuthorOrg = latestBook.AuthorOrg;
book.MagName = latestBook.MagName;
book.TimePdf2Swf = latestBook.TimePdf2Swf;
db.SaveChanges();
}
}
book.Author = (latestBook.Author.Length > 400 &&!string.isnullorempty( latestBook.Author)? latestBook.Author.Substring(0, 400) : latestBook.Author);
谢谢,!string.isnullorempty( latestBook.Author)这个要写前边,不然latestBook.Author==null的话,latestBook.Author.Length > 400就会引发异常
{
public static string CutOut(this string str,int length)
{
if (str == null)
return null;
if (str.Length < length)
return str;
return str.Substring(0, length);
}
} var s = "12345";
Console.WriteLine( s.CutOut(1));
Console.WriteLine(s.CutOut(3));
Console.WriteLine(s.CutOut(5));
Console.WriteLine(s.CutOut(7));
Console.WriteLine(s.CutOut(8));你可以自己扩展一个