开发者

Iterate a Linq Expression and convert it into sqlparameter

since the Map can be written code as bellow by Enterlib 5.0,

.Map(p => p.ID).ToColumn("ID").Map(p => p.Version).ToColumn("ver")

Now I want to make AddInParameter have the same feature, Take a Database object for an开发者_运维百科 exmple, see

Database db=DatabaseManager.Create();
db.GetStoredProcCommand(“Stored procedure Name”);

So I want to encapsulate the constructor with Parameters like this,

db.SetParamter("@Parameter1",value).SetParamter("@Parameter2",2)
                                   .SetParamter("@Parameter3",3).Build();

After setting the params of db, then we can execute the command "db.ExecuteNonQuery();"

How i can use "Expression>" & convert it to SQLParameter?

thanks...


Extension methods to the rescue!

(Assuming you're using ADO.NET SqlClient)

public static class Extensions
{
    public static Database SetParameter(this Database db, string name, object value)
    {
        if (db == null) throw new ArgumentNullException();

        DbCommand command = db.CurrentCommand; // or whatever
        command.Parameters.AddWithValue(name, value);

        return db;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜