解决方案 »
- 关于“统一多个窗体内自定义控件风格”的问题
- 两个窗体间传值
- 请看看这段代码,为什么结果都是100
- MenuStrip如何才能当ToolStrip 的哥哥?
- 怎么读取 存储过程中"@ReturnId uniqueidentifier output"返回参数值的内容???
- 急问 立即给分 如何跟据矩形中心点以及对角线与水平线的夹角画矩形
- .net中如何学习网络编程
- C#获得按钮的句柄之后如何触发,求大神,急急急!!!!!!
- 有关设置控件enable或readonly属性的问题
- 在20分钟内能回答出这道题的人,平均年薪在8万美金以上
- C#版手机GIS问题
- HttpWebResponse怎么转换成HTMLElement,
贴出objectDateSource看看
DataObjectTypeName="chinaredstar.Models.Purchase" TypeName="chinaredstar.Models.Purchase"
InsertMethod="Insert" SelectMethod="SelectEmpty">
</asp:ObjectDataSource>model public Purchase()
{
this.CreateTime = System.DateTime.Now;
} /// <summary>
/// ID
/// </summary>
[Column]
public Guid ID
{
get;
set;
} /// <summary>
/// 团购标题
/// </summary>
[Column]
public string Title
{
get;
set;
} /// <summary>
/// 链接
/// </summary>
[Column]
public string Url
{
get;
set;
} /// <summary>
/// 内容
/// </summary>
[Column]
public string Content
{
get;
set;
} /// <summary>
/// 浏览量
/// </summary>
[Column]
public int PageViews
{
get;
set;
} /// <summary>
/// 创建时间
/// </summary>
[Column]
public DateTime CreateTime
{
get;
set;
} /// <summary>
/// 状态
/// </summary>
[Column]
public bool State
{
get;
set;
}
/// <summary>
/// 小图
/// </summary>
[Column]
public String SmallImage
{
get;
set;
}
}
entity 的 insert方法 public virtual bool Insert()
{
if (this.ID is Guid)
{
this.ID = (I)Convert.ChangeType(Guid.NewGuid(), typeof(I));
} Type type = this.GetType();
List<PropertyInfo> ps = new List<PropertyInfo>(type.GetProperties().Where<PropertyInfo>(p => p.Name != "ID" && p.GetCustomAttributes(typeof(ColumnAttribute), true).Length > 0)); using (SqlConnection connection = ConnectionHelper.CreateConnection())
{
SqlCommand command = connection.CreateCommand();
if (this.ID is Guid)
{
command.CommandText = String.Format(
"INSERT INTO {0}({1}) VALUES({2})",
(type.GetCustomAttributes(typeof(TableAttribute), true)[0] as TableAttribute).TableName,
ps.Aggregate<PropertyInfo, string>("ID", (s, p) => s + "," + p.Name),
ps.Aggregate<PropertyInfo, string>("@ID", (s, p) => s + ",@" + p.Name)); command.Parameters.AddWithValue("@ID", this.ID);
}
else // 认为 ID 是自增的。
{
command.CommandText = String.Format(
"INSERT INTO {0}({1}) VALUES({2});SELECT @@IDENTITY;",
(type.GetCustomAttributes(typeof(TableAttribute), true)[0] as TableAttribute).TableName,
ps.Aggregate<PropertyInfo, string>("", (s, p) => s + "," + p.Name).Substring(1),
ps.Aggregate<PropertyInfo, string>("", (s, p) => s + ",@" + p.Name).Substring(1));
} ps.ForEach(p =>
{
command.Parameters.AddWithValue("@" + p.Name,
p.PropertyType.IsEnum ?
Convert.ChangeType(p.GetValue(this, null), Enum.GetUnderlyingType(p.PropertyType))
:
p.GetValue(this, null));
}); connection.Open();
if (this.ID is Guid)
{
return command.ExecuteNonQuery() > 0;
}
else
{
object o = command.ExecuteScalar();
if (o is I)
{
this.ID = (I)Convert.ChangeType(o, typeof(I));
return true;
}
else
{
return false;
}
}
}
}
大侠你是用的什么方法呢?