好久没写C#代码了,今天在维护公司老项目时,偶然发现一个BUG。记录一下,后面的同学就不要踩坑啦。
-------------------------------------------------------------------
该项目采用了一个国产的数据访问框架 PDF.NET ,但是我有一个表的主键是bigint,数据编号已经大于Int32.MaxValue,此时就不能再插入数据了。原因如下(红色部分转换报错):
internal static int InsertInner(EntityBase entity, List<string> objFields, CommonDB DB)
{
if (objFields == null || objFields.Count == 0)
return 0; IDataParameter[] paras = new IDataParameter[objFields.Count]; string tableName = entity.TableName;
string identityName = entity.IdentityName;
string sql = "INSERT INTO " + entity.GetSchemeTableName();
string fields = "";
string values = "";
int index = 0; //获取实体属性信息缓存
var entityFieldsCache = EntityFieldsCache.Item(entity.GetType()); foreach (string field in objFields)
{
if (identityName != field)
{
fields += ",[" + field + "]";
string paraName = DB.GetParameterChar + "P" + index.ToString();
values += "," + paraName;
paras[index] = DB.GetParameter(paraName, entity.PropertyList(field)); //从缓存中获取当前field所对应的类型
Type fieldType = entityFieldsCache.GetPropertyType(field); if (fieldType == typeof(string) && paras[index].Value != null)
//为字符串类型的参数指定长度 edit at 2012.4.23
//((IDbDataParameter)paras[index]).Size = entity.GetStringFieldSize(field);
SetParameterSize(ref paras[index], entity, field,DB);
else if (fieldType == typeof(byte[]))
//为字节类型指定转换类型,防止空值时被当作字符串类型
paras[index].DbType = DbType.Binary; index++;
}
}
sql = sql + "(" + fields.TrimStart(',') + ") VALUES (" + values.TrimStart(',') + ")"; int count = 0; if (identityName != "")
{
//有自增字段
object id = entity.PropertyList(identityName); EntityCommand ec = new EntityCommand(entity, DB);
string insertKey = ec.GetInsertKey(); count = DB.ExecuteInsertQuery(sql, CommandType.Text, paras, ref id,insertKey);
entity.setProperty(identityName, Convert.ToInt32(id));
}
else
{
count = DB.ExecuteNonQuery(sql, CommandType.Text, paras);
}
if (count > 0)
entity.ResetChanges(); return count; }
该bug已经给作者说了,期待他们的及时修复。