sqlHelper.ExecNonQuery( sql,entity);怎么编写这个方法呢
entity是实体,怎么编写这个执行Sql的语句呢
------解决方案--------------------这个要看你想怎么弄了,手动,或者泛型+反射
------解决方案--------------------
public void Insert(Engine entity)
{
string sql = @"
INSERT INTO Engine (Id, Code, Name)
VALUES (@Id, @Code, @Name)";
sqlHelper.ExecNonQuery(sql, Entity2Condition(entity));
}
private QueryCondition Entity2Condition(Engine entity)
{
QueryCondition condition = new QueryCondition();
condition.Parameters.Add(new Parameter("Id", DbType.Guid, entity.Id));
condition.Parameters.Add(new Parameter("Code", DbType.AnsiString, entity.Code));
condition.Parameters.Add(new Parameter("Name", DbType.String, entity.Name));
return condition ;
sqlHelper.ExecNonQuery( sql,QueryCondition condition){
using (SqlConnection conn=new SqlConnection("连接字符串"))
{
using (SqlCommand cmd=new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.Parameters.AddRange(condition。Parameters);
conn.Open();
int num = cmd.ExecuteNonQuery();
conn.Close();
return num;
}
}
标准(我的标准)的这个方法是:
public int ExecuteCommand(string sql, SqlParameter[] sp)
{
using (SqlConnection conn=new SqlConnection("连接字符串"))
{
using (SqlCommand cmd=new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.Parameters.AddRange(sp);
conn.Open();
int num = cmd.ExecuteNonQuery();
conn.Close();
return num;
}
}
}
}
}
这个是你要的